Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add cond-void-to-when-or-unless #423

Merged
merged 1 commit into from
Nov 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 54 additions & 0 deletions default-recommendations/conditional-shortcuts-test.rkt
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,60 @@ test: "if expressions can be refactored to unless expressions when equivalent"
------------------------------


test: "cond expressions can be refactored to when expressions when equivalent"
------------------------------
(cond
[#true
(begin
(println "first line")
;; preserved comment
(println "second line"))]
[else (void)])
------------------------------
------------------------------
(cond
[(not #true) (void)]
[else
(begin
(println "first line")
;; preserved comment
(println "second line"))])
------------------------------
------------------------------
(when #true
(println "first line")
;; preserved comment
(println "second line"))
------------------------------


test: "cond expressions can be refactored to unless expressions when equivalent"
------------------------------
(cond
[#false (void)]
[else
(begin
(println "first line")
;; preserved comment
(println "second line"))])
------------------------------
------------------------------
(cond
[(not #false)
(begin
(println "first line")
;; preserved comment
(println "second line"))]
[else (void)])
------------------------------
------------------------------
(unless #false
(println "first line")
;; preserved comment
(println "second line"))
------------------------------


test: "if expressions with an always-throwing first branch can be refactored to when"
------------------------------
(define (f c)
Expand Down
21 changes: 19 additions & 2 deletions default-recommendations/conditional-shortcuts.rkt
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@
(pattern single-body #:with (body ...) #'(single-body)))


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

Expand All @@ -81,7 +81,23 @@

(define-refactoring-rule if-void-to-when-or-unless
#:description equivalent-conditional-description
conditional:when-or-unless-equivalent-conditional
conditional:when-or-unless-equivalent-if-expression
((~if conditional.negated? unless when) conditional.condition conditional.body ...))


(define-syntax-class when-or-unless-equivalent-cond-expression
#:attributes (negated? condition [body 1])
#:literals (cond void not begin let)

(pattern (cond [(not condition) (void)] [else :block-expression]) #:with negated? #false)
(pattern (cond [(not condition) :block-expression] [else (void)]) #:with negated? #true)
(pattern (cond [condition (void)] [else :block-expression]) #:with negated? #true)
(pattern (cond [condition :block-expression] [else (void)]) #:with negated? #false))


(define-refactoring-rule cond-void-to-when-or-unless
#:description equivalent-conditional-description
conditional:when-or-unless-equivalent-cond-expression
((~if conditional.negated? unless when) conditional.condition conditional.body ...))


Expand Down Expand Up @@ -208,6 +224,7 @@
always-throwing-if-to-when
cond-else-cond-to-cond
cond-let-to-cond-define
cond-void-to-when-or-unless
if-begin-to-cond
if-else-false-to-and
if-let-to-cond
Expand Down
Loading