Skip to content

Commit a7ee08f

Browse files
Copilotjackfirth
andcommitted
Add refactoring rule to suggest hash instead of make-immutable-hash with quasiquote
Co-authored-by: jackfirth <[email protected]>
1 parent 0d00b87 commit a7ee08f

File tree

2 files changed

+80
-0
lines changed

2 files changed

+80
-0
lines changed

default-recommendations/hash-shortcuts-test.rkt

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -295,3 +295,63 @@ no-change-test:
295295
(define sum (hash-ref! h1 (cadr term) 0))
296296
(hash-set! h2 (cadr term) (+ (car term) sum)))
297297
------------------------------
298+
299+
300+
test: "make-immutable-hash with quasiquoted pairs can be simplified to hash"
301+
------------------------------
302+
(define body 'test-body)
303+
(define event 'test-event)
304+
(define comments '(c1 c2))
305+
(make-immutable-hash
306+
`((body . ,body)
307+
(event . ,event)
308+
(comments . ,(map values comments))))
309+
==============================
310+
(define body 'test-body)
311+
(define event 'test-event)
312+
(define comments '(c1 c2))
313+
(hash 'body body 'event event 'comments (map values comments))
314+
------------------------------
315+
316+
317+
test: "make-immutable-hash with simple quasiquoted pairs can be simplified to hash"
318+
------------------------------
319+
(define x 1)
320+
(define y 2)
321+
(make-immutable-hash `((a . ,x) (b . ,y)))
322+
==============================
323+
(define x 1)
324+
(define y 2)
325+
(hash 'a x 'b y)
326+
------------------------------
327+
328+
329+
test: "make-immutable-hash with single pair can be simplified to hash"
330+
------------------------------
331+
(define value 42)
332+
(make-immutable-hash `((key . ,value)))
333+
==============================
334+
(define value 42)
335+
(hash 'key value)
336+
------------------------------
337+
338+
339+
no-change-test: "make-immutable-hash without quasiquote should not be changed"
340+
------------------------------
341+
(make-immutable-hash '((a . 1) (b . 2)))
342+
------------------------------
343+
344+
345+
no-change-test: "make-immutable-hash with variable keys should not be changed"
346+
------------------------------
347+
(define k 'key)
348+
(define v 'value)
349+
(make-immutable-hash `((,k . ,v)))
350+
------------------------------
351+
352+
353+
no-change-test: "make-immutable-hash with list literal should not be changed"
354+
------------------------------
355+
(define pairs '((a . 1) (b . 2)))
356+
(make-immutable-hash pairs)
357+
------------------------------

default-recommendations/hash-shortcuts.rkt

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,25 @@
159159
(hash-values h))
160160

161161

162+
(define-syntax-class quoted-key
163+
#:attributes (key)
164+
#:literals (quote)
165+
(pattern (quote key:id)))
166+
167+
168+
(define-syntax-class hash-pair-with-quoted-key
169+
#:attributes (key value)
170+
#:literals (unquote)
171+
(pattern (key:id unquote value)))
172+
173+
174+
(define-refactoring-rule make-immutable-hash-with-quasiquote-to-hash
175+
#:description "This `make-immutable-hash` with quasiquoted pairs can be replaced with a simpler `hash` call."
176+
#:literals (make-immutable-hash quasiquote)
177+
(make-immutable-hash (quasiquote (pair:hash-pair-with-quoted-key ...)))
178+
(hash (~@ 'pair.key pair.value) ...))
179+
180+
162181
(define-refactoring-suite hash-shortcuts
163182
#:rules (define-hash-ref-set!-to-hash-update!
164183
hash-map-to-hash-keys
@@ -169,4 +188,5 @@
169188
hash-ref-with-constant-lambda-to-hash-ref-without-lambda
170189
hash-ref!-with-constant-lambda-to-hash-ref!-without-lambda
171190
hash-set!-ref-to-hash-update!
191+
make-immutable-hash-with-quasiquote-to-hash
172192
or-hash-ref-set!-to-hash-ref!))

0 commit comments

Comments
 (0)