diff --git a/lib/rubocop/ast/node/regexp_node.rb b/lib/rubocop/ast/node/regexp_node.rb index 5ca476e08..bf494d05c 100644 --- a/lib/rubocop/ast/node/regexp_node.rb +++ b/lib/rubocop/ast/node/regexp_node.rb @@ -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 diff --git a/spec/rubocop/ast/regexp_node_spec.rb b/spec/rubocop/ast/regexp_node_spec.rb index 1897f5976..d052dee86 100644 --- a/spec/rubocop/ast/regexp_node_spec.rb +++ b/spec/rubocop/ast/regexp_node_spec.rb @@ -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