|
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 |
@@ -236,13 +244,56 @@ tail expression outside `cond` lets you replace `cond` with `when`." |
236 | 244 | body-after ...)) |
237 | 245 |
|
238 | 246 |
|
| 247 | +(define if-begin-to-cond-message |
| 248 | + "The `cond` form supports multiple body expressions in each branch, making `begin` unnecessary.") |
| 249 | + |
| 250 | + |
| 251 | +(define-refactoring-rule if-else-cond-to-cond |
| 252 | + #:description if-begin-to-cond-message |
| 253 | + #:literals (if cond) |
| 254 | + (if condition then-branch (cond clause ...)) |
| 255 | + (cond [condition then-branch] clause ...)) |
| 256 | + |
| 257 | + |
| 258 | +(define-refactoring-rule cond-else-if-to-cond |
| 259 | + #:description "The `else`-`if` branch of this `cond` expression can be collapsed into the `cond`\ |
| 260 | + expression." |
| 261 | + #:literals (cond else if) |
| 262 | + (cond clause ... [else (if inner-condition inner-then-branch else-branch)]) |
| 263 | + (cond clause ... [inner-condition inner-then-branch] [else else-branch])) |
| 264 | + |
| 265 | + |
| 266 | +(define-refactoring-rule cond-begin-to-cond |
| 267 | + #:description "The bodies of `cond` clauses are already implicitly wrapped in `begin`." |
| 268 | + #:literals (cond begin else void) |
| 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])) |
| 284 | + (cond clause-before ... [condition body ...] clause-after ...)) |
| 285 | + |
| 286 | + |
239 | 287 | (define-refactoring-suite conditional-shortcuts |
240 | 288 | #:rules (always-throwing-cond-to-when |
241 | 289 | always-throwing-if-to-when |
| 290 | + cond-begin-to-cond |
242 | 291 | cond-else-cond-to-cond |
| 292 | + cond-else-if-to-cond |
243 | 293 | cond-void-to-when-or-unless |
244 | 294 | explicit-cond-else-void |
245 | 295 | if-begin-to-cond |
| 296 | + if-else-cond-to-cond |
246 | 297 | if-else-false-to-and |
247 | 298 | if-void-to-when-or-unless |
248 | 299 | if-x-else-x-to-and |
|
0 commit comments