|
91 | 91 | #:attributes (negated? condition [body 1]) |
92 | 92 | #:literals (cond void not begin let) |
93 | 93 |
|
94 | | - (pattern (cond [(not condition) (void)] [else :block-expression]) #:with negated? #false) |
95 | | - (pattern (cond [(not condition) :block-expression] [else (void)]) #:with negated? #true) |
96 | | - (pattern (cond [condition (void)] [else :block-expression]) #:with negated? #true) |
97 | | - (pattern (cond [condition :block-expression] [else (void)]) #:with negated? #false)) |
| 94 | + (pattern (cond [(not condition) (void)] [else block-expr:block-expression]) |
| 95 | + #:with negated? #false |
| 96 | + #:with (body ...) #'(block-expr.body ...)) |
| 97 | + (pattern (cond [(not condition) block-expr:block-expression] [else (void)]) |
| 98 | + #:with negated? #true |
| 99 | + #:with (body ...) #'(block-expr.body ...)) |
| 100 | + (pattern (cond [condition (void)] [else block-expr:block-expression]) |
| 101 | + #:with negated? #true |
| 102 | + #:with (body ...) #'(block-expr.body ...)) |
| 103 | + (pattern (cond [condition block-expr:block-expression] [else (void)]) |
| 104 | + #:with negated? #false |
| 105 | + #:with (body ...) #'(block-expr.body ...))) |
98 | 106 |
|
99 | 107 |
|
100 | 108 | (define-refactoring-rule cond-void-to-when-or-unless |
@@ -257,18 +265,32 @@ tail expression outside `cond` lets you replace `cond` with `when`." |
257 | 265 |
|
258 | 266 | (define-refactoring-rule cond-begin-to-cond |
259 | 267 | #:description "The bodies of `cond` clauses are already implicitly wrapped in `begin`." |
260 | | - #:literals (cond begin) |
| 268 | + #:literals (cond begin else void) |
261 | 269 | (cond clause-before ... [condition (begin body ...)] clause-after ...) |
| 270 | + ;; Don't match if this is the else clause of a cond that could be converted to when/unless |
| 271 | + #:when (not (and (empty? (attribute clause-after)) |
| 272 | + (= (length (attribute clause-before)) 1) |
| 273 | + (syntax-parse (first (attribute clause-before)) |
| 274 | + #:literals (void not) |
| 275 | + [(~or [_ (void)] |
| 276 | + [(not _) (void)]) #true] |
| 277 | + [_ #false]))) |
| 278 | + ;; Also don't match if the clause after is [else (void)] |
| 279 | + #:when (or (empty? (attribute clause-after)) |
| 280 | + (syntax-parse #'(clause-after ...) |
| 281 | + #:literals (else void) |
| 282 | + [(~not ([else (void)])) #true] |
| 283 | + [_ #false])) |
262 | 284 | (cond clause-before ... [condition body ...] clause-after ...)) |
263 | 285 |
|
264 | 286 |
|
265 | 287 | (define-refactoring-suite conditional-shortcuts |
266 | 288 | #:rules (always-throwing-cond-to-when |
267 | 289 | always-throwing-if-to-when |
| 290 | + cond-begin-to-cond |
268 | 291 | cond-else-cond-to-cond |
269 | 292 | cond-else-if-to-cond |
270 | 293 | cond-void-to-when-or-unless |
271 | | - cond-begin-to-cond |
272 | 294 | explicit-cond-else-void |
273 | 295 | if-begin-to-cond |
274 | 296 | if-else-cond-to-cond |
|
0 commit comments