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 authored and bbatsov committed Jul 12, 2020
1 parent 0c64787 commit ac40528
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 7 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#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
49 changes: 44 additions & 5 deletions spec/rubocop/ast/processed_source_spec.rb
Expand Up @@ -251,25 +251,64 @@ def foo # comment one
end
end

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

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, 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 ac40528

Please sign in to comment.