diff --git a/changelog/fix_regexp_node_aware_of_fixed_encoding_regopt.md b/changelog/fix_regexp_node_aware_of_fixed_encoding_regopt.md new file mode 100644 index 000000000..29ca6106c --- /dev/null +++ b/changelog/fix_regexp_node_aware_of_fixed_encoding_regopt.md @@ -0,0 +1 @@ +* [#230](https://github.com/rubocop-hq/rubocop-ast/pull/230): Make `RegexpNode` aware of fixed-encoding regopt. ([@koic][]) diff --git a/lib/rubocop/ast/node/regexp_node.rb b/lib/rubocop/ast/node/regexp_node.rb index f5e971a30..f9ee27e47 100644 --- a/lib/rubocop/ast/node/regexp_node.rb +++ b/lib/rubocop/ast/node/regexp_node.rb @@ -11,6 +11,7 @@ class RegexpNode < Node i: Regexp::IGNORECASE, m: Regexp::MULTILINE, n: Regexp::NOENCODING, + u: Regexp::FIXEDENCODING, o: 0 }.freeze private_constant :OPTIONS @@ -87,6 +88,11 @@ def no_encoding? regopt_include?(:n) end + # @return [Bool] if regexp uses the fixed-encoding regopt + def fixed_encoding? + regopt_include?(:u) + end + private def regopt_include?(option) diff --git a/spec/rubocop/ast/regexp_node_spec.rb b/spec/rubocop/ast/regexp_node_spec.rb index 3081b792a..9e921336e 100644 --- a/spec/rubocop/ast/regexp_node_spec.rb +++ b/spec/rubocop/ast/regexp_node_spec.rb @@ -53,6 +53,18 @@ it { expect(regexp_node.to_regexp.inspect).to eq('/abc/i') } end + + context 'with a regexp with an "n" option' do + let(:source) { '/abc/n' } + + it { expect(regexp_node.to_regexp.inspect).to eq('/abc/n') } + end + + context 'with a regexp with an "u" option' do + let(:source) { '/abc/u' } + + it { expect(regexp_node.to_regexp.inspect).to eq('/abc/') } + end end describe '#regopt' do @@ -490,6 +502,32 @@ end end + describe '#fixed_encoding?' do + context 'with no options' do + let(:source) { '/x/' } + + it { is_expected.not_to be_fixed_encoding } + end + + context 'with other options' do + let(:source) { '/x/xm' } + + it { is_expected.not_to be_fixed_encoding } + end + + context 'with only u option' do + let(:source) { '/x/u' } + + it { is_expected.to be_fixed_encoding } + end + + context 'with u and other options' do + let(:source) { '/x/unm' } + + it { is_expected.to be_fixed_encoding } + end + end + describe '#single_interpolation?' do context 'with no options' do let(:source) { '/x/' }