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 0a8bd74
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 0 deletions.
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.select(&:begin_type?).any?
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) { '/\n\n(abc)+/' }

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

0 comments on commit 0a8bd74

Please sign in to comment.