Skip to content

Commit ddee7d5

Browse files
Copilotjackfirth
andauthored
Reduce aggressiveness of nested when merging rule (#705)
Co-authored-by: copilot-swe-agent[bot] <[email protected]> Co-authored-by: jackfirth <[email protected]> Co-authored-by: Jack Firth <[email protected]>
1 parent 85902ad commit ddee7d5

File tree

2 files changed

+63
-12
lines changed

2 files changed

+63
-12
lines changed

default-recommendations/conditional-shortcuts-test.rkt

Lines changed: 45 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -698,20 +698,56 @@ test: "if clause with begin in false branch and commented true branch refactorab
698698
------------------------------
699699

700700

701-
test: "immediately-nested when expressions can be merged"
701+
no-change-test: "two when forms with non-trivial conditions"
702702
--------------------
703-
(define (f c1 c2)
704-
(when c1
705-
(when c2
706-
(displayln "both passed"))))
703+
(define (f a b c)
704+
(when (a)
705+
(when (b)
706+
c)))
707+
--------------------
708+
709+
710+
test: "three when forms"
711+
--------------------
712+
(define (f a b c d)
713+
(when (a)
714+
(when (b)
715+
(when (c)
716+
d))))
707717
====================
708-
(define (f c1 c2)
709-
(when (and c1 c2)
710-
(displayln "both passed")))
718+
(define (f a b c d)
719+
(when (and (a) (b) (c))
720+
d))
721+
--------------------
722+
723+
724+
test: "two when forms with and"
725+
--------------------
726+
(define (f a b c d)
727+
(when (and (a) (b))
728+
(when (c)
729+
d)))
730+
====================
731+
(define (f a b c d)
732+
(when (and (a) (b) (c))
733+
d))
734+
--------------------
735+
736+
737+
test: "two when forms with identifiers"
738+
--------------------
739+
(define (f a b c)
740+
(when a
741+
(when b
742+
c)))
743+
====================
744+
(define (f a b c)
745+
(when (and a b)
746+
c))
711747
--------------------
712748

713749

714-
test: "nested when with multiple body expressions can be merged"
750+
test: "two when forms with identifiers and multiple body expressions"
715751
--------------------
716752
(define (f c1 c2)
717753
(when c1

default-recommendations/conditional-shortcuts.rkt

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -204,12 +204,27 @@
204204
(~replacement [else else-expr.refactored ...] #:original else-expr)))
205205

206206

207+
(define-syntax-class nested-when-expression
208+
#:attributes ([condition 1] [body 1] depth)
209+
#:literals (when and)
210+
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))))
217+
218+
207219
(define-refactoring-rule nested-when-to-compound-when
208220
#:description
209221
"Nested `when` expressions can be merged into a single compound `when` expression."
210-
#:literals (when and)
211-
(when outer-condition:expr (when inner-condition:expr body:expr ...))
212-
(when (and outer-condition inner-condition) 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 ...))
213228

214229

215230
(define-refactoring-rule ignored-and-to-when

0 commit comments

Comments
 (0)