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

Commits on Mar 21, 2020

  1. Fix an error for Lint/ErbNewArguments cop

    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 committed Mar 21, 2020
    Copy the full SHA
    ab5144a View commit details
    Browse the repository at this point in the history