|
12 | 12 | (require racket/list |
13 | 13 | racket/match |
14 | 14 | racket/set |
15 | | - racket/stream |
16 | 15 | resyntax/base |
17 | 16 | resyntax/default-recommendations/private/lambda-by-any-name |
18 | 17 | resyntax/default-recommendations/private/syntax-identifier-sets |
19 | 18 | resyntax/private/syntax-traversal |
20 | | - resyntax/private/logger |
21 | | - syntax/parse) |
| 19 | + syntax/parse |
| 20 | + syntax/strip-context) |
22 | 21 |
|
23 | 22 |
|
24 | 23 | (module+ test |
|
31 | 30 |
|
32 | 31 | (define-syntax-class single-clause-match |
33 | 32 | #:literals (match) |
34 | | - #:attributes (match-pattern [as-definition-context-body 1]) |
| 33 | + #:attributes (match-pattern subject [body 1] [as-definition-context-body 1]) |
35 | 34 |
|
36 | 35 | (pattern (match subject [match-pattern body ...]) |
37 | 36 | #:with definition #'(match-define match-pattern subject) |
|
43 | 42 | #:description "This `match` expression can be simplified using `match-define`." |
44 | 43 | #:literals (match) |
45 | 44 | (~seq body-before ... match-expression:single-clause-match) |
46 | | - #:when (set-empty? (set-intersect (syntax-bound-identifiers #'(body-before ...)) |
47 | | - (syntax-bound-identifiers #'match-expression.match-pattern))) |
48 | | - #:with (new-body ...) (if (empty? (attribute body-before)) |
49 | | - (attribute match-expression.as-definition-context-body) |
50 | | - #'(~focus-replacement-on |
51 | | - (match-expression.as-definition-context-body ...))) |
| 45 | + |
| 46 | + #:do |
| 47 | + [(define pattern-ids |
| 48 | + (syntax-bound-identifiers (attribute match-expression.match-pattern))) |
| 49 | + (define pattern-ids-in-surrounding-context |
| 50 | + (syntax-bound-identifiers |
| 51 | + (replace-context (attribute match-expression) (attribute match-expression.match-pattern)))) |
| 52 | + (define body-ids (syntax-bound-identifiers #'(body-before ... match-expression.subject))) |
| 53 | + (define subject-ids-in-body-context |
| 54 | + (syntax-bound-identifiers |
| 55 | + (replace-context |
| 56 | + (first (attribute match-expression.body)) (attribute match-expression.subject))))] |
| 57 | + #:when (set-empty? (set-intersect pattern-ids-in-surrounding-context body-ids)) |
| 58 | + #:when (set-empty? (set-intersect pattern-ids subject-ids-in-body-context)) |
| 59 | + #:with (new-body ...) |
| 60 | + (if (empty? (attribute body-before)) |
| 61 | + (attribute match-expression.as-definition-context-body) |
| 62 | + #'(~focus-replacement-on |
| 63 | + (match-expression.as-definition-context-body ...))) |
| 64 | + |
52 | 65 | (body-before ... new-body ...)) |
53 | 66 |
|
54 | 67 |
|
|
144 | 157 |
|
145 | 158 |
|
146 | 159 | (define-refactoring-suite match-shortcuts |
147 | | - #:rules (predicate-pattern-with-lambda-to-when |
148 | | - single-clause-match-to-match-define)) |
| 160 | + #:rules (predicate-pattern-with-lambda-to-when |
| 161 | + single-clause-match-to-match-define)) |
0 commit comments