Skip to content

Commit

Permalink
Make RegexpNode aware of fixed-encoding regopt
Browse files Browse the repository at this point in the history
Resolves rubocop/rubocop#10552.

This PR makes `RegexpNode` aware of fixed-encoding regopt.
  • Loading branch information
koic authored and marcandre committed Aug 7, 2022
1 parent 5acfb16 commit ce40f21
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 0 deletions.
@@ -0,0 +1 @@
* [#230](https://github.com/rubocop-hq/rubocop-ast/pull/230): Make `RegexpNode` aware of fixed-encoding regopt. ([@koic][])
6 changes: 6 additions & 0 deletions lib/rubocop/ast/node/regexp_node.rb
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down
38 changes: 38 additions & 0 deletions spec/rubocop/ast/regexp_node_spec.rb
Expand Up @@ -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
Expand Down Expand Up @@ -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/' }
Expand Down

0 comments on commit ce40f21

Please sign in to comment.