Skip to content

Commit

Permalink
Rewrite send chains to send+
Browse files Browse the repository at this point in the history
Closes #390.
  • Loading branch information
jackfirth committed Nov 20, 2024
1 parent 3853e6d commit 97d43e8
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 0 deletions.
3 changes: 3 additions & 0 deletions default-recommendations.rkt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

(provide
(all-from-out resyntax/default-recommendations/boolean-shortcuts
resyntax/default-recommendations/class-shortcuts
resyntax/default-recommendations/comparison-shortcuts
resyntax/default-recommendations/conditional-shortcuts
resyntax/default-recommendations/console-io-suggestions
Expand Down Expand Up @@ -37,6 +38,7 @@
(require rebellion/private/static-name
resyntax/base
resyntax/default-recommendations/boolean-shortcuts
resyntax/default-recommendations/class-shortcuts
resyntax/default-recommendations/comparison-shortcuts
resyntax/default-recommendations/conditional-shortcuts
resyntax/default-recommendations/console-io-suggestions
Expand Down Expand Up @@ -68,6 +70,7 @@

(define-refactoring-suite default-recommendations
#:suites (boolean-shortcuts
class-shortcuts
comparison-shortcuts
conditional-shortcuts
console-io-suggestions
Expand Down
29 changes: 29 additions & 0 deletions default-recommendations/class-shortcuts-test.rkt
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#lang resyntax/test


require: resyntax/default-recommendations class-shortcuts


header:
--------------------
#lang racket/base
(require racket/class)
--------------------


test: "nested send expressions refactorable to flat send+ expression"
--------------------
(define (f obj x y z)
(send (send (send obj m1 x) m2 y) m3 z))
--------------------
--------------------
(define (f obj x y z)
(send+ obj (m1 x) (m2 y) (m3 z)))
--------------------


test: "two-method nested send expression not refactorable to send+"
--------------------
(define (f obj x y)
(send (send obj m1 x) m2 y))
--------------------
46 changes: 46 additions & 0 deletions default-recommendations/class-shortcuts.rkt
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
#lang racket/base


(require racket/contract/base)


(provide
(contract-out
[class-shortcuts refactoring-suite?]))


(require racket/class
resyntax/base
syntax/parse)


;@----------------------------------------------------------------------------------------------------


(define-syntax-class manual-method-chain
#:attributes (initial-object [method 1] [arg 2])
#:literals (send)

(pattern (send inner-chain:manual-method-chain last-method:id last-method-arg ...)
#:attr initial-object (attribute inner-chain.initial-object)
#:attr [method 1] (append (attribute inner-chain.method) (list #'last-method))
#:attr [arg 2] (append (attribute inner-chain.arg) (list (attribute last-method-arg))))

(pattern (send (~and initial-object (~not _:manual-method-chain))
first-method:id
first-method-arg ...)
#:attr [method 1] (list #'first-method)
#:attr [arg 2] (list (attribute first-method-arg))))


(define-refactoring-rule send-chain-to-send+
#:description
"This method chain made of nested `send` expressions can be written more clearly as a `send+`\
expression."
chain:manual-method-chain
#:when (>= (length (attribute chain.method)) 3)
(send+ chain.initial-object (chain.method chain.arg ...) ...))


(define-refactoring-suite class-shortcuts
#:rules (send-chain-to-send+))

0 comments on commit 97d43e8

Please sign in to comment.