Skip to content

Rails/ContentTag creates an invalid method call trying to fix the legacy syntax with parameters #556

Closed
@yheuhtozr

Description

The following behavior seems not right, as the transformed expression ends up in error.

it 'corrects an offense with all arguments' do
expect_offense(<<~RUBY)
tag(:br, {class: ["strong", "highlight"]}, true, false)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use `tag.br` instead of `tag(:br)`.
RUBY
expect_correction(<<~RUBY)
tag.br({class: ["strong", "highlight"]}, true, false)
RUBY
end

Expected behavior

Leave a call with legacy arguments such as tag(:br, {class: ["strong", "highlight"]}, true, false) as it as, or convert into something legal and non-deprecated (does it have any outside a raw HTML string?).

Actual behavior

It outputs tag.br({class: ["strong", "highlight"]}, true, false), which results in error.

Confirmed on my env:

$ rails c
Running via Spring preloader in process 29296
Loading development environment (Rails 6.1.4.1)
[1] pry(main)> helper.tag.br({class: ["strong", "highlight"]}, true, false)
ArgumentError: wrong number of arguments (given 4, expected 1..2)
from /home/user/.rbenv/versions/3.0.2/lib/ruby/gems/3.0.0/gems/actionview-6.1.4.1/lib/action_view/helpers/tag_helper.rb:56:in `tag_string'
[2] pry(main)> helper.tag(:br, {class: ["strong", "highlight"]}, true, false)
=> "<br class=\"strong highlight\">"

Steps to reproduce the problem

Run Rubocop.

RuboCop version

$ rubocop -V
1.21.0 (using Parser 3.0.2.0, rubocop-ast 1.11.0, running on ruby 3.0.2 x86_64-linux)
  - rubocop-performance 1.11.5
  - rubocop-rails 2.12.2
  - rubocop-rspec 2.5.0

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions