From 0a8bd74e9225c0049e9a4b71058f6147ad17eeb6 Mon Sep 17 00:00:00 2001 From: Tejas Bubane Date: Mon, 1 Jun 2020 14:51:08 +0530 Subject: [PATCH] Add interpolation? check for RegexpNode Closes #4 --- lib/rubocop/ast/node/regexp_node.rb | 5 +++++ spec/rubocop/ast/regexp_node_spec.rb | 20 ++++++++++++++++++++ 2 files changed, 25 insertions(+) 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