Skip to content

Commit 8b791f7

Browse files
committed
Add cond-void-to-when-or-unless
Closes #420.
1 parent f84cfc6 commit 8b791f7

File tree

2 files changed

+73
-2
lines changed

2 files changed

+73
-2
lines changed

default-recommendations/conditional-shortcuts-test.rkt

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,60 @@ test: "if expressions can be refactored to unless expressions when equivalent"
137137
------------------------------
138138

139139

140+
test: "cond expressions can be refactored to when expressions when equivalent"
141+
------------------------------
142+
(cond
143+
[#true
144+
(begin
145+
(println "first line")
146+
;; preserved comment
147+
(println "second line"))]
148+
[else (void)])
149+
------------------------------
150+
------------------------------
151+
(cond
152+
[(not #true) (void)]
153+
[else
154+
(begin
155+
(println "first line")
156+
;; preserved comment
157+
(println "second line"))])
158+
------------------------------
159+
------------------------------
160+
(when #true
161+
(println "first line")
162+
;; preserved comment
163+
(println "second line"))
164+
------------------------------
165+
166+
167+
test: "cond expressions can be refactored to unless expressions when equivalent"
168+
------------------------------
169+
(cond
170+
[#false (void)]
171+
[else
172+
(begin
173+
(println "first line")
174+
;; preserved comment
175+
(println "second line"))])
176+
------------------------------
177+
------------------------------
178+
(cond
179+
[(not #false)
180+
(begin
181+
(println "first line")
182+
;; preserved comment
183+
(println "second line"))]
184+
[else (void)])
185+
------------------------------
186+
------------------------------
187+
(unless #false
188+
(println "first line")
189+
;; preserved comment
190+
(println "second line"))
191+
------------------------------
192+
193+
140194
test: "if expressions with an always-throwing first branch can be refactored to when"
141195
------------------------------
142196
(define (f c)

default-recommendations/conditional-shortcuts.rkt

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@
6969
(pattern single-body #:with (body ...) #'(single-body)))
7070

7171

72-
(define-syntax-class when-or-unless-equivalent-conditional
72+
(define-syntax-class when-or-unless-equivalent-if-expression
7373
#:attributes (negated? condition [body 1])
7474
#:literals (if void not begin let)
7575

@@ -81,7 +81,23 @@
8181

8282
(define-refactoring-rule if-void-to-when-or-unless
8383
#:description equivalent-conditional-description
84-
conditional:when-or-unless-equivalent-conditional
84+
conditional:when-or-unless-equivalent-if-expression
85+
((~if conditional.negated? unless when) conditional.condition conditional.body ...))
86+
87+
88+
(define-syntax-class when-or-unless-equivalent-cond-expression
89+
#:attributes (negated? condition [body 1])
90+
#:literals (cond void not begin let)
91+
92+
(pattern (cond [(not condition) (void)] [else :block-expression]) #:with negated? #false)
93+
(pattern (cond [(not condition) :block-expression] [else (void)]) #:with negated? #true)
94+
(pattern (cond [condition (void)] [else :block-expression]) #:with negated? #true)
95+
(pattern (cond [condition :block-expression] [else (void)]) #:with negated? #false))
96+
97+
98+
(define-refactoring-rule cond-void-to-when-or-unless
99+
#:description equivalent-conditional-description
100+
conditional:when-or-unless-equivalent-cond-expression
85101
((~if conditional.negated? unless when) conditional.condition conditional.body ...))
86102

87103

@@ -208,6 +224,7 @@
208224
always-throwing-if-to-when
209225
cond-else-cond-to-cond
210226
cond-let-to-cond-define
227+
cond-void-to-when-or-unless
211228
if-begin-to-cond
212229
if-else-false-to-and
213230
if-let-to-cond

0 commit comments

Comments
 (0)