Skip to content

Commit 151ef6e

Browse files
authored
Improve throw-unless-truthy-to-or formatting (#463)
1 parent a6045ab commit 151ef6e

File tree

2 files changed

+45
-3
lines changed

2 files changed

+45
-3
lines changed

default-recommendations/conditional-shortcuts-test.rkt

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -427,6 +427,41 @@ test: "always-throwing unless before returning condition variable refactorable t
427427
--------------------
428428

429429

430+
test: "refactoring always-throwing unless to or doesn't reformat surrounding context"
431+
--------------------
432+
(define (f s)
433+
434+
( define foo 42 )
435+
436+
(define x (string->number s))
437+
(unless x
438+
(error "string that is not a num"))
439+
x)
440+
--------------------
441+
--------------------
442+
(define (f s)
443+
444+
( define foo 42 )
445+
446+
(or (string->number s) (error "string that is not a num")))
447+
--------------------
448+
449+
450+
test: "refactoring always-throwing unless to or can reformat when its the only body"
451+
--------------------
452+
(define (f s)
453+
(λ (v)
454+
(define x (string->number s))
455+
(unless x
456+
(error "string that is not a num"))
457+
x))
458+
--------------------
459+
--------------------
460+
(define (f s)
461+
(λ (v) (or (string->number s) (error "string that is not a num"))))
462+
--------------------
463+
464+
430465
test: "cond with nested else-cond can be flattened"
431466
------------------------------
432467
(define (f a b)

default-recommendations/conditional-shortcuts.rkt

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111

1212
(require (for-syntax racket/base)
13+
racket/list
1314
resyntax/base
1415
resyntax/default-recommendations/private/boolean
1516
resyntax/default-recommendations/private/exception
@@ -156,11 +157,17 @@
156157
(~and def (define truthy-var:id truthy-expr))
157158
(~and check (unless truthy-var2:id fail:always-throwing-expression))
158159
truthy-var3:id)
160+
159161
#:when (free-identifier=? #'truthy-var #'truthy-var2)
160162
#:when (free-identifier=? #'truthy-var #'truthy-var3)
161-
(body-before ...
162-
(~focus-replacement-on
163-
(~replacement (or truthy-expr fail) #:original-splice (def check truthy-var3)))))
163+
#:with or-expression
164+
#'(~replacement (or truthy-expr fail) #:original-splice (def check truthy-var3))
165+
#:with focused-or-expression
166+
(if (empty? (attribute body-before))
167+
#'or-expression
168+
#'(~focus-replacement-on or-expression))
169+
170+
(body-before ... focused-or-expression))
164171

165172

166173
(define-refactoring-rule cond-else-cond-to-cond

0 commit comments

Comments
 (0)