Skip to content

Commit

Permalink
Add ProcessedSource#each_comment_in_lines
Browse files Browse the repository at this point in the history
Deprecate `ProcessedSource#comments_before_line`
  • Loading branch information
marcandre committed Aug 1, 2020
1 parent 641f0eb commit eb51ce0
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 5 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
* [#85](https://github.com/rubocop-hq/rubocop-ast/pull/85): Add `IntNode#value` and `FloatNode#value`. ([@fatkodima][])
* [#82](https://github.com/rubocop-hq/rubocop-ast/pull/82): `NodePattern`: Allow comments ([@marcandre][])
* [#83](https://github.com/rubocop-hq/rubocop-ast/pull/83): Add `ProcessedSource#comment_at_line` ([@marcandre][])
* [#83](https://github.com/rubocop-hq/rubocop-ast/pull/83): Add `ProcessedSource#each_comment_in_lines` ([@marcandre][])

### Bug fixes

Expand Down
22 changes: 17 additions & 5 deletions lib/rubocop/ast/processed_source.rb
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ def each_comment(&block)
comments.each(&block)
end

# @deprecated Use `comments.find` or `comment_at_line`
# @deprecated Use `comment_at_line`, `each_comment_in_lines`, or `comments.find`
def find_comment(&block)
comments.find(&block)
end
Expand Down Expand Up @@ -109,17 +109,29 @@ def line_with_comment?(line)
comment_index.include?(line)
end

# Enumerates on the comments contained with the given `line_range`
def each_comment_in_lines(line_range)
return to_enum(:each_comment_in_lines, line_range) unless block_given?

line_range.each do |line|
if (comment = comment_index[line])
yield comment
end
end
end

# @return [Boolean] if any of the lines in the given `source_range` has a comment.
# Consider using `each_comment_in_lines` instead
def contains_comment?(source_range)
(source_range.line..source_range.last_line).any? do |line|
line_with_comment?(line)
end
each_comment_in_lines(source_range.line..source_range.last_line).any?
end
# @deprecated use contains_comment?
alias commented? contains_comment?

# @deprecated Use `each_comment_in_lines`
# Should have been called `comments_before_or_at_line`. Doubtful it has of any valid use.
def comments_before_line(line)
comments.select { |c| c.location.line <= line }
each_comment_in_lines(0..line).to_a
end

def start_with?(string)
Expand Down
9 changes: 9 additions & 0 deletions spec/rubocop/ast/processed_source_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,15 @@ def some_method
end
end

describe '#each_comment_in_lines' do
it 'yields the comments' do
enum = processed_source.each_comment_in_lines(1..4)
expect(enum.is_a?(Enumerable)).to be(true)
expect(enum.to_a).to eq processed_source.comments
expect(processed_source.each_comment_in_lines(2..5).map(&:text)).to eq ['# comment two']
end
end

describe '#line_with_comment?' do
it 'returns true for lines with comments' do
expect(processed_source.line_with_comment?(1)).to be true
Expand Down

0 comments on commit eb51ce0

Please sign in to comment.