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

Rails/ContentTag cop auto-correct creates invalid Ruby code in Ruby 3.0 #511

Closed
Adrian-Hirt opened this issue Jun 28, 2021 · 1 comment
Closed

Comments

@Adrian-Hirt
Copy link

When using an application with Ruby 3.0 (3.0.1 in my case), the autocorrect output of the Rails/ContentTag generates tags which either produce an exception or ignore the passed options. I've been using the tags in the controller via view_context.

Using rubocop --auto-correct --only Rails/ContentTag on the following code samples produces invalid ruby code:

  1. Using content_tag with escaping:

    view_context.content_tag(:span, '<b>Content</b>', { class: 'test', data: { foo: 'bar' } })
  2. Using content_tag , disabling escaping by passing in false as last param:

    view_context.content_tag(:span, '<b>Content</b>', { class: 'test', data: { foo: 'bar' } }, false)
  3. Using the test added by PR Fix auto correction for Rails/ContentTag when content_tag is called with options hash and block #270:

    view_context.content_tag :div, { class: 'strong' } do
      'body'
    end

Expected behavior

The cop should generate Ruby code which is valid in Ruby 3.0+, i.e. it should produce the following:

  1. Either one of the two variations:

    view_context.tag.span('<b>Content</b>', **{ class: 'test', data: { foo: 'bar' } })
    view_context.tag.span('<b>Content</b>', class: 'test', data: { foo: 'bar' })
  2. Either one of the two variations:

    view_context.tag.span('<b>Content</b>', **{ class: 'test', data: { foo: 'bar' } }, escape_attributes: false)
    view_context.tag.span('<b>Content</b>', class: 'test', data: { foo: 'bar' }, escape_attributes: false)
  3. Either one of the two variations:

    view_context.tag.div(**{class: 'strong'}) do
      'body'
    end
    view_context.tag.div(class: 'strong') do
      'body'
    end

Actual behavior

The cop generates the following code:

  1. The code fails with an ArgumentError: wrong number of arguments (given 3, expected 1..2)

    view_context.tag.span('<b>Content</b>', { class: 'test', data: { foo: 'bar' } })
  2. The code fails with an ArgumentError: wrong number of arguments (given 4, expected 1..2). Please note, that the last argument false to disable escaping of the content is a keyword argument in the tag method, i.e. this needs to be converted into a keyword argument.

    view_context.tag.span('<b>Content</b>', { class: 'test', data: { foo: 'bar' } }, false)
  3. This variant does not fail, but the generated HTML is incorrect. The generated code is:

    view_context.tag.div({class: 'strong'}) do
      'body'
    end

    which generates the following HTML:

    <div>body</div>

    i.e. the class option is ignored.

Steps to reproduce the problem

Use any of the above mentioned versions in a Rails 6.1.4 App using Ruby 3.0.1, and run rubocop --auto-correct --only Rails/ContentTag.

Please let me know if you need more informations or when I should create a Demo Repo to reproduce the issue.

RuboCop version

$ bundle exec rubocop -V
1.17.0 (using Parser 3.0.1.1, rubocop-ast 1.7.0, running on ruby 3.0.1 x86_64-linux)
  - rubocop-rails 2.11.1
@koic
Copy link
Member

koic commented Aug 20, 2021

This issue will be resolved by #526 because it will make Rails/ContentTag aware of tag method instead of content_tag method.

@koic koic closed this as completed Aug 20, 2021
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

No branches or pull requests

2 participants