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 for Lint/ErbNewArguments cop #7806

Merged
merged 1 commit into from Mar 21, 2020

Conversation

koic
Copy link
Member

@koic koic commented Mar 21, 2020

This PR fixes the following error for Lint/ErbNewArguments cop when inspecting ActionView::Template::Handlers::ERB.new.

% cd path/to/rails/rails # rails/rails@f265e0dd
% bundle exec rubocop -d --only Lint/ErbNewArguments
actionview/lib/action_view/template/handlers.rb
For /Users/koic/src/github.com/rails/rails: configuration from
/Users/koic/src/github.com/rubocop-hq/rubocop/config/default.yml
Inspecting 1 file
Scanning
/Users/koic/src/github.com/rails/rails/actionview/lib/action_view/template/handlers.rb
An error occurred while Lint/ErbNewArguments cop was inspecting
/Users/koic/src/github.com/rails/rails/actionview/lib/action_view/template/handlers.rb:14:45.
undefined method `each_with_index' for nil:NilClass
/Users/koic/src/github.com/rubocop-hq/rubocop/lib/rubocop/cop/lint/erb_new_arguments.rb:89:in
`block in on_send'
/Users/koic/src/github.com/rubocop-hq/rubocop/lib/rubocop/cop/lint/erb_new_arguments.rb:90:in
`erb_new_with_non_keyword_arguments'
/Users/koic/src/github.com/rubocop-hq/rubocop/lib/rubocop/cop/lint/erb_new_arguments.rb:86:in
`on_send'

ActionView::Template::Handlers::ERB accepts new method without arguments, unlike standard ERB.

Below is the code that caused the error.

base.register_template_handler :erb, ERB.new

https://github.com/rails/rails/blob/v6.0.2.2/actionview/lib/action_view/template/handlers.rb#L16

It is an error if the standard ERB.new without arguments is used.
Therefore standard ERB.new and ActionView::Template::Handlers::ERB.new do not conflict.

% ruby -rerb -ve 'ERB.new'
ruby 2.7.0p0 (2019-12-25 revision 647ee6f091) [x86_64-darwin17]
Traceback (most recent call last):
        2: from -e:1:in `<main>'
        1: from -e:1:in `new'
/Users/koic/.rbenv/versions/2.7.0/lib/ruby/2.7.0/erb.rb:809:in
`initialize': wrong number of arguments (given 0, expected 1..4) (ArgumentError)

So including this patch in the cop will not interfere with linting to standard ERB.


Before submitting the PR make sure the following are checked:

  • 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.
  • Added an entry to the Changelog if the new code introduces user-observable changes. See changelog entry format.
  • The PR relates to only one subject with a clear title and description in grammatically correct, complete sentences.
  • Run bundle exec rake default. It executes all tests and RuboCop for itself, and generates the documentation.

This PR fixes the following error for `Lint/ErbNewArguments` cop
when inspecting `ActionView::Template::Handlers::ERB.new`.

```console
% cd path/to/rails/rails # rails/rails@f265e0dd
% bundle exec rubocop -d --only Lint/ErbNewArguments
actionview/lib/action_view/template/handlers.rb
For /Users/koic/src/github.com/rails/rails: configuration from
/Users/koic/src/github.com/rubocop-hq/rubocop/config/default.yml
Inspecting 1 file
Scanning
/Users/koic/src/github.com/rails/rails/actionview/lib/action_view/template/handlers.rb
An error occurred while Lint/ErbNewArguments cop was inspecting
/Users/koic/src/github.com/rails/rails/actionview/lib/action_view/template/handlers.rb:14:45.
undefined method `each_with_index' for nil:NilClass
/Users/koic/src/github.com/rubocop-hq/rubocop/lib/rubocop/cop/lint/erb_new_arguments.rb:89:in
`block in on_send'
/Users/koic/src/github.com/rubocop-hq/rubocop/lib/rubocop/cop/lint/erb_new_arguments.rb:90:in
`erb_new_with_non_keyword_arguments'
/Users/koic/src/github.com/rubocop-hq/rubocop/lib/rubocop/cop/lint/erb_new_arguments.rb:86:in
`on_send'
```

`ActionView::Template::Handlers::ERB` accepts `new` method without arguments,
unlike standard `ERB`.

Below is the code that caused the error.

```ruby
base.register_template_handler :erb, ERB.new
```

https://github.com/rails/rails/blob/v6.0.2.2/actionview/lib/action_view/template/handlers.rb#L16

It is an error if the standard `ERB.new` without arguments is used.
Therefore standard `ERB.new` and `ActionView::Template::Handlers::ERB.new`
do not conflict.

```console
% ruby -rerb -ve 'ERB.new'
ruby 2.7.0p0 (2019-12-25 revision 647ee6f091) [x86_64-darwin17]
Traceback (most recent call last):
        2: from -e:1:in `<main>'
        1: from -e:1:in `new'
/Users/koic/.rbenv/versions/2.7.0/lib/ruby/2.7.0/erb.rb:809:in
`initialize': wrong number of arguments (given 0, expected 1..4) (ArgumentError)
```

So including this patch in the cop will not interfere with linting to standard `ERB`.
@koic koic force-pushed the fix_error_for_lint_erb_new_arguments branch from 43182ea to ab5144a Compare March 21, 2020 12:25
@bbatsov bbatsov merged commit 0aa2e59 into rubocop:master Mar 21, 2020
@bbatsov
Copy link
Collaborator

bbatsov commented Mar 21, 2020

Thanks!

@koic koic deleted the fix_error_for_lint_erb_new_arguments branch March 21, 2020 15:08
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