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 d8c50c9 commit 948fe1c
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 0 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Expand Up @@ -2,6 +2,10 @@

## master (unreleased)

### New features

* [#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
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.any?(&:begin_type?)
end
end
end
end
20 changes: 20 additions & 0 deletions spec/rubocop/ast/regexp_node_spec.rb
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 948fe1c

Please sign in to comment.