Skip to content

Commit

Permalink
Move ProcessedSource#sorted_tokens to be a public method.
Browse files Browse the repository at this point in the history
  • Loading branch information
dvandersluis authored and marcandre committed Aug 6, 2021
1 parent f8e278d commit 18a2934
Showing 1 changed file with 8 additions and 8 deletions.
16 changes: 8 additions & 8 deletions lib/rubocop/ast/processed_source.rb
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,14 @@ def last_token_of(range_or_node)
sorted_tokens[last_token_index(range_or_node)]
end

# The tokens list is always sorted by token position, except for cases when heredoc
# is passed as a method argument. In this case tokens are interleaved by
# heredoc contents' tokens.
def sorted_tokens
# Use stable sort.
@sorted_tokens ||= tokens.sort_by.with_index { |token, i| [token.begin_pos, i] }
end

private

def comment_index
Expand Down Expand Up @@ -272,14 +280,6 @@ def last_token_index(range_or_node)
sorted_tokens.bsearch_index { |token| token.end_pos >= end_pos }
end

# The tokens list is always sorted by token position, except for cases when heredoc
# is passed as a method argument. In this case tokens are interleaved by
# heredoc contents' tokens.
def sorted_tokens
# Use stable sort.
@sorted_tokens ||= tokens.sort_by.with_index { |token, i| [token.begin_pos, i] }
end

def source_range(range_or_node)
if range_or_node.respond_to?(:source_range)
range_or_node.source_range
Expand Down

0 comments on commit 18a2934

Please sign in to comment.