diff --git a/default-recommendations/hash-shortcuts-test.rkt b/default-recommendations/hash-shortcuts-test.rkt index bdefad2..40eab78 100644 --- a/default-recommendations/hash-shortcuts-test.rkt +++ b/default-recommendations/hash-shortcuts-test.rkt @@ -295,3 +295,63 @@ no-change-test: (define sum (hash-ref! h1 (cadr term) 0)) (hash-set! h2 (cadr term) (+ (car term) sum))) ------------------------------ + + +test: "make-immutable-hash with quasiquoted pairs can be simplified to hash" +------------------------------ +(define body 'test-body) +(define event 'test-event) +(define comments '(c1 c2)) +(make-immutable-hash + `((body . ,body) + (event . ,event) + (comments . ,(map values comments)))) +============================== +(define body 'test-body) +(define event 'test-event) +(define comments '(c1 c2)) +(hash 'body body 'event event 'comments (map values comments)) +------------------------------ + + +test: "make-immutable-hash with simple quasiquoted pairs can be simplified to hash" +------------------------------ +(define x 1) +(define y 2) +(make-immutable-hash `((a . ,x) (b . ,y))) +============================== +(define x 1) +(define y 2) +(hash 'a x 'b y) +------------------------------ + + +test: "make-immutable-hash with single pair can be simplified to hash" +------------------------------ +(define value 42) +(make-immutable-hash `((key . ,value))) +============================== +(define value 42) +(hash 'key value) +------------------------------ + + +no-change-test: "make-immutable-hash without quasiquote should not be changed" +------------------------------ +(make-immutable-hash '((a . 1) (b . 2))) +------------------------------ + + +no-change-test: "make-immutable-hash with variable keys should not be changed" +------------------------------ +(define k 'key) +(define v 'value) +(make-immutable-hash `((,k . ,v))) +------------------------------ + + +no-change-test: "make-immutable-hash with list literal should not be changed" +------------------------------ +(define pairs '((a . 1) (b . 2))) +(make-immutable-hash pairs) +------------------------------ diff --git a/default-recommendations/hash-shortcuts.rkt b/default-recommendations/hash-shortcuts.rkt index 797669e..eea2e8e 100644 --- a/default-recommendations/hash-shortcuts.rkt +++ b/default-recommendations/hash-shortcuts.rkt @@ -159,6 +159,19 @@ (hash-values h)) +(define-syntax-class hash-pair-with-quoted-key + #:attributes (key value) + #:literals (unquote) + (pattern (key:id unquote value))) + + +(define-refactoring-rule make-immutable-hash-with-quasiquote-to-hash + #:description "This `make-immutable-hash` with quasiquoted pairs can be replaced with a simpler `hash` call." + #:literals (make-immutable-hash quasiquote) + (make-immutable-hash (quasiquote (pair:hash-pair-with-quoted-key ...))) + (hash (~@ 'pair.key pair.value) ...)) + + (define-refactoring-suite hash-shortcuts #:rules (define-hash-ref-set!-to-hash-update! hash-map-to-hash-keys @@ -169,4 +182,5 @@ hash-ref-with-constant-lambda-to-hash-ref-without-lambda hash-ref!-with-constant-lambda-to-hash-ref!-without-lambda hash-set!-ref-to-hash-update! + make-immutable-hash-with-quasiquote-to-hash or-hash-ref-set!-to-hash-ref!))