Skip to content

Commit 90130c1

Browse files
Copilotjackfirth
andauthored
Handle multiple variables in define-let-to-multi-define rule (#706)
Co-authored-by: copilot-swe-agent[bot] <[email protected]> Co-authored-by: jackfirth <[email protected]>
1 parent ddee7d5 commit 90130c1

File tree

2 files changed

+64
-11
lines changed

2 files changed

+64
-11
lines changed

default-recommendations/let-replacement/let-replacement-test.rkt

Lines changed: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -597,6 +597,57 @@ no-change-test:
597597
------------------------------
598598

599599

600+
test: "variable definition with nested let binding with multiple bindings refactorable"
601+
------------------------------
602+
(define (f)
603+
(displayln "foo")
604+
(define a (let ([b 1] [c 2]) (+ b c 10)))
605+
(* a 3))
606+
==============================
607+
(define (f)
608+
(displayln "foo")
609+
(define b 1)
610+
(define c 2)
611+
(define a (+ b c 10))
612+
(* a 3))
613+
------------------------------
614+
615+
616+
test: "variable definition with nested let binding with three bindings refactorable"
617+
------------------------------
618+
(define (f)
619+
(define a (let ([b 1] [c 2] [d 3]) (+ b c d)))
620+
(* a 10))
621+
==============================
622+
(define (f)
623+
(define b 1)
624+
(define c 2)
625+
(define d 3)
626+
(define a (+ b c d))
627+
(* a 10))
628+
------------------------------
629+
630+
631+
no-change-test:
632+
"variable definition with nested let binding with multiple bindings where one conflicts not refactorable"
633+
------------------------------
634+
(define (f)
635+
(define x 5)
636+
(define a (let ([b 1] [x 2]) (+ b x 10)))
637+
(* a 3))
638+
------------------------------
639+
640+
641+
no-change-test:
642+
"variable definition with nested let binding with multiple bindings where one shadows outer binding not refactorable"
643+
------------------------------
644+
(define x 5)
645+
(define (f)
646+
(define a (let ([x 1] [c 2]) (+ x c 10)))
647+
(* x a))
648+
------------------------------
649+
650+
600651
test: "let binding nested in begin0 extractable to definition"
601652
------------------------------
602653
(define (f)
@@ -640,7 +691,7 @@ test: "let-to-define doesn't reformat the entire definition context"
640691
----------------------------------------
641692

642693

643-
test: "define-let-to-double-define doesn't reformat the entire definition context"
694+
test: "define-let-to-multi-define doesn't reformat the entire definition context"
644695
----------------------------------------
645696
(define (f)
646697
( displayln "foo" )

default-recommendations/let-replacement/let-replacement.rkt

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -36,20 +36,22 @@
3636
(leading-body ... replacement ...))
3737

3838

39-
(define-definition-context-refactoring-rule define-let-to-double-define
40-
#:description "This `let` expression can be pulled up into a `define` expression."
39+
(define-definition-context-refactoring-rule define-let-to-multi-define
40+
#:description "This `let` expression can be pulled up into multiple `define` expressions."
4141
#:literals (define let)
4242
(~seq body-before ...
43-
(~and original-definition (define id:id (let ([nested-id:id nested-expr:expr]) expr:expr)))
43+
(~and original-definition (define id:id (let ([nested-id:id nested-expr:expr] ...) expr:expr)))
4444
body-after ...)
45-
#:when (identifier-binding-unchanged-in-context? (attribute id) (attribute nested-expr))
46-
#:when (for/and ([body-free-id
47-
(in-free-id-set
48-
(syntax-free-identifiers #'(body-before ... nested-expr body-after ...)))])
49-
(identifier-binding-unchanged-in-context? body-free-id (attribute nested-id)))
45+
#:when (for/and ([nested-expr (in-list (attribute nested-expr))])
46+
(identifier-binding-unchanged-in-context? (attribute id) nested-expr))
47+
#:when (for*/and ([body-free-id
48+
(in-free-id-set
49+
(syntax-free-identifiers #'(body-before ... (nested-expr ...) body-after ...)))]
50+
[nested-id (in-list (attribute nested-id))])
51+
(identifier-binding-unchanged-in-context? body-free-id nested-id))
5052
(body-before ...
5153
(~@ . (~focus-replacement-on
52-
(~splicing-replacement ((define nested-id nested-expr) (define id expr))
54+
(~splicing-replacement ((define nested-id nested-expr) ... (define id expr))
5355
#:original original-definition)))
5456
body-after ...))
5557

@@ -73,5 +75,5 @@
7375

7476
(define-refactoring-suite let-replacement
7577
#:rules (let-to-define
76-
define-let-to-double-define
78+
define-let-to-multi-define
7779
begin0-let-to-define-begin0))

0 commit comments

Comments
 (0)