diff --git a/default-recommendations/conditional-shortcuts-test.rkt b/default-recommendations/conditional-shortcuts-test.rkt index 5343701d..40dbc24e 100644 --- a/default-recommendations/conditional-shortcuts-test.rkt +++ b/default-recommendations/conditional-shortcuts-test.rkt @@ -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) diff --git a/default-recommendations/conditional-shortcuts.rkt b/default-recommendations/conditional-shortcuts.rkt index 66cf62a3..644a673a 100644 --- a/default-recommendations/conditional-shortcuts.rkt +++ b/default-recommendations/conditional-shortcuts.rkt @@ -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) @@ -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 ...)) @@ -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