Skip to content

Commit

Permalink
Add begin splicing rules
Browse files Browse the repository at this point in the history
Closes #375.
  • Loading branch information
jackfirth committed Nov 20, 2024
1 parent 0cd2b7a commit 7acf8c7
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 1 deletion.
28 changes: 28 additions & 0 deletions default-recommendations/definition-shortcuts-test.rkt
Original file line number Diff line number Diff line change
Expand Up @@ -192,3 +192,31 @@ test: "inlining immediately returned variable definition respects requested line
(define x 1)
x)
------------------------------


test: "begin in right hand side of definition can be removed"
--------------------
(define (foo)
(define x (begin (displayln "foo") 42))
(* x 2))
--------------------
--------------------
(define (foo)
(displayln "foo")
(define x 42)
(* x 2))
--------------------


test: "begin0 in right hand side of definition can be removed"
--------------------
(define (foo)
(define x (begin0 42 (displayln "foo")))
(* x 2))
--------------------
--------------------
(define (foo)
(define x 42)
(displayln "foo")
(* x 2))
--------------------
30 changes: 29 additions & 1 deletion default-recommendations/definition-shortcuts.rkt
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,34 @@
(body-before ... focused))


(define-definition-context-refactoring-rule define-begin-extraction
#:description
"The `begin` in this definition can be extracted into the surrounding definition context."
#:literals (define begin)
(~seq body-before ...
(~and definition (define id (begin pre-body ... expr)))
body-after ...)
#:with (replacement ...)
#'(~focus-replacement-on
(~splicing-replacement (pre-body ... (define id expr)) #:original definition))
(body-before ... replacement ... body-after ...))


(define-definition-context-refactoring-rule define-begin0-extraction
#:description
"The `begin0` in this definition can be extracted into the surrounding definition context."
#:literals (define begin0)
(~seq body-before ...
(~and definition (define id (begin0 expr post-body ...)))
body-after ...)
#:with (replacement ...)
#'(~focus-replacement-on
(~splicing-replacement ((define id expr) post-body ...) #:original definition))
(body-before ... replacement ... body-after ...))


(define-refactoring-suite definition-shortcuts
#:rules (define-values-values-to-define
#:rules (define-begin-extraction
define-begin0-extraction
define-values-values-to-define
inline-unnecessary-define))

0 comments on commit 7acf8c7

Please sign in to comment.