Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 60 additions & 0 deletions default-recommendations/hash-shortcuts-test.rkt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
------------------------------
14 changes: 14 additions & 0 deletions default-recommendations/hash-shortcuts.rkt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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!))