Skip to content
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

Fix ProcessedSource#commented? for multi-line ranges. Add ProcessedSource#commented_line? #55

Merged
merged 2 commits into from Jul 12, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 5 additions & 0 deletions CHANGELOG.md
Expand Up @@ -5,6 +5,11 @@
### New features

* [#50](https://github.com/rubocop-hq/rubocop-ast/pull/50): Support find pattern matching for Ruby 2.8 (3.0) parser. ([@koic][])
* [#55](https://github.com/rubocop-hq/rubocop-ast/pull/55): Add `ProcessedSource#line_with_comment?`. ([@marcandre][])

### Bug fixes

* [#55](https://github.com/rubocop-hq/rubocop-ast/pull/55): Fix `ProcessedSource#commented?` for multi-line ranges. Renamed `contains_comment?` ([@marcandre][])

## 0.1.0 (2020-06-26)

Expand Down
14 changes: 12 additions & 2 deletions lib/rubocop/ast/processed_source.rb
Expand Up @@ -99,10 +99,20 @@ def blank?
ast.nil?
end

def commented?(source_range)
comment_lines.include?(source_range.line)
# @return [Boolean] if the given line number has a comment.
def line_with_comment?(line)
comment_lines.include?(line)
end

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

def comments_before_line(line)
comments.select { |c| c.location.line <= line }
end
Expand Down
61 changes: 50 additions & 11 deletions spec/rubocop/ast/processed_source_spec.rb
Expand Up @@ -251,24 +251,63 @@ def foo # comment one
end
end

describe '#commented?' do
describe '#line_with_comment?' do
let(:source) { <<~RUBY }
# comment
[ 1, 2 ]
[
1, # comment
2
]
RUBY

context 'provided source_range on line with comment' do
it 'returns true' do
bracket_range = processed_source.find_token(&:left_bracket?).pos
expect(processed_source.commented?(bracket_range)).to be false
end
it 'returns true for lines with comments' do
expect(processed_source.line_with_comment?(1)).to be true
expect(processed_source.line_with_comment?(3)).to be true
end

it 'returns false for lines without comments' do
expect(processed_source.line_with_comment?(2)).to be false
expect(processed_source.line_with_comment?(4)).to be false
end
end

describe '#contains_comment?' do
subject(:commented) { processed_source.contains_comment?(range) }

let(:source) { <<~RUBY }
# comment
[ 1,
{ a: 2,
b: 3 # comment
}
]
RUBY
let(:ast) { processed_source.ast }
let(:array) { ast }
let(:hash) { array.children[1] }

context 'provided source_range on line without comment' do
it 'returns false' do
comment_range = processed_source.find_token(&:comment?).pos
expect(processed_source.commented?(comment_range)).to be true
end
let(:range) { hash.pairs.first.loc.expression }

it { is_expected.to be false }
end

context 'provided source_range on comment line' do
let(:range) { processed_source.find_token(&:comment?).pos }

it { is_expected.to be true }
end

context 'provided source_range on line with comment' do
let(:range) { hash.pairs.last.loc.expression }

it { is_expected.to be true }
end

context 'provided a multiline source_range with at least one line with comment' do
let(:range) { array.loc.expression }

it { is_expected.to be true }
end
end

Expand Down