From a16747f1daf94363af07b32af80fb984113723a0 Mon Sep 17 00:00:00 2001 From: Jack Firth Date: Sun, 24 Nov 2024 14:44:16 -0800 Subject: [PATCH] Add `instantiate` shortcuts Closes #418. --- .../class-shortcuts-test.rkt | 29 +++++++++++++++++++ default-recommendations/class-shortcuts.rkt | 20 ++++++++++++- 2 files changed, 48 insertions(+), 1 deletion(-) diff --git a/default-recommendations/class-shortcuts-test.rkt b/default-recommendations/class-shortcuts-test.rkt index 6e41a07f..07579778 100644 --- a/default-recommendations/class-shortcuts-test.rkt +++ b/default-recommendations/class-shortcuts-test.rkt @@ -27,3 +27,32 @@ test: "two-method nested send expression not refactorable to send+" (define (f obj x y) (send (send obj m1 x) m2 y)) -------------------- + + +test: "instantiate without by-name arguments refactorable to make-object" +-------------------- +(define (f cls x y z) + (instantiate cls (x y z))) +-------------------- +-------------------- +(define (f cls x y z) + (make-object cls x y z)) +-------------------- + + +test: "instantiate without by-position arguments refactorable to new" +-------------------- +(define (f cls x y z) + (instantiate cls () [x x] [y y] [z z])) +-------------------- +-------------------- +(define (f cls x y z) + (new cls [x x] [y y] [z z])) +-------------------- + + +test: "instantiate without any arguments not refactorable" +-------------------- +(define (f cls) + (instantiate cls ())) +-------------------- diff --git a/default-recommendations/class-shortcuts.rkt b/default-recommendations/class-shortcuts.rkt index 8af7bb84..063c9022 100644 --- a/default-recommendations/class-shortcuts.rkt +++ b/default-recommendations/class-shortcuts.rkt @@ -42,5 +42,23 @@ (send+ chain.initial-object (chain.method chain.arg ...) ...)) +(define-refactoring-rule instantiate-to-make-object + #:description "The `instantiate` form is for mixing positional and by-name constructor arguments.\ + When no by-name arguments are needed, use `make-object` instead." + #:literals (instantiate) + (instantiate cls (by-position-arg ...+)) + (make-object cls by-position-arg ...)) + + +(define-refactoring-rule instantiate-to-new + #:description "The `instantiate` form is for mixing positional and by-name constructor arguments.\ + When no positional arguments are needed, use `new` instead." + #:literals (instantiate) + (instantiate cls () by-name-arg ...+) + (new cls by-name-arg ...)) + + (define-refactoring-suite class-shortcuts - #:rules (send-chain-to-send+)) + #:rules (instantiate-to-make-object + instantiate-to-new + send-chain-to-send+))