|
204 | 204 | (~replacement [else else-expr.refactored ...] #:original else-expr))) |
205 | 205 |
|
206 | 206 |
|
207 | | -;; Syntax class to count and extract deeply nested when expressions |
208 | | -(define-syntax-class deeply-nested-when-expression |
209 | | - #:attributes ([condition 1] [body 1]) |
210 | | - #:literals (when) |
211 | | - |
212 | | - ;; Base case: innermost when with any condition |
213 | | - (pattern (when inner-condition:expr inner-body:expr ...) |
214 | | - #:with (condition ...) #'(inner-condition) |
215 | | - #:with (body ...) #'(inner-body ...)) |
216 | | - |
217 | | - ;; Recursive case: nest of when expressions |
218 | | - (pattern (when outer-condition:expr nested:deeply-nested-when-expression) |
219 | | - #:with (condition ...) #'(outer-condition nested.condition ...) |
220 | | - #:with (body ...) #'(nested.body ...))) |
221 | | - |
222 | | - |
223 | | -;; Match three or more nested when expressions |
224 | | -(define-syntax-class triple-nested-when-expression |
225 | | - #:attributes ([condition 1] [body 1]) |
226 | | - #:literals (when) |
227 | | - (pattern (when c1:expr (when c2:expr nested:deeply-nested-when-expression)) |
228 | | - #:with (condition ...) #'(c1 c2 nested.condition ...) |
229 | | - #:with (body ...) #'(nested.body ...))) |
230 | | - |
231 | | - |
232 | | -;; Match two nested when where outer has and |
233 | | -(define-syntax-class double-nested-when-outer-and |
234 | | - #:attributes ([condition 1] [body 1]) |
| 207 | +(define-syntax-class nested-when-expression |
| 208 | + #:attributes ([condition 1] [body 1] depth) |
235 | 209 | #:literals (when and) |
236 | | - (pattern (when (and outer-parts ...) (when inner-condition:expr inner-body:expr ...)) |
237 | | - #:with (condition ...) #'(outer-parts ... inner-condition) |
238 | | - #:with (body ...) #'(inner-body ...))) |
239 | 210 |
|
240 | | - |
241 | | -;; Match two nested when where inner has and |
242 | | -(define-syntax-class double-nested-when-inner-and |
243 | | - #:attributes ([condition 1] [body 1]) |
244 | | - #:literals (when and) |
245 | | - (pattern (when outer-condition:expr (when (and inner-parts ...) inner-body:expr ...)) |
246 | | - #:with (condition ...) #'(outer-condition inner-parts ...) |
247 | | - #:with (body ...) #'(inner-body ...))) |
248 | | - |
249 | | - |
250 | | -;; Match two nested when where both are identifiers |
251 | | -(define-syntax-class double-nested-when-both-ids |
252 | | - #:attributes ([condition 1] [body 1]) |
253 | | - #:literals (when) |
254 | | - (pattern (when outer-id:id (when inner-id:id inner-body:expr ...)) |
255 | | - #:with (condition ...) #'(outer-id inner-id) |
256 | | - #:with (body ...) #'(inner-body ...))) |
| 211 | + (pattern (when (~or (and subcondition ...) first-condition) |
| 212 | + (~or nested:nested-when-expression (~seq only-body ...))) |
| 213 | + #:with (condition ...) |
| 214 | + #'((~? (~@ subcondition ...) first-condition) (~? (~@ nested.condition ...))) |
| 215 | + #:attr [body 1] (or (attribute nested.body) (attribute only-body)) |
| 216 | + #:attr depth (add1 (or (attribute nested.depth) 0)))) |
257 | 217 |
|
258 | 218 |
|
259 | 219 | (define-refactoring-rule nested-when-to-compound-when |
260 | 220 | #:description |
261 | 221 | "Nested `when` expressions can be merged into a single compound `when` expression." |
262 | | - #:literals (when and) |
263 | | - (~or nested:triple-nested-when-expression |
264 | | - nested:double-nested-when-outer-and |
265 | | - nested:double-nested-when-inner-and |
266 | | - nested:double-nested-when-both-ids) |
267 | | - (when (and nested.condition ...) nested.body ...)) |
| 222 | + when-expr:nested-when-expression |
| 223 | + #:when (or (>= (attribute when-expr.depth) 3) |
| 224 | + (>= (length (attribute when-expr.condition)) 3) |
| 225 | + (and (equal? (attribute when-expr.depth) 2) |
| 226 | + (andmap identifier? (attribute when-expr.condition)))) |
| 227 | + (when (and when-expr.condition ...) when-expr.body ...)) |
268 | 228 |
|
269 | 229 |
|
270 | 230 | (define-refactoring-rule ignored-and-to-when |
|
0 commit comments