Skip to content

Commit

Permalink
Fix ProcessedSource#commented? for multi-line ranges. Add `Processe…
Browse files Browse the repository at this point in the history
…dSource#commented_line?`
  • Loading branch information
marcandre committed Jul 5, 2020
1 parent 4388778 commit e3f08dc
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 4 deletions.
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#commented_line?`. ([@marcandre][])

### Bug fixes

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

## 0.1.0 (2020-06-26)

Expand Down
10 changes: 9 additions & 1 deletion lib/rubocop/ast/processed_source.rb
Expand Up @@ -95,8 +95,16 @@ def blank?
ast.nil?
end

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

# @return [Boolean] if any of the lines in the given `source_range` has a comment.
def commented?(source_range)
comment_lines.include?(source_range.line)
(source_range.line..source_range.last_line).any? do |line|
commented_line?(line)
end
end

def comments_before_line(line)
Expand Down
45 changes: 42 additions & 3 deletions spec/rubocop/ast/processed_source_spec.rb
Expand Up @@ -251,25 +251,64 @@ def foo # comment one
end
end

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

it 'returns true for lines with comments' do
expect(processed_source.commented_line?(1)).to be true
expect(processed_source.commented_line?(3)).to be true
end

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

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

let(:source) { <<~RUBY }
# comment
[ 1, 2 ]
[ 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
let(:range) { processed_source.find_token(&:left_bracket?).pos }
let(:range) { hash.pairs.first.loc.expression }

it { is_expected.to be false }
end

context 'provided source_range on line with comment' do
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

describe '#comments_before_line' do
Expand Down

0 comments on commit e3f08dc

Please sign in to comment.