Add refactoring rule to suggest hash over make-immutable-hash with quasiquoting
Suggests replacing make-immutable-hash with quasiquoted pairs with the simpler hash constructor when all keys are statically known.
Changes
- Added
make-immutable-hash-with-quasiquote-to-hashrule tohash-shortcuts.rkt - Matches pattern
(make-immutable-hash((key . ,value) ...))` where keys are literal identifiers - Transforms to
(hash 'key value ...)for improved readability - Added 6 test cases covering positive and negative scenarios
Example
;; Before
(make-immutable-hash
`((body . ,body)
(event . ,event)
(comments . ,(map github-review-comment-jsexpr comments))))
;; After
(hash 'body body 'event event 'comments (map github-review-comment-jsexpr comments))
The rule only applies when keys are quoted symbols. Variable keys like (,k . ,v) are correctly excluded.
Original prompt
This section details on the original issue you should resolve
<issue_title>Suggest using
hashinstead ofmake-immutable-hashwith quasiquoting</issue_title> <issue_description>Example:(make-immutable-hash `((body . ,body) (event . ,event) (comments . ,(map github-review-comment-jsexpr comments))))If the keys are statically known,
hashis simpler.The same goes for quasiquoted
#hash()literals, but those are trickier.</issue_description>Comments on the Issue (you are @copilot in this section)
@jackfirth I'd rather read the `(hash 'body body 'event event 'comments (map ...))` expression. Fewer parens, fewer special characters, it's shorter, and it doesn't invite confusion about why this doesn't work:(define x 42) #hash([foo x]) ;; why am I not getting `(hash 'foo 42)`???People ask questions in the racket discord about that kind of quotation issue all the time. I'd rather just avoid it entirely.</comment_new>
- Fixes jackfirth/resyntax#122
💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.