Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make RegexpNode aware of fixed-encoding regopt #230

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
@@ -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