Skip to content

Commit

Permalink
Add interpolation? check for RegexpNode
Browse files Browse the repository at this point in the history
Closes rubocop#4
  • Loading branch information
tejasbubane committed Jun 1, 2020
1 parent b23ae1c commit e8e7d3e
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 0 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

## master (unreleased)

* [#4](https://github.com/rubocop-hq/rubocop-ast/issues/4): Add `interpolation?` for `RegexpNode`. ([@tejasbubane][])

## 0.0.3 (2020-05-15)

### Changes
Expand Down
5 changes: 5 additions & 0 deletions lib/rubocop/ast/node/regexp_node.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@ def regopt
def content
children.select(&:str_type?).map(&:str_content).join
end

# @return [Bool] if regexp contains interpolation
def interpolation?
children.select(&:begin_type?).any?
end
end
end
end
20 changes: 20 additions & 0 deletions spec/rubocop/ast/regexp_node_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -140,4 +140,24 @@
it { expect(content).to eq("\n.+\n") }
end
end

describe '#has_interpolation?' do
context 'with direct variable interpoation' do
let(:source) { '/\n\n#{foo}(abc)+/' }

it { expect(regexp_node.interpolation?).to eq(true) }
end

context 'with regexp quote' do
let(:source) { '/\n\n#{Regexp.quote(foo)}(abc)+/' }

it { expect(regexp_node.interpolation?).to eq(true) }
end

context 'with no interpolation returns false' do
let(:source) { '/a{3,6}/' }

it { expect(regexp_node.interpolation?).to eq(false) }
end
end
end

0 comments on commit e8e7d3e

Please sign in to comment.