Skip to content

Refactor in-range to in-inclusive-range when possible #376

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
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
11 changes: 11 additions & 0 deletions default-recommendations/for-loop-shortcuts-test.rkt
Original file line number Diff line number Diff line change
Expand Up @@ -788,3 +788,14 @@ test: "(unless ...) in a for* loop refactored to #:when clause"
#:unless (number? x))
(displayln x))
------------------------------------------------------------


test: "in-range with add1 in a for loop can be refactored to in-inclusive-range"
------------------------------------------------------------
(for ([i (in-range 1 (add1 5))])
(displayln i))
------------------------------------------------------------
------------------------------------------------------------
(for ([i (in-inclusive-range 1 5)])
(displayln i))
------------------------------------------------------------
8 changes: 8 additions & 0 deletions default-recommendations/for-loop-shortcuts.rkt
Original file line number Diff line number Diff line change
Expand Up @@ -462,6 +462,13 @@ return just that result."
(for-id (clause ... #:unless condition) body ...))


(define-refactoring-rule in-range-with-add1-to-in-inclusive-range
#:description "Use `in-inclusive-range` to include the upper bound value in the iterated sequence."
#:literals (in-range add1 +)
(in-range start (~or (add1 end) (+ end 1) (+ 1 end)))
(in-inclusive-range start end))


(define-refactoring-suite for-loop-shortcuts
#:rules (andmap-to-for/and
append-map-for/list-to-for*/list
Expand All @@ -472,6 +479,7 @@ return just that result."
for/fold-with-conditional-body-to-unless-keyword
for/fold-with-conditional-body-to-when-keyword
for-each-to-for
in-range-with-add1-to-in-inclusive-range
list->set-to-for/set
list->vector-to-for/vector
map-to-for
Expand Down
28 changes: 27 additions & 1 deletion private/fully-expanded-syntax.rkt
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,19 @@
(-> syntax?
(hash/c (or/c exact-integer? #false)
(free-id-table/c identifier? (listof identifier?) #:immutable #true)
#:immutable #true))]))
#:immutable #true))]
[fully-expanded-syntax-disappeared-visits
(-> syntax? (vectorof syntax? #:immutable #true #:flat? #true))]))


(require racket/hash
racket/list
racket/match
racket/set
racket/treelist
rebellion/collection/hash
rebellion/collection/vector/builder
resyntax/private/syntax-traversal
syntax/id-table
syntax/parse)

Expand Down Expand Up @@ -305,3 +310,24 @@
(syntax-parse stx
[:fully-expanded-top-level-form
(identifier-binding-table (attribute bound-ids-by-phase) (attribute used-ids-by-phase))]))


(define (fully-expanded-syntax-disappeared-visits stx)
(define builder (make-vector-builder))
(let loop ([stx stx])
(syntax-traverse stx
[form
#:when (syntax-property #'form 'disappeared-visit)
(vector-builder-add-cons-tree builder (syntax-property #'form 'disappeared-visit))
(loop (syntax-property #'form 'disappeared-visit #false))]))
(build-vector builder))


(define (vector-builder-add-cons-tree builder cons-tree)
(match cons-tree
[(cons left right)
(vector-builder-add-cons-tree builder left)
(vector-builder-add-cons-tree builder right)]
['() builder]
[_
(vector-builder-add builder cons-tree)]))
6 changes: 6 additions & 0 deletions private/source.rkt
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,12 @@
(define expanded
(parameterize ([current-expand-observe observe-event!])
(expand program-stx)))

(for ([visited (in-vector (fully-expanded-syntax-disappeared-visits expanded))]
#:when (resyntax-should-analyze-syntax? visited))
(vector-builder-add original-visits visited)
(add-all-original-subforms! visited))

(define binding-table (fully-expanded-syntax-binding-table expanded))
(define original-binding-table-by-position
(for*/fold ([table (hash)])
Expand Down
Loading