Skip to content

Commit

Permalink
Delete ORIGINAL-SPLICE
Browse files Browse the repository at this point in the history
The original splice inference system implemented in #243 makes it unnecessary.
  • Loading branch information
jackfirth committed Aug 28, 2024
1 parent dd6b49f commit 1050838
Show file tree
Hide file tree
Showing 17 changed files with 65 additions and 113 deletions.
14 changes: 5 additions & 9 deletions default-recommendations/boolean-shortcuts.rkt
Original file line number Diff line number Diff line change
Expand Up @@ -64,25 +64,21 @@
#:description "This `if` expression can be refactored to an equivalent expression using `and`."
#:literals (if)
[(if condition then #false)
(and (ORIGINAL-SPLICE condition then))])
(and condition then)])


(define-refactoring-rule inverted-when
#:description "This negated when expression can be replaced by an unless expression."
#:literals (when not)
[(when (~and negated (not condition))
body0 body ...)
(unless condition (ORIGINAL-GAP negated body0)
(ORIGINAL-SPLICE body0 body ...))])
[(when (~and negated (not condition)) body0 body ...)
(unless condition (ORIGINAL-GAP negated body0) body0 body ...)])


(define-refactoring-rule inverted-unless
#:description "This negated `unless` expression can be replaced by a `when` expression."
#:literals (unless not)
[(unless (~and negated (not condition))
body0 body ...)
(when condition (ORIGINAL-GAP negated body0)
(ORIGINAL-SPLICE body0 body ...))])
[(unless (~and negated (not condition)) body0 body ...)
(when condition (ORIGINAL-GAP negated body0) body0 body ...)])


(define boolean-shortcuts
Expand Down
18 changes: 9 additions & 9 deletions default-recommendations/conditional-shortcuts.rkt
Original file line number Diff line number Diff line change
Expand Up @@ -56,15 +56,15 @@
#:description equivalent-conditional-description
#:literals (if)
[(if condition then-branch #false)
(and (ORIGINAL-SPLICE condition then-branch))])
(and condition then-branch)])


(define-refactoring-rule if-x-else-x-to-and
#:description equivalent-conditional-description
#:literals (if)
[(if x:id then-branch:expr y:id)
#:when (free-identifier=? #'x #'y)
(and (ORIGINAL-SPLICE x then-branch))])
(and x then-branch)])


(define-syntax-class block-expression
Expand All @@ -89,7 +89,7 @@
#:description equivalent-conditional-description
[conditional:when-or-unless-equivalent-conditional
((~if conditional.negated? unless when)
conditional.condition (ORIGINAL-SPLICE conditional.body ...))])
conditional.condition conditional.body ...)])


(define-refactoring-rule always-throwing-if-to-when
Expand Down Expand Up @@ -119,7 +119,7 @@
(header.formatted
...
((~if condition.negated? unless when) condition.base-condition fail)
(ORIGINAL-SPLICE body ...))])
body ...)])


(define-refactoring-rule cond-else-cond-to-cond
Expand All @@ -129,9 +129,9 @@
[((~and outer-cond-id cond)
clause ... last-non-else-clause
(~and outer-else-clause [else (cond nested-clause ...)]))
((ORIGINAL-SPLICE outer-cond-id clause ... last-non-else-clause)
(outer-cond-id clause ... last-non-else-clause
(ORIGINAL-GAP last-non-else-clause outer-else-clause)
(ORIGINAL-SPLICE nested-clause ...))])
nested-clause ...)])


(define-syntax-class let-refactorable-cond-clause
Expand Down Expand Up @@ -163,9 +163,9 @@
#:with (after ...)
(let ([form-after (first-syntax #'(clause-after ...))])
(if form-after
#`((ORIGINAL-GAP clause #,form-after) (ORIGINAL-SPLICE clause-after ...))
#`((ORIGINAL-GAP clause #,form-after) clause-after ...)
(list)))
((ORIGINAL-SPLICE outer-cond-id clause-before ...)
(outer-cond-id clause-before ...
(ORIGINAL-GAP form-before clause)
clause.refactored
after ...)])
Expand All @@ -177,7 +177,7 @@
(pattern (begin body ...)
#:attr uses-begin? #true
#:attr uses-let? #false
#:with (refactored ...) #'((ORIGINAL-SPLICE body ...)))
#:with (refactored ...) #'(body ...))
(pattern :refactorable-let-expression
#:attr uses-begin? #false
#:attr uses-let? #true)
Expand Down
2 changes: 1 addition & 1 deletion default-recommendations/contract-shortcuts.rkt
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@
(~and rest-kw #:rest) (~and rest-list (listof rest-contract))
result-contract)
(-> (ORIGINAL-GAP arrow-id args)
(ORIGINAL-SPLICE arg-contract ...)
arg-contract ...
(ORIGINAL-GAP args rest-kw)
rest-contract (... ...)
(ORIGINAL-GAP rest-list result-contract)
Expand Down
5 changes: 1 addition & 4 deletions default-recommendations/definition-shortcuts.rkt
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,10 @@


(require (for-syntax racket/base)
racket/sequence
rebellion/private/static-name
resyntax/default-recommendations/private/definition-context
resyntax/default-recommendations/private/syntax-lines
resyntax/refactoring-rule
resyntax/refactoring-suite
resyntax/private/syntax-replacement
syntax/parse)


Expand All @@ -32,7 +29,7 @@
rest ...)
(header.formatted ...
(define id expr) ...
(ORIGINAL-SPLICE rest ...))])
rest ...)])


(define definition-shortcuts
Expand Down
17 changes: 7 additions & 10 deletions default-recommendations/for-loop-shortcuts.rkt
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@

(require (for-syntax racket/base)
racket/list
racket/sequence
racket/set
rebellion/private/static-name
resyntax/refactoring-rule
Expand Down Expand Up @@ -132,7 +131,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 ...) #'((ORIGINAL-SPLICE first-body remaining-body ...)))
#:with (body ...) #'(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
Expand Down Expand Up @@ -181,14 +180,14 @@
#:description "`for` loops can build vectors directly."
#:literals (list->vector)
[(list->vector (loop-id:for-list-id clauses body ...))
(loop-id.vector-id (ORIGINAL-GAP loop-id clauses) (ORIGINAL-SPLICE clauses body ...))])
(loop-id.vector-id (ORIGINAL-GAP loop-id clauses) clauses body ...)])


(define-refactoring-rule list->set-to-for/set
#:description "`for` loops can build sets directly"
#:literals (list->set)
[(list->set (loop-id:for-list-id clauses body ...))
(loop-id.set-id (ORIGINAL-GAP loop-id clauses) (ORIGINAL-SPLICE clauses body ...))])
(loop-id.set-id (ORIGINAL-GAP loop-id clauses) clauses body ...)])


(define-refactoring-rule for/fold-building-hash-to-for/hash
Expand All @@ -200,7 +199,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 ...) (values key value))])
(loop iteration-clauses body ... (values key value))])


(define-syntax-class nested-for
Expand All @@ -221,7 +220,7 @@
[nested:nested-for
#:when (>= (length (attribute nested.clause)) 2)
(for* (nested.clause ...)
(ORIGINAL-SPLICE nested.body ...))])
nested.body ...)])


(define-refactoring-rule named-let-loop-to-for/first-in-vector
Expand Down Expand Up @@ -249,8 +248,7 @@
(~and original-clauses (clause ...))
(~and original-body (or condition:condition-expression ...+ last-condition)))
(loop-id (ORIGINAL-GAP loop-id original-clauses)
((ORIGINAL-SPLICE clause ...)
(~@ (~if condition.negated? #:when #:unless) condition.base-condition) ...)
(clause ... (~@ (~if condition.negated? #:when #:unless) condition.base-condition) ...)
(ORIGINAL-GAP original-clauses original-body)
last-condition)])

Expand All @@ -268,8 +266,7 @@
(pattern ((~and loop-id for*/list) (clause ...) only-body:expr)
#:when (oneline-syntax? #'only-body)
#:with refactored-loop
#'(loop-id ((ORIGINAL-SPLICE clause ...) [v (in-list only-body)])
v)))
#'(loop-id (clause ... [v (in-list only-body)]) v)))


(define-refactoring-rule apply-append-for-loop-to-for-loop
Expand Down
41 changes: 16 additions & 25 deletions default-recommendations/function-definition-shortcuts.rkt
Original file line number Diff line number Diff line change
Expand Up @@ -33,35 +33,26 @@


(define-syntax-class lambda-header
#:attributes (converted)

(pattern converted:id)

(pattern ()
#:with converted #'())

(pattern (formal ...+)
#:with converted #'((ORIGINAL-SPLICE formal ...)))

(pattern (formal ... . rest-arg:id)
#:with converted #'((ORIGINAL-SPLICE formal ... rest-arg))))
(pattern _:id)
(pattern (formal ...))
(pattern (formal ... . rest-arg:id)))


(define-syntax-class possibly-nested-lambdas
#:attributes ([converted-formals 1] [formatted-body 1] [original-formals 1])
#:attributes ([argument-lists 1] [body 1])

(pattern (_:lambda-by-any-name first-formals:lambda-header nested:possibly-nested-lambdas)
#:with (original-formals ...) #'(first-formals nested.original-formals ...)
#:with (converted-formals ...) #'(first-formals.converted nested.converted-formals ...)
#:with (formatted-body ...) #'(nested.formatted-body ...))
(pattern (_:lambda-by-any-name first-argument-list:lambda-header nested:possibly-nested-lambdas)
#:with (argument-lists ...) (cons #'first-argument-list (attribute nested.argument-lists))
#:with (body ...) #'(nested.body ...))

(pattern
(_:lambda-by-any-name
first-formals:lambda-header (~and initial-body (~not _:possibly-nested-lambdas)) body ...)
#:with (original-formals ...) #'(first-formals)
#:with (converted-formals ...) #'(first-formals.converted)
#:with (formatted-body ...)
#'((ORIGINAL-GAP first-formals initial-body) (ORIGINAL-SPLICE initial-body body ...))))
first-argument-list:lambda-header
(~and initial-body (~not _:possibly-nested-lambdas))
remaining-body ...)
#:with (argument-lists ...) #'(first-argument-list)
#:with (body ...)
#'((ORIGINAL-GAP first-argument-list initial-body) initial-body remaining-body ...)))


(define/guard (build-function-header original-header converted-lambda-formal-lists)
Expand All @@ -79,12 +70,12 @@
[(define header lambda-form:possibly-nested-lambdas)
#:when (not (syntax-property this-syntax 'class-body))
#:do [(define multiline-lambda-header-count
(count multiline-syntax? (attribute lambda-form.original-formals)))]
(count multiline-syntax? (attribute lambda-form.argument-lists)))]
#:when (< multiline-lambda-header-count 2)
#:when (oneline-syntax? #'header)
#:when (or (identifier? #'header) (zero? multiline-lambda-header-count))
#:with new-header (build-function-header #'header (attribute lambda-form.converted-formals))
(define new-header lambda-form.formatted-body ...)])
#:with new-header (build-function-header #'header (attribute lambda-form.argument-lists))
(define new-header lambda-form.body ...)])


(define-refactoring-rule define-case-lambda-to-define
Expand Down
2 changes: 1 addition & 1 deletion default-recommendations/function-shortcuts.rkt
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@
"The `apply` function accepts single arguments in addition to a trailing list argument."
#:literals (apply)
[((~and id apply) function:expr arg ... trailing-arg:trailing-list-argument)
((ORIGINAL-SPLICE id function arg ...) trailing-arg.lifted ... trailing-arg.trailing)])
(id function arg ... trailing-arg.lifted ... trailing-arg.trailing)])


(define function-shortcuts
Expand Down
4 changes: 2 additions & 2 deletions default-recommendations/hash-shortcuts.rkt
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,15 @@
#:description "The lambda can be removed from the failure result in this `hash-ref` expression."
#:literals (hash-ref)
[((~and ref hash-ref) h:expr k:expr (~and lambda-expr (_:lambda-by-any-name () v:literal-constant)))
((ORIGINAL-SPLICE ref h k) (ORIGINAL-GAP k lambda-expr) v)])
(ref h k (ORIGINAL-GAP k lambda-expr) v)])


(define-refactoring-rule hash-ref!-with-constant-lambda-to-hash-ref!-without-lambda
#:description "The lambda can be removed from the failure result in this `hash-ref!` expression."
#:literals (hash-ref!)
[((~and ref hash-ref!) h:expr k:expr
(~and lambda-expr (_:lambda-by-any-name () v:literal-constant)))
((ORIGINAL-SPLICE ref h k) (ORIGINAL-GAP k lambda-expr) v)])
(ref h k (ORIGINAL-GAP k lambda-expr) v)])


(define-syntax-class value-initializer
Expand Down
7 changes: 3 additions & 4 deletions default-recommendations/legacy-struct-migrations.rkt
Original file line number Diff line number Diff line change
Expand Up @@ -38,17 +38,16 @@


(define-splicing-syntax-class struct-option
#:attributes (keyword [expr 1] [formatted 1] [original 1])
#:attributes (keyword [expr 1] [original 1])
(pattern (~seq (~and keyword:keyword (~not :constructor-name-keyword)) expr:expr ...)
#:with (original ...) #'(keyword expr ...)
#:with (formatted ...) #'((ORIGINAL-SPLICE keyword expr ...))))
#:with (original ...) #'(keyword expr ...)))


(define-refactoring-rule define-struct-to-struct
#:description "The `define-struct` form exists for backwards compatibility, `struct` is preferred."
#:literals (define-struct)
[(define-struct id:id-maybe-super fields option:struct-option ...)
(struct id.migrated ... (ORIGINAL-SPLICE fields option.original ... ...)
(struct id.migrated ... fields option.original ... ...
#:extra-constructor-name id.make-id)])


Expand Down
15 changes: 5 additions & 10 deletions default-recommendations/legacy-syntax-migrations.rkt
Original file line number Diff line number Diff line change
Expand Up @@ -27,36 +27,31 @@
(define-refactoring-rule datum->syntax-migration
#:description "The fifth argument to `datum->syntax` is ignored."
#:literals (datum->syntax)
[((~and id datum->syntax) ctxt v srcloc prop ignored)
((ORIGINAL-SPLICE id ctxt v srcloc prop))])
[((~and id datum->syntax) ctxt v srcloc prop ignored) (id ctxt v srcloc prop)])


(define-refactoring-rule syntax-recertify-migration
#:description "The `syntax-recertify` function is a legacy function that does nothing."
#:literals (syntax-recertify)
[(syntax-recertify stx _ _ _)
stx])
[(syntax-recertify stx _ _ _) stx])


(define-refactoring-rule syntax-disarm-migration
#:description "The `syntax-disarm` function is a legacy function that does nothing."
#:literals (syntax-disarm)
[(syntax-disarm stx _)
stx])
[(syntax-disarm stx _) stx])


(define-refactoring-rule syntax-rearm-migration
#:description "The `syntax-rearm` function is a legacy function that does nothing."
#:literals (syntax-rearm)
[(syntax-rearm stx _ ...)
stx])
[(syntax-rearm stx _ ...) stx])


(define-refactoring-rule syntax-protect-migration
#:description "The `syntax-protect` function is a legacy function that does nothing."
#:literals (syntax-protect)
[(syntax-protect stx)
stx])
[(syntax-protect stx) stx])


(define-refactoring-rule syntax-local-match-introduce-migration
Expand Down
2 changes: 1 addition & 1 deletion default-recommendations/let-binding-suggestions.rkt
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@
#:literals (let)
[(let name:id header body ...)
#:when (not (set-member? (syntax-free-identifiers #'(body ...)) #'name))
(let (ORIGINAL-SPLICE header body ...))])
(let header body ...)])


(define-refactoring-rule let-values-then-call-to-call-with-values
Expand Down
2 changes: 1 addition & 1 deletion default-recommendations/match-shortcuts.rkt
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
[pattern body ...]))
(header.formatted ...
(match-define pattern subject)
(ORIGINAL-SPLICE body ...))])
body ...)])


(define match-shortcuts
Expand Down
Loading

0 comments on commit 1050838

Please sign in to comment.