Skip to content

Commit

Permalink
Add provide-deduplication rule
Browse files Browse the repository at this point in the history
Closes #28. The implementation is pretty basic and ignores things like `(provide (for-syntax x x))` but it catches the simple case.
  • Loading branch information
jackfirth committed Sep 12, 2024
1 parent f3fcf5a commit 7646d4b
Show file tree
Hide file tree
Showing 3 changed files with 102 additions and 0 deletions.
3 changes: 3 additions & 0 deletions default-recommendations.rkt
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
resyntax/default-recommendations/match-shortcuts
resyntax/default-recommendations/miscellaneous-suggestions
resyntax/default-recommendations/numeric-shortcuts
resyntax/default-recommendations/require-and-provide-suggestions
resyntax/default-recommendations/string-shortcuts
resyntax/default-recommendations/syntax-shortcuts
resyntax/default-recommendations/syntax-parse-shortcuts
Expand All @@ -48,6 +49,7 @@
resyntax/default-recommendations/match-shortcuts
resyntax/default-recommendations/miscellaneous-suggestions
resyntax/default-recommendations/numeric-shortcuts
resyntax/default-recommendations/require-and-provide-suggestions
resyntax/default-recommendations/string-shortcuts
resyntax/default-recommendations/syntax-shortcuts
resyntax/default-recommendations/syntax-parse-shortcuts
Expand Down Expand Up @@ -86,6 +88,7 @@
(refactoring-suite-rules match-shortcuts)
(refactoring-suite-rules miscellaneous-suggestions)
(refactoring-suite-rules numeric-shortcuts)
(refactoring-suite-rules require-and-provide-suggestions)
(refactoring-suite-rules string-shortcuts)
(refactoring-suite-rules syntax-shortcuts)
(refactoring-suite-rules syntax-parse-shortcuts)
Expand Down
56 changes: 56 additions & 0 deletions default-recommendations/require-and-provide-suggestions-test.rkt
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
#lang resyntax/testing/refactoring-test


require: resyntax/default-recommendations require-and-provide-suggestions


header:
- #lang racket/base


test: "duplicate provided identifiers should be removed"
----------------------------------------
(provide foo
foo
foo)
(define foo 1)
----------------------------------------
----------------------------------------
(provide foo)
(define foo 1)
----------------------------------------


test: "removing duplicate provided identifiers leaves other exports unchanged"
----------------------------------------
(provide a
foo
b
foo
c)
(define a 1)
(define b 1)
(define c 1)
(define foo 1)
----------------------------------------
----------------------------------------
(provide a
foo
b
c)
(define a 1)
(define b 1)
(define c 1)
(define foo 1)
----------------------------------------


test: "provide deduplication doesn't affect exports at different phases"
----------------------------------------
(provide foo
(for-syntax foo))
(require (for-syntax racket/base))
(define foo 1)
(begin-for-syntax
(define foo 2))
----------------------------------------
43 changes: 43 additions & 0 deletions default-recommendations/require-and-provide-suggestions.rkt
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#lang racket/base


(require racket/contract/base)


(provide
(contract-out
[require-and-provide-suggestions refactoring-suite?]))


(require racket/list
rebellion/private/static-name
resyntax/refactoring-rule
resyntax/refactoring-suite
syntax/parse)


;@----------------------------------------------------------------------------------------------------


(define-syntax-class export-spec
#:attributes (id)
(pattern id:id))


(define-refactoring-rule provide-deduplication
#:description "Providing the same identifier multiple times is unnecessary."
#:literals (provide)
(provide spec:export-spec ...)
#:when (check-duplicate-identifier (attribute spec.id))

#:with (deduped-spec ...)
(remove-duplicates (attribute spec) bound-identifier=?
#:key (λ (spec-stx) (syntax-parse spec-stx [:export-spec #'id])))

(provide deduped-spec ...))


(define require-and-provide-suggestions
(refactoring-suite
#:name (name require-and-provide-suggestions)
#:rules (list provide-deduplication)))

0 comments on commit 7646d4b

Please sign in to comment.