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

Fix an error when using regexp with non-encoding option #10432

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 @@
* [#10432](https://github.com/rubocop/rubocop/pull/10432): Fix an error when using regexp with non-encoding option. ([@koic][])
6 changes: 1 addition & 5 deletions lib/rubocop/cop/variable_force.rb
Expand Up @@ -186,11 +186,7 @@ def process_regexp_named_captures(node)
end

def regexp_captured_names(node)
regexp_string = node.children.select(&:str_type?).map do |child|
child.children.first
end.join || ''

regexp = Regexp.new(regexp_string) # FIXME: Need to be handle `Regexp.new(/\x82/n)`.
regexp = node.to_regexp

regexp.named_captures.keys
end
Expand Down
24 changes: 16 additions & 8 deletions spec/rubocop/cop/variable_force_spec.rb
Expand Up @@ -21,7 +21,17 @@
end

context 'when processing an empty regex' do
let(:node) { s(:match_with_lvasgn, s(:regexp, s(:regopt)), s(:str)) }
let(:node) { parse_source('// =~ ""').ast }

it 'does not raise an error' do
expect { force.process_node(node) }.not_to raise_error
end
end

# FIXME: Remove `broken_on: jruby` when JRuby incompatible is resolved:
# https://github.com/jruby/jruby/issues/7113
context 'when processing a regex with regopt', broken_on: :jruby do
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This new test will be skipped due to JRuby incompatibility.
jruby/jruby#7113

let(:node) { parse_source('/\x82/n =~ "a"').ast }

it 'does not raise an error' do
expect { force.process_node(node) }.not_to raise_error
Expand All @@ -30,13 +40,11 @@

context 'when processing a regexp with a line break at the start of capture parenthesis' do
let(:node) do
s(:match_with_lvasgn,
s(:regexp,
s(:str, "(\n"),
s(:str, " pattern\n"),
s(:str, ')'),
s(:regopt)),
s(:send, nil?, :string))
parse_source(<<~REGEXP).ast
/(
pattern
)/ =~ string
REGEXP
end

it 'does not raise an error' do
Expand Down