Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions default-recommendations/syntax-shortcuts-test.rkt
Original file line number Diff line number Diff line change
Expand Up @@ -53,3 +53,17 @@ test: "making a symbol with format from a keyword can be simplified to format-sy
test: "making a symbol with format from a keyword syntax object can be simplified to format-symbol"
- (string->symbol (format "make-~a" (keyword->string (syntax-e #'#:foo))))
- (format-symbol "make-~a" #'#:foo)


test: "flattening a nested ellipsis template can be simplified to a flat template"
------------------------------------------------------------------------------------------------------
(define (f stx)
(with-syntax ([((a ...) ...) stx])
(apply append
(map syntax->list (syntax->list #'((a ...) ...))))))
------------------------------------------------------------------------------------------------------
------------------------------------------------------------------------------------------------------
(define (f stx)
(with-syntax ([((a ...) ...) stx])
(syntax->list #'(a ... ...))))
------------------------------------------------------------------------------------------------------
16 changes: 15 additions & 1 deletion default-recommendations/syntax-shortcuts.rkt
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,20 @@
(format-symbol template (~replacement arg.simplified #:original arg) ...))


(define-refactoring-rule flatten-apply-append-syntax-template
#:description
"Flattening a nested ellipsis syntax template with `apply`, `append`, and `map` can be expressed\
more directly with a single flattened ellipsis template."
#:literals (apply append map syntax->list syntax)

(apply append
(map syntax->list
(syntax->list (syntax ((elem (~datum ...)) (~datum ...))))))

(syntax->list #'(elem (... ...) (... ...))))


(define-refactoring-suite syntax-shortcuts
#:rules (format-string-to-format-symbol
#:rules (flatten-apply-append-syntax-template
format-string-to-format-symbol
syntax-e-in-format-id-unnecessary))
6 changes: 4 additions & 2 deletions private/syntax-replacement.rkt
Original file line number Diff line number Diff line change
Expand Up @@ -141,12 +141,14 @@
(define end (+ start (syntax-span stx)))
(list (copied-string start end)))
(syntax-parse stx
#:literals (quote)
#:literals (quote syntax)

[(~or v:id v:boolean v:char v:keyword v:number v:regexp v:byte-regexp v:string v:bytes)
(list (inserted-string (string->immutable-string (~s (syntax-e #'v)))))]

[(quote datum) (cons (inserted-string "'") (pieces #'datum #:focused? focused?))]

[(syntax datum) (cons (inserted-string "#'") (pieces #'datum #:focused? focused?))]

[(subform ...)
(define shape (syntax-property stx 'paren-shape))
Expand Down
Loading