Skip to content

Commit 611c436

Browse files
Copilotjackfirth
andcommitted
Move rules from miscellaneous-suggestions to appropriate suites with tests
Co-authored-by: jackfirth <[email protected]>
1 parent 383b739 commit 611c436

File tree

4 files changed

+116
-1
lines changed

4 files changed

+116
-1
lines changed

default-recommendations/conditional-shortcuts-test.rkt

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -828,3 +828,49 @@ no-change-test: "cond with unshared tail expression not refactorable to when"
828828
[else
829829
(displayln "false tail")]))
830830
--------------------
831+
832+
833+
test: "if with else cond can be flattened to cond"
834+
- (if 'a 'b (cond ['c 'd] ['e 'f]))
835+
------------------------------
836+
(cond
837+
['a 'b]
838+
['c 'd]
839+
['e 'f])
840+
------------------------------
841+
842+
843+
test: "cond with else-if can be collapsed"
844+
- (cond ['a 'b] ['c 'd] [else (if 'e 'f 'g)])
845+
------------------------------
846+
(cond
847+
['a 'b]
848+
['c 'd]
849+
['e 'f]
850+
[else 'g])
851+
------------------------------
852+
853+
854+
test: "cond with begin in clause can be simplified"
855+
------------------------------
856+
(cond ['a (begin 'b 'c 'd)])
857+
==============================
858+
(cond
859+
['a
860+
'b
861+
'c
862+
'd])
863+
------------------------------
864+
865+
866+
test: "cond with begin in middle clause can be simplified"
867+
------------------------------
868+
(cond ['a 'b] ['c (begin 'd 'e)] ['f 'g])
869+
==============================
870+
(cond
871+
['a 'b]
872+
['c
873+
'd
874+
'e]
875+
['f 'g])
876+
------------------------------

default-recommendations/conditional-shortcuts.rkt

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,13 +236,42 @@ tail expression outside `cond` lets you replace `cond` with `when`."
236236
body-after ...))
237237

238238

239+
(define if-begin-to-cond-message
240+
"The `cond` form supports multiple body expressions in each branch, making `begin` unnecessary.")
241+
242+
243+
(define-refactoring-rule if-else-cond-to-cond
244+
#:description if-begin-to-cond-message
245+
#:literals (if cond)
246+
(if condition then-branch (cond clause ...))
247+
(cond [condition then-branch] clause ...))
248+
249+
250+
(define-refactoring-rule cond-else-if-to-cond
251+
#:description "The `else`-`if` branch of this `cond` expression can be collapsed into the `cond`\
252+
expression."
253+
#:literals (cond else if)
254+
(cond clause ... [else (if inner-condition inner-then-branch else-branch)])
255+
(cond clause ... [inner-condition inner-then-branch] [else else-branch]))
256+
257+
258+
(define-refactoring-rule cond-begin-to-cond
259+
#:description "The bodies of `cond` clauses are already implicitly wrapped in `begin`."
260+
#:literals (cond begin)
261+
(cond clause-before ... [condition (begin body ...)] clause-after ...)
262+
(cond clause-before ... [condition body ...] clause-after ...))
263+
264+
239265
(define-refactoring-suite conditional-shortcuts
240266
#:rules (always-throwing-cond-to-when
241267
always-throwing-if-to-when
242268
cond-else-cond-to-cond
269+
cond-else-if-to-cond
243270
cond-void-to-when-or-unless
271+
cond-begin-to-cond
244272
explicit-cond-else-void
245273
if-begin-to-cond
274+
if-else-cond-to-cond
246275
if-else-false-to-and
247276
if-void-to-when-or-unless
248277
if-x-else-x-to-and

default-recommendations/match-shortcuts-test.rkt

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -474,4 +474,34 @@ test: "refactoring list element variable definitions to match-define doesn't ref
474474
------------------------------
475475

476476

477+
test: "and with match on same identifier can be simplified"
478+
------------------------------
479+
(define (f x)
480+
(and x (match x [1 2])))
481+
==============================
482+
(define (f x)
483+
(match x
484+
[#f #f]
485+
[1 2]))
486+
------------------------------
487+
488+
489+
test: "and with match on same identifier preserves formatting"
490+
------------------------------
491+
(define (foo some-var)
492+
(and some-var
493+
(match some-var
494+
['first-case 'first-result]
495+
['second-case 'second-result])))
496+
==============================
497+
(define (foo some-var)
498+
(match some-var
499+
[#f #f]
500+
['first-case 'first-result]
501+
['second-case 'second-result]))
502+
------------------------------
503+
504+
505+
no-change-test: "and with match on different identifiers not refactorable"
506+
- (define (foo x y) (and x (match y ['a 'b] ['c 'd])))
477507

default-recommendations/match-shortcuts.rkt

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -265,8 +265,18 @@ elements than expected."
265265
(body-before ... (~focus-replacement-on match-definition) body-after ...))
266266

267267

268+
(define-refactoring-rule and-match-to-match
269+
#:description "This `and` expression can be turned into a clause of the inner `match` expression,\
270+
reducing nesting."
271+
#:literals (and match)
272+
(and and-subject:id (match match-subject:id match-clause ...))
273+
#:when (free-identifier=? #'and-subject #'match-subject)
274+
(match match-subject [#false #false] match-clause ...))
275+
276+
268277
(define-refactoring-suite match-shortcuts
269-
#:rules (list-element-definitions-to-match-define
278+
#:rules (and-match-to-match
279+
list-element-definitions-to-match-define
270280
match-conditional-to-when
271281
predicate-pattern-with-lambda-to-when
272282
remove-unnecessary-root-and-pattern

0 commit comments

Comments
 (0)