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

Conversation

koic
Copy link
Member

@koic koic commented Feb 21, 2022

This PR fixes the following error when using regexp with non-encoding option.

% cat /tmp/example.rb
/\x82/n =~ 'a'

% rubocop -d /tmp/example.rb
(snip)

For /tmp: An error occurred while VariableForce cop was inspecting /tmp/example.rb.
invalid multibyte escape: /\x82/
/Users/koic/.rbenv/versions/3.1.0/lib/ruby/gems/3.1.0/gems/rubocop-1.25.1/lib/rubocop/cop/variable_force.rb:193:in
`initialize'
/Users/koic/.rbenv/versions/3.1.0/lib/ruby/gems/3.1.0/gems/rubocop-1.25.1/lib/rubocop/cop/variable_force.rb:193:in `new'

This error is due to VariableForce#regexp_captured_names not processing the regexp option.
This PR uses RegexpNode#to_regexp to prevent the regexp option from being cut.

In addition, the test code replaces the Parser gem (plain) AST with ProcessedSource#ast so that it can call RegexpNode#to_regexp defined in the RuboCop AST.


Before submitting the PR make sure the following are checked:

  • The PR relates to only one subject with a clear title and description in grammatically correct, complete sentences.
  • Wrote good commit messages.
  • Commit message starts with [Fix #issue-number] (if the related issue exists).
  • Feature branch is up-to-date with master (if not - rebase it).
  • Squashed related commits together.
  • Added tests.
  • Ran bundle exec rake default. It executes all tests and runs RuboCop on its own code.
  • Added an entry (file) to the changelog folder named {change_type}_{change_description}.md if the new code introduces user-observable changes. See changelog entry format for details.

@koic koic force-pushed the fix_an_error_when_using_regexp_with_noencoding_option branch from 1144278 to 3f97bc4 Compare February 21, 2022 15:31
@koic koic changed the title Fix an error when using regexp with non-encoding moption Fix an error when using regexp with non-encoding option Feb 21, 2022
@koic koic force-pushed the fix_an_error_when_using_regexp_with_noencoding_option branch 3 times, most recently from d3082f2 to db0ccab Compare February 21, 2022 15:38
This PR fixes the following error when using regexp with non-encoding option.

```console
% cat /tmp/example.rb
/\x82/n =~ 'a'

% rubocop -d /tmp/example.rb
(snip)

For /tmp: An error occurred while VariableForce cop was inspecting /tmp/example.rb.
invalid multibyte escape: /\x82/
/Users/koic/.rbenv/versions/3.1.0/lib/ruby/gems/3.1.0/gems/rubocop-1.25.1/lib/rubocop/cop/variable_force.rb:193:in
`initialize'
/Users/koic/.rbenv/versions/3.1.0/lib/ruby/gems/3.1.0/gems/rubocop-1.25.1/lib/rubocop/cop/variable_force.rb:193:in `new'
```

This error is due to `VariableForce#regexp_captured_names` not processing the regexp option.
This PR uses `RegexpNode#to_regexp` to prevent the regexp option from being cut.

In addition, the test code replaces the Parser gem (plain) AST with `ProcessedSource#ast`
so that it can call `RegexpNode#to_regexp` defined in the RuboCop AST.
@koic koic force-pushed the fix_an_error_when_using_regexp_with_noencoding_option branch from db0ccab to eaf40f8 Compare February 22, 2022 03:00

# 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

@bbatsov bbatsov merged commit 3abbf83 into rubocop:master Feb 27, 2022
@bbatsov
Copy link
Collaborator

bbatsov commented Feb 27, 2022

Thanks!

@koic koic deleted the fix_an_error_when_using_regexp_with_noencoding_option branch February 27, 2022 06:29
koic added a commit to koic/rubocop that referenced this pull request Feb 8, 2023
This PR reverts a pending test of rubocop#10432 because jruby/jruby#7113
has been resolved in JRuby 9.3.9.0.

## JRuby 9.3.3.0

`RegexpError` error will occur:

```console
% rbenv local jruby-9.3.3.0
% ruby -e "Regexp.new('\x82', Regexp::NOENCODING)"
RegexpError: invalid multibyte escape: /\x82/
  initialize at org/jruby/RubyRegexp.java:956
         new at org/jruby/RubyClass.java:893
      <main> at -e:1
```

## JRuby 9.3.9.0 and JRuby 9.4.1.0

No errors:

```console
% rbenv local jruby-9.3.9.0
% ruby -e "Regexp.new('\x82', Regexp::NOENCODING)"

% rbenv local jruby-9.4.1.0
% ruby -e "Regexp.new('\x82', Regexp::NOENCODING)"
```
bbatsov pushed a commit that referenced this pull request Feb 8, 2023
This PR reverts a pending test of #10432 because jruby/jruby#7113
has been resolved in JRuby 9.3.9.0.

## JRuby 9.3.3.0

`RegexpError` error will occur:

```console
% rbenv local jruby-9.3.3.0
% ruby -e "Regexp.new('\x82', Regexp::NOENCODING)"
RegexpError: invalid multibyte escape: /\x82/
  initialize at org/jruby/RubyRegexp.java:956
         new at org/jruby/RubyClass.java:893
      <main> at -e:1
```

## JRuby 9.3.9.0 and JRuby 9.4.1.0

No errors:

```console
% rbenv local jruby-9.3.9.0
% ruby -e "Regexp.new('\x82', Regexp::NOENCODING)"

% rbenv local jruby-9.4.1.0
% ruby -e "Regexp.new('\x82', Regexp::NOENCODING)"
```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants