Skip to content

Commit ea001d9

Browse files
Copilotjackfirth
andcommitted
Modify when/unless rules to only apply with multi-body loops
- Add check to require at least 2 body forms before refactoring - Update test cases to test both multi-body (should refactor) and single-body (should not refactor) cases - All tests pass successfully Co-authored-by: jackfirth <[email protected]>
1 parent 1a8eb17 commit ea001d9

File tree

2 files changed

+54
-12
lines changed

2 files changed

+54
-12
lines changed

default-recommendations/loops/for-loop-shortcuts-test.rkt

Lines changed: 52 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -341,51 +341,91 @@ test: "nested for/and forms can be flattened to a for*/and form"
341341
------------------------------
342342

343343

344-
test: "(when ...) in a for loop refactored to #:when clause"
344+
test: "(when ...) in a for loop refactored to #:when clause when multiple body forms"
345345
------------------------------------------------------------
346346
(for ([x (in-list (list 1 2 'a 3 'b 4))])
347347
(when (number? x)
348-
(displayln x)))
348+
(displayln x)
349+
(displayln (* x 2))))
349350
============================================================
350351
(for ([x (in-list (list 1 2 'a 3 'b 4))]
351352
#:when (number? x))
352-
(displayln x))
353+
(displayln x)
354+
(displayln (* x 2)))
353355
------------------------------------------------------------
354356

355357

356-
test: "(when ...) in a for* loop refactored to #:when clause"
358+
no-change-test: "(when ...) with single body form in a for loop not refactored"
357359
------------------------------------------------------------
358-
(for* ([x (in-list (list 1 2 'a 3 'b 4))])
360+
(for ([x (in-list (list 1 2 'a 3 'b 4))])
359361
(when (number? x)
360362
(displayln x)))
363+
------------------------------------------------------------
364+
365+
366+
test: "(when ...) in a for* loop refactored to #:when clause when multiple body forms"
367+
------------------------------------------------------------
368+
(for* ([x (in-list (list 1 2 'a 3 'b 4))])
369+
(when (number? x)
370+
(displayln x)
371+
(displayln (* x 2))))
361372
============================================================
362373
(for* ([x (in-list (list 1 2 'a 3 'b 4))]
363374
#:when (number? x))
364-
(displayln x))
375+
(displayln x)
376+
(displayln (* x 2)))
377+
------------------------------------------------------------
378+
379+
380+
no-change-test: "(when ...) with single body form in a for* loop not refactored"
381+
------------------------------------------------------------
382+
(for* ([x (in-list (list 1 2 'a 3 'b 4))])
383+
(when (number? x)
384+
(displayln x)))
365385
------------------------------------------------------------
366386

367387

368-
test: "(unless ...) in a for loop refactored to #:when clause"
388+
test: "(unless ...) in a for loop refactored to #:unless clause when multiple body forms"
369389
------------------------------------------------------------
370390
(for ([x (in-list (list 1 2 'a 3 'b 4))])
371391
(unless (number? x)
372-
(displayln x)))
392+
(displayln x)
393+
(displayln "non-number")))
373394
============================================================
374395
(for ([x (in-list (list 1 2 'a 3 'b 4))]
375396
#:unless (number? x))
376-
(displayln x))
397+
(displayln x)
398+
(displayln "non-number"))
377399
------------------------------------------------------------
378400

379401

380-
test: "(unless ...) in a for* loop refactored to #:when clause"
402+
no-change-test: "(unless ...) with single body form in a for loop not refactored"
381403
------------------------------------------------------------
382-
(for* ([x (in-list (list 1 2 'a 3 'b 4))])
404+
(for ([x (in-list (list 1 2 'a 3 'b 4))])
383405
(unless (number? x)
384406
(displayln x)))
407+
------------------------------------------------------------
408+
409+
410+
test: "(unless ...) in a for* loop refactored to #:unless clause when multiple body forms"
411+
------------------------------------------------------------
412+
(for* ([x (in-list (list 1 2 'a 3 'b 4))])
413+
(unless (number? x)
414+
(displayln x)
415+
(displayln "non-number")))
385416
============================================================
386417
(for* ([x (in-list (list 1 2 'a 3 'b 4))]
387418
#:unless (number? x))
388-
(displayln x))
419+
(displayln x)
420+
(displayln "non-number"))
421+
------------------------------------------------------------
422+
423+
424+
no-change-test: "(unless ...) with single body form in a for* loop not refactored"
425+
------------------------------------------------------------
426+
(for* ([x (in-list (list 1 2 'a 3 'b 4))])
427+
(unless (number? x)
428+
(displayln x)))
389429
------------------------------------------------------------
390430

391431

default-recommendations/loops/for-loop-shortcuts.rkt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -261,13 +261,15 @@ return just that result."
261261
#:description "Use the `#:when` keyword instead of `when` to reduce loop body indentation."
262262
#:literals (when for for*)
263263
((~or for-id:for for-id:for*) (clause ...) (when condition body ...))
264+
#:when (>= (length (attribute body)) 2)
264265
(for-id (clause ... #:when condition) body ...))
265266

266267

267268
(define-refactoring-rule unless-expression-in-for-loop-to-unless-keyword
268269
#:description "Use the `#:unless` keyword instead of `unless` to reduce loop body indentation."
269270
#:literals (unless for for*)
270271
((~or for-id:for for-id:for*) (clause ...) (unless condition body ...))
272+
#:when (>= (length (attribute body)) 2)
271273
(for-id (clause ... #:unless condition) body ...))
272274

273275

0 commit comments

Comments
 (0)