diff --git a/default-recommendations/conditional-shortcuts.rkt b/default-recommendations/conditional-shortcuts.rkt index 6769c72..7968b5e 100644 --- a/default-recommendations/conditional-shortcuts.rkt +++ b/default-recommendations/conditional-shortcuts.rkt @@ -32,11 +32,11 @@ #:literals (if) (pattern (if cond then nested:nested-if-else) - #:with (branch ...) #'(NEWLINE [cond then] nested.branch ...) + #:with (branch ...) #'([cond then] nested.branch ...) #:attr branches-size (+ 1 (attribute nested.branches-size))) (pattern (if cond then default) - #:with (branch ...) #'(NEWLINE [cond then] NEWLINE [else default]) + #:with (branch ...) #'([cond then] [else default]) #:attr branches-size 2)) @@ -89,7 +89,7 @@ #:description equivalent-conditional-description [conditional:when-or-unless-equivalent-conditional ((~if conditional.negated? unless when) - conditional.condition NEWLINE (ORIGINAL-SPLICE conditional.body ...))]) + conditional.condition (ORIGINAL-SPLICE conditional.body ...))]) (define-refactoring-rule always-throwing-if-to-when @@ -101,8 +101,8 @@ fail:always-throwing-expression else-expression)) (header.formatted - ... NEWLINE - ((~if condition.negated? unless when) condition.base-condition NEWLINE fail) NEWLINE + ... + ((~if condition.negated? unless when) condition.base-condition fail) else-expression)]) @@ -117,8 +117,8 @@ [else body ...])) (header.formatted - ... NEWLINE - ((~if condition.negated? unless when) condition.base-condition NEWLINE fail) NEWLINE + ... + ((~if condition.negated? unless when) condition.base-condition fail) (ORIGINAL-SPLICE body ...))]) @@ -198,11 +198,11 @@ (if (or (multiline-syntax? #'condition) (multiline-syntax? #'then-expr) (attribute then-expr.uses-begin?)) - #'(NEWLINE [condition (ORIGINAL-GAP condition then-expr) then-expr.refactored ...]) + #'([condition (ORIGINAL-GAP condition then-expr) then-expr.refactored ...]) #'((ORIGINAL-GAP condition then-expr) [condition then-expr.refactored ...])) #:with (false-branch ...) (if (attribute else-expr.uses-begin?) - #'(NEWLINE [else (ORIGINAL-GAP then-expr else-expr) else-expr.refactored ...]) + #'([else (ORIGINAL-GAP then-expr else-expr) else-expr.refactored ...]) #'((ORIGINAL-GAP then-expr else-expr) [else else-expr.refactored ...])) (cond true-branch ... @@ -221,11 +221,11 @@ (if (or (multiline-syntax? #'condition) (multiline-syntax? #'then-expr) (attribute then-expr.uses-let?)) - #'(NEWLINE [condition (ORIGINAL-GAP condition then-expr) then-expr.refactored ...]) + #'([condition (ORIGINAL-GAP condition then-expr) then-expr.refactored ...]) #'((ORIGINAL-GAP condition then-expr) [condition then-expr.refactored ...])) #:with (false-branch ...) (if (attribute else-expr.uses-let?) - #'(NEWLINE [else (ORIGINAL-GAP then-expr else-expr) else-expr.refactored ...]) + #'([else (ORIGINAL-GAP then-expr else-expr) else-expr.refactored ...]) #'((ORIGINAL-GAP then-expr else-expr) [else else-expr.refactored ...])) (cond true-branch ... diff --git a/default-recommendations/definition-shortcuts.rkt b/default-recommendations/definition-shortcuts.rkt index a05cc41..4a575db 100644 --- a/default-recommendations/definition-shortcuts.rkt +++ b/default-recommendations/definition-shortcuts.rkt @@ -30,14 +30,8 @@ [(header:header-form-allowing-internal-definitions (define-values (id:id ...) (values expr:expr ...)) rest ...) - - #:with ((gap ...) ...) - (for/list ([e (in-syntax #'(expr ...))]) - (if (multiline-syntax? e) #'(NEWLINE) #'())) - (header.formatted ... - NEWLINE - (~@ (define id gap ... expr) NEWLINE) ... + (define id expr) ... (ORIGINAL-SPLICE rest ...))]) diff --git a/default-recommendations/for-loop-shortcuts.rkt b/default-recommendations/for-loop-shortcuts.rkt index 28a2fc7..f6b618d 100644 --- a/default-recommendations/for-loop-shortcuts.rkt +++ b/default-recommendations/for-loop-shortcuts.rkt @@ -87,7 +87,7 @@ list-expression:sequence-syntax-convertible-list-expression)) #:when (bound-identifier=? #'x #'y) #:with nesting-loop? #false - #:with loop-clauses #'([x list-expression.refactored] NEWLINE #:when filter-body) + #:with loop-clauses #'([x list-expression.refactored] #:when filter-body) #:with loop #'(for/list loop-clauses loop-body ...)) (pattern @@ -98,7 +98,7 @@ list-expression:sequence-syntax-convertible-list-expression)) #:when (not (bound-identifier=? #'x #'y)) #:with nesting-loop? #true - #:with loop-clauses #'([y list-expression.refactored] NEWLINE [x append-map-body.refactored]) + #:with loop-clauses #'([y list-expression.refactored] [x append-map-body.refactored]) #:with loop #'(for*/list loop-clauses loop-body ...)) (pattern @@ -114,7 +114,7 @@ #:description "Applying `+` to a list of numbers can be replaced with a `for/sum` loop." #:literals (apply +) [(apply + loop:for-loop-convertible-list-expression) - ((~if loop.nesting-loop? for*/sum for/sum) loop.loop-clauses (~@ NEWLINE loop.loop-body) ...)]) + ((~if loop.nesting-loop? for*/sum for/sum) loop.loop-clauses loop.loop-body ...)]) ;; A loop body function is a lambda expression that is passed to a function like map, for-each, or @@ -132,7 +132,7 @@ ;; assume all such lambdas are multi-line, and multi-line for-each functions are typically easier ;; to read when they're in the body of a for loop. (pattern (_:lambda-by-any-name (x) first-body remaining-body ...+) - #:with (body ...) #'(NEWLINE (ORIGINAL-SPLICE first-body remaining-body ...))) + #:with (body ...) #'((ORIGINAL-SPLICE first-body remaining-body ...))) ;; We don't bother migrating for-each forms with only a single body form unless the body form is ;; exceptionally long, so that forms which span multiple lines tend to get migrated. By not @@ -140,7 +140,7 @@ ;; enough to need a lot of refactoring in the first place. (pattern (_:lambda-by-any-name (x) only-body) #:when (>= (syntax-span #'only-body) 60) - #:with (body ...) #'(NEWLINE only-body))) + #:with (body ...) #'(only-body))) (define-refactoring-rule for-each-to-for @@ -148,7 +148,7 @@ #:literals (for-each) [(for-each function:worthwhile-loop-body-function loop:for-clause-convertible-list-expression) ((~if loop.flat? for for*) - ((~@ loop.leading-clause NEWLINE) ... [function.x loop.trailing-expression]) + (loop.leading-clause ... [function.x loop.trailing-expression]) function.body ...)]) @@ -157,8 +157,8 @@ #:literals (ormap) [(ormap function:worthwhile-loop-body-function loop:for-clause-convertible-list-expression) ((~if loop.flat? for/or for*/or) - ((~@ loop.leading-clause NEWLINE) ... [function.x loop.trailing-expression]) - function.body ...)]) + (loop.leading-clause ... [function.x loop.trailing-expression]) + function.body ...)]) (define-refactoring-rule andmap-to-for/and @@ -166,8 +166,8 @@ #:literals (andmap) [(andmap function:worthwhile-loop-body-function loop:for-clause-convertible-list-expression) ((~if loop.flat? for/and for*/and) - ((~@ loop.leading-clause NEWLINE) ... [function.x loop.trailing-expression]) - function.body ...)]) + (loop.leading-clause ... [function.x loop.trailing-expression]) + function.body ...)]) (define-syntax-class for-list-id @@ -200,7 +200,7 @@ (hash-set h-usage:id key value)) #:when (free-identifier=? #'h #'h-usage) #:when (not (set-member? (syntax-free-identifiers #'(body ...)) #'h)) - (loop (ORIGINAL-SPLICE iteration-clauses body ...) NEWLINE (values key value))]) + (loop (ORIGINAL-SPLICE iteration-clauses body ...) (values key value))]) (define-syntax-class nested-for @@ -209,7 +209,7 @@ #:literals (for) (pattern (for (outer-clause) nested:nested-for) - #:with (clause ...) #'(outer-clause NEWLINE nested.clause ...) + #:with (clause ...) #'(outer-clause nested.clause ...) #:with (body ...) #'(nested.body ...)) (pattern (for (only-clause) body ...) @@ -220,7 +220,7 @@ #:description "These nested `for` loops can be replaced by a single `for*` loop." [nested:nested-for #:when (>= (length (attribute nested.clause)) 2) - (for* (nested.clause ...) NEWLINE + (for* (nested.clause ...) (ORIGINAL-SPLICE nested.body ...))]) @@ -238,8 +238,7 @@ (free-identifier=? #'i1 #'i3) (free-identifier=? #'i1 #'i4) (free-identifier=? #'vec1 #'vec2)) - (for/first ([x (in-vector vec1)] - NEWLINE #:when condition) NEWLINE + (for/first ([x (in-vector vec1)] #:when condition) true-branch)]) @@ -251,7 +250,7 @@ (~and original-body (or condition:condition-expression ...+ last-condition))) (loop-id (ORIGINAL-GAP loop-id original-clauses) ((ORIGINAL-SPLICE clause ...) - (~@ NEWLINE (~if condition.negated? #:when #:unless) condition.base-condition) ...) + (~@ (~if condition.negated? #:when #:unless) condition.base-condition) ...) (ORIGINAL-GAP original-clauses original-body) last-condition)]) @@ -263,14 +262,14 @@ (pattern (for/list (only-clause) only-body:expr) #:when (oneline-syntax? #'only-body) #:with refactored-loop - #'(for*/list (only-clause NEWLINE [v (in-list only-body)]) - NEWLINE v)) + #'(for*/list (only-clause [v (in-list only-body)]) + v)) (pattern ((~and loop-id for*/list) (clause ...) only-body:expr) #:when (oneline-syntax? #'only-body) #:with refactored-loop - #'(loop-id ((ORIGINAL-SPLICE clause ...) NEWLINE [v (in-list only-body)]) - NEWLINE v))) + #'(loop-id ((ORIGINAL-SPLICE clause ...) [v (in-list only-body)]) + v))) (define-refactoring-rule apply-append-for-loop-to-for-loop diff --git a/default-recommendations/function-definition-shortcuts-test.rkt b/default-recommendations/function-definition-shortcuts-test.rkt index 1eda165..189f983 100644 --- a/default-recommendations/function-definition-shortcuts-test.rkt +++ b/default-recommendations/function-definition-shortcuts-test.rkt @@ -32,12 +32,13 @@ test: "lambda variable definition with no arguments to function definition" ------------------------------ -test: "one-line lambda variable definition to one-line function definition" +test: "one-line lambda variable definition to function definition" ------------------------------ (define f (λ (a b c) 1)) ------------------------------ ------------------------------ -(define (f a b c) 1) +(define (f a b c) + 1) ------------------------------ @@ -96,22 +97,6 @@ test: "lambda function definition with closed-over expressions not refactorable" ------------------------------ -test: "lambda variable definition with long header to function definition with preserved formatting" ------------------------------- -(define f - (λ (a - b - c) - 1)) ------------------------------- ------------------------------- -(define (f a - b - c) - 1) ------------------------------- - - test: "lambda variable definition with commented body to definition with preserved comments" ------------------------------ (define f diff --git a/default-recommendations/function-definition-shortcuts.rkt b/default-recommendations/function-definition-shortcuts.rkt index d69c185..a68d726 100644 --- a/default-recommendations/function-definition-shortcuts.rkt +++ b/default-recommendations/function-definition-shortcuts.rkt @@ -101,7 +101,7 @@ #:when (free-identifiers=? #'(case1-arg ...) #'(case2-arg ...)) #:when (free-identifiers=? #'(case1-arg ...) #'(usage1 ...)) (define (id case2-arg ... [bonus-arg default]) - (~@ NEWLINE body) ...)]) + body ...)]) (define function-definition-shortcuts diff --git a/default-recommendations/legacy-struct-migrations-test.rkt b/default-recommendations/legacy-struct-migrations-test.rkt index 169509f..0fe5100 100644 --- a/default-recommendations/legacy-struct-migrations-test.rkt +++ b/default-recommendations/legacy-struct-migrations-test.rkt @@ -106,14 +106,16 @@ test: "define-struct with options with separating whitespace" test: "define-struct with field comments" ---------------------------------------- -(define-struct point (x ;; The X coordinate of the point - y ;; The Y coordinate of the point - )) +(define-struct point + (x ;; The X coordinate of the point + y ;; The Y coordinate of the point + )) ---------------------------------------- ---------------------------------------- -(struct point (x ;; The X coordinate of the point - y ;; The Y coordinate of the point - ) +(struct point + (x ;; The X coordinate of the point + y ;; The Y coordinate of the point + ) #:extra-constructor-name make-point) ---------------------------------------- @@ -124,8 +126,7 @@ test: "define-struct with comments between options" ;; Custom write implementation #:property prop:custom-write - (λ (this out mode) - (write-string "#" out)) + (λ (this out mode) (write-string "#" out)) ;; Field guard #:guard (λ (x y _) (values x y))) @@ -135,8 +136,7 @@ test: "define-struct with comments between options" ;; Custom write implementation #:property prop:custom-write - (λ (this out mode) - (write-string "#" out)) + (λ (this out mode) (write-string "#" out)) ;; Field guard #:guard (λ (x y _) (values x y)) diff --git a/default-recommendations/legacy-struct-migrations.rkt b/default-recommendations/legacy-struct-migrations.rkt index bc6c647..3e871c6 100644 --- a/default-recommendations/legacy-struct-migrations.rkt +++ b/default-recommendations/legacy-struct-migrations.rkt @@ -41,7 +41,7 @@ #:attributes (keyword [expr 1] [formatted 1] [original 1]) (pattern (~seq (~and keyword:keyword (~not :constructor-name-keyword)) expr:expr ...) #:with (original ...) #'(keyword expr ...) - #:with (formatted ...) #'(NEWLINE (ORIGINAL-SPLICE keyword expr ...)))) + #:with (formatted ...) #'((ORIGINAL-SPLICE keyword expr ...)))) (define-refactoring-rule define-struct-to-struct @@ -49,7 +49,7 @@ #:literals (define-struct) [(define-struct id:id-maybe-super fields option:struct-option ...) (struct id.migrated ... (ORIGINAL-SPLICE fields option.original ... ...) - NEWLINE #:extra-constructor-name id.make-id)]) + #:extra-constructor-name id.make-id)]) (define legacy-struct-migrations diff --git a/default-recommendations/match-shortcuts.rkt b/default-recommendations/match-shortcuts.rkt index 5883a2f..604b46f 100644 --- a/default-recommendations/match-shortcuts.rkt +++ b/default-recommendations/match-shortcuts.rkt @@ -30,8 +30,8 @@ [(header:header-form-allowing-internal-definitions (match subject [pattern body ...])) - (header.formatted ... NEWLINE - (match-define pattern subject) NEWLINE + (header.formatted ... + (match-define pattern subject) (ORIGINAL-SPLICE body ...))]) diff --git a/default-recommendations/miscellaneous-suggestions.rkt b/default-recommendations/miscellaneous-suggestions.rkt index 455aca7..3d308bc 100644 --- a/default-recommendations/miscellaneous-suggestions.rkt +++ b/default-recommendations/miscellaneous-suggestions.rkt @@ -28,27 +28,21 @@ #:description if-begin-to-cond-message #:literals (if begin) [(if condition (begin then-body ...) else-branch) - (cond - NEWLINE [condition (~@ NEWLINE then-body) ...] - NEWLINE [else NEWLINE else-branch])]) + (cond [condition then-body ...] [else else-branch])]) (define-refactoring-rule if-else-begin-to-cond #:description if-begin-to-cond-message #:literals (if begin) [(if condition then-branch (begin else-body ...)) - (cond - NEWLINE [condition NEWLINE then-branch] - NEWLINE [else (~@ NEWLINE else-body) ...])]) + (cond [condition then-branch] [else else-body ...])]) (define-refactoring-rule if-else-cond-to-cond #:description if-begin-to-cond-message #:literals (if cond) [(if condition then-branch (cond clause ...)) - (cond - NEWLINE [condition NEWLINE then-branch] - (~@ NEWLINE clause) ...)]) + (cond [condition then-branch] clause ...)]) (define-refactoring-rule cond-else-if-to-cond @@ -56,22 +50,14 @@ expression." #:literals (cond else if) [(cond clause ... [else (if inner-condition inner-then-branch else-branch)]) - (cond - (~@ NEWLINE clause) ... - NEWLINE [inner-condition NEWLINE inner-then-branch] - NEWLINE [else NEWLINE else-branch])]) + (cond clause ... [inner-condition inner-then-branch] [else else-branch])]) (define-refactoring-rule cond-begin-to-cond #:description "The bodies of `cond` clauses are already implicitly wrapped in `begin`." #:literals (cond begin) - [(cond clause-before ... - [condition (begin body ...)] - clause-after ...) - (cond - (~@ NEWLINE clause-before) ... - NEWLINE [condition (~@ NEWLINE body) ...] - (~@ NEWLINE clause-after) ...)]) + [(cond clause-before ... [condition (begin body ...)] clause-after ...) + (cond clause-before ... [condition body ...] clause-after ...)]) (define-refactoring-rule or-cond-to-cond @@ -79,9 +65,7 @@ reducing nesting." #:literals (or cond) [(or condition (cond clause ...)) - (cond - NEWLINE [condition #t] - (~@ NEWLINE clause) ...)]) + (cond [condition #t] clause ...)]) (define-refactoring-rule and-match-to-match @@ -90,9 +74,7 @@ #:literals (and match) [(and and-subject:id (match match-subject:id match-clause ...)) #:when (free-identifier=? #'and-subject #'match-subject) - (match match-subject - NEWLINE [#false #false] - (~@ NEWLINE match-clause) ...)]) + (match match-subject [#false #false] match-clause ...)]) (define miscellaneous-suggestions diff --git a/default-recommendations/private/let-binding.rkt b/default-recommendations/private/let-binding.rkt index 18dfdff..ba30c07 100644 --- a/default-recommendations/private/let-binding.rkt +++ b/default-recommendations/private/let-binding.rkt @@ -79,7 +79,6 @@ #:with (refactored ...) #'(leading-body.formatted ... - NEWLINE bindings.outer-definition ... inner-body.formatted ...)) @@ -99,7 +98,6 @@ #:with (refactored ...) #'(leading-body.formatted ... - NEWLINE bindings.outer-definition ... inner-body.formatted ...))) @@ -115,22 +113,16 @@ (test-case "refactorable let bindings after non-conflicting definitions" (define stx #'((define a 1) (let ([b a]) (+ a b)))) (define expected - '(NEWLINE - (define a 1) - NEWLINE + '((define a 1) (define b a) - NEWLINE (+ a b))) (check-equal? (parse stx) expected)) (test-case "refactorable let* bindings after non-conflicting definitions" (define stx #'((define a 1) (let* ([b a]) (+ a b)))) (define expected - '(NEWLINE - (define a 1) - NEWLINE + '((define a 1) (define b a) - NEWLINE (+ a b))) (check-equal? (parse stx) expected)) @@ -277,7 +269,6 @@ (define (parsed-binding-clause-definition clause) (define rhs (parsed-binding-clause-right-hand-side clause)) (define id-side (parsed-binding-clause-identifier-side clause)) - (define long? (> (+ (syntax-span id-side) (syntax-span rhs)) 90)) ;; conservative under-estimate (define different-lines? (not (equal? (syntax-line id-side) (syntax-line rhs)))) (match (parsed-binding-clause-bound-identifiers clause) [(list id) @@ -285,23 +276,21 @@ #:literals (lambda λ) [((~or lambda λ) (arg:formal ...) body ...) #`(define (#,id (~@ (~? arg.kw) (~? [arg.name arg.default] arg.name)) ...) - (~@ NEWLINE body) ...)] + body ...)] [((~or lambda λ) (arg:formal ... . rest:identifier) body ...) #`(define (#,id (~@ (~? arg.kw) (~? [arg.name arg.default] arg.name)) ... . rest) - (~@ NEWLINE body) ...)] + body ...)] [((~or lambda λ) args:identifier body ...) #:with id* id #`(define (id* . args) - (~@ NEWLINE body) ...)] + body ...)] [_ (cond [different-lines? #`(define #,id (ORIGINAL-GAP #,id-side #,rhs) #,rhs)] - [long? #`(define #,id NEWLINE #,rhs)] [else #`(define #,id #,rhs)])])] [_ (cond [different-lines? #`(define-values (ORIGINAL-SPLICE #,id-side #,rhs))] - [long? #`(define-values #,id-side NEWLINE #,rhs)] [else #`(define-values #,id-side #,rhs)])])) @@ -408,22 +397,18 @@ id)) -(define/guard (split-bindings-outer-definitions split) - (define definitions +(define (split-bindings-outer-definitions split) + (define/with-syntax (definition ...) (for/list ([before (in-list (split-bindings-before-cycles split))]) (parsed-binding-clause-definition before))) - (cond - [(empty? definitions) #'()] - [else - (define/with-syntax (first-definition definition ...) definitions) - #'(first-definition (~@ NEWLINE definition) ...)])) + #'(definition ...)) (define (split-bindings-inner-definitions split) (define/with-syntax (definition ...) (for/list ([after (in-list (split-bindings-after-cycles split))]) (parsed-binding-clause-definition after))) - #'((~@ NEWLINE definition) ...)) + #'(definition ...)) (define-syntax-class body-form @@ -450,7 +435,7 @@ #false))) (and (pair? (attribute form)) (last (attribute form)))) #:with (bound-id ...) #'(form.bound-id ... ...) - #:with (formatted ...) #'((~@ NEWLINE form) ...))) + #:with (formatted ...) #'(form ...))) (module+ test diff --git a/default-recommendations/syntax-rules-shortcuts.rkt b/default-recommendations/syntax-rules-shortcuts.rkt index 9b4aafe..ab7ecdd 100644 --- a/default-recommendations/syntax-rules-shortcuts.rkt +++ b/default-recommendations/syntax-rules-shortcuts.rkt @@ -20,12 +20,11 @@ (define-refactoring-rule define-syntax-syntax-rules-to-define-syntax-rule - #:description "This `define-syntax` macro can be replaced with a simpler, equivalent `define-syntax-rule` macro." + #:description + "This `define-syntax` macro can be replaced with a simpler, equivalent `define-syntax-rule` macro." #:literals (define-syntax syntax-rules) - [(define-syntax macro:id - (syntax-rules () [(_ . pattern) template])) - (define-syntax-rule (macro . pattern) - NEWLINE template)]) + [(define-syntax macro:id (syntax-rules () [(_ . pattern) template])) + (define-syntax-rule (macro . pattern) template)]) (define syntax-rules-shortcuts diff --git a/private/syntax-replacement.rkt b/private/syntax-replacement.rkt index c40bfd2..2d60575 100644 --- a/private/syntax-replacement.rkt +++ b/private/syntax-replacement.rkt @@ -5,7 +5,6 @@ (provide - SPACE NEWLINE ORIGINAL-GAP ORIGINAL-SPLICE @@ -98,7 +97,7 @@ #:literals (quote NEWLINE SPACE ORIGINAL-GAP ORIGINAL-SPLICE) [(~or (ORIGINAL-GAP _ ...) (ORIGINAL-SPLICE _ ...) (quote _ ...)) template] - + [(subform ...) (define (contents-to-add-between left-form right-form) (if (or (template-separator? left-form) (template-separator? right-form)) @@ -170,7 +169,7 @@ [SPACE (list (inserted-string " "))] - [NEWLINE (list (inserted-string "\n"))] + [NEWLINE (list)] [(ORIGINAL-GAP ~! before after) (define before-end (+ (sub1 (syntax-position #'before)) (syntax-span #'before)))