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 #260] Fix target of Rails/ContentTag #526

Merged
merged 1 commit into from
Aug 19, 2021
Merged

[Fix #260] Fix target of Rails/ContentTag #526

merged 1 commit into from
Aug 19, 2021

Conversation

tabuchik
Copy link
Contributor

@tabuchik tabuchik commented Aug 15, 2021

Fix #260

This PR fixes target of Rails/ContentTag cop.
The reason is that it is unclear wheather content_tag is deprecated, and auto-correction may cause performance regression.

Following test cases are added.

  • If first argument is kebab case, it can be autocorrected to snake case method.
  • If first argument is snake case, it cannot be autocorrected.
#!/usr/bin/env rails runner

helper = ApplicationController.helpers
puts helper.tag("foo-bar")
# => <foo-bar />
puts helper.tag("foo_bar")
# => <foo_bar />
puts helper.tag.foo_bar
# => <foo-bar></foo-bar>

puts helper.tag("-foo")
# => <-foo />
puts helper.tag("_foo")
# => <_foo />
puts helper.tag._foo
# => <-foo></-foo>

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 (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.
  • 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.
  • If this is a new cop, consider making a corresponding update to the Rails Style Guide.

@@ -181,8 +181,9 @@ Rails/BulkChangeTable:
- db/migrate/*.rb

Rails/ContentTag:
Description: 'Use `tag` instead of `content_tag`.'
Description: 'Use `tag.something(` instead of `tag(:something`.'
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IMHO, it's strange to write only the opening parenthesis.

Suggested change
Description: 'Use `tag.something(` instead of `tag(:something`.'
Description: 'Use `tag.something` instead of `tag(:something)`.'

# NOTE: Allow `content_tag` when the first argument is a variable because
# `content_tag(name)` is simpler rather than `tag.public_send(name)`.
# NOTE: Allow `tag` when the first argument is a variable because
# `tag(name)` is simpler rather than `tag.public_send(name)`.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add the following mention?

Suggested change
# `tag(name)` is simpler rather than `tag.public_send(name)`.
# `tag(name)` is simpler rather than `tag.public_send(name)`.
# And this cop will be renamed to something like `LegacyTag` in the future. (e.g. RuboCop Rails 2.0)

# content_tag(:p, 'Hello world!')
# content_tag(:br)
# tag(:p)
# tag(:br, {class: 'classname'})
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you omit the redundant parentheses?

Suggested change
# tag(:br, {class: 'classname'})
# tag(:br, class: 'classname')

# content_tag(name, 'Hello world!')
# tag.p
# tag.br(class: 'classname')
# tag(name, {class: 'classname'})
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
# tag(name, {class: 'classname'})
# tag(name, class: 'classname')

class ContentTag < Base
include RangeHelp
extend AutoCorrector
extend TargetRailsVersion

minimum_target_rails_version 5.1

MSG = 'Use `tag` instead of `content_tag`.'
RESTRICT_ON_SEND = %i[content_tag].freeze
MSG = 'Use `tag.something(` instead of `tag(:something`.'
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
MSG = 'Use `tag.something(` instead of `tag(:something`.'
MSG = 'Use `tag.something` instead of `tag(:something)`.'

@koic
Copy link
Member

koic commented Aug 17, 2021

Can you add the changelog entry and add VersionChanged: '2.12' to Rails/ContentTag configuration in the config/default.yml

RUBY
end

it 'does not register an offense when `content_tag` is called with no arguments' do
it 'does not register an offense when first argument is string starts with underscore' do
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
it 'does not register an offense when first argument is string starts with underscore' do
it 'does not register an offense when first argument is a string which starts with an underscore' do

@tabuchik
Copy link
Contributor Author

@koic @andyw8
I updated this pull request with your comment.
Thank you for reviews!

CHANGELOG.md Outdated Show resolved Hide resolved
config/default.yml Outdated Show resolved Hide resolved
@tabuchik
Copy link
Contributor Author

@koic
Thanks for your quick response!
I accepted your suggestions and fix CI.

@koic
Copy link
Member

koic commented Aug 18, 2021

Nice! Can you squash your commits into one?

Tweak for review comments

Add a changelog entry

Modify VersionChanged instead of VersionAdded

Co-authored-by: Koichi ITO <[email protected]>

Fix CHANGELOG.md

Co-authored-by: Koichi ITO <[email protected]>

Generate doc
@tabuchik
Copy link
Contributor Author

@koic
Thank you! done

@koic koic merged commit 6163f9a into rubocop:master Aug 19, 2021
@koic
Copy link
Member

koic commented Aug 19, 2021

Thanks!

koic added a commit that referenced this pull request Aug 21, 2021
koic added a commit that referenced this pull request Dec 17, 2023
evgeni added a commit to evgeni/katello that referenced this pull request Jan 23, 2024
The cop is buggy before rubocop-rails 2.12.0 [1][2], but to get that
we'd need a newer theforeman-rubocop, which generates more offenses than
I am willing to fix today. Disable the cop like it was disabled in
foreman_ansible too [3].

[1] rubocop/rubocop-rails#260
[2] rubocop/rubocop-rails#526
[3] theforeman/foreman_ansible#357
jeremylenz pushed a commit to Katello/katello that referenced this pull request Jan 23, 2024
The cop is buggy before rubocop-rails 2.12.0 [1][2], but to get that
we'd need a newer theforeman-rubocop, which generates more offenses than
I am willing to fix today. Disable the cop like it was disabled in
foreman_ansible too [3].

[1] rubocop/rubocop-rails#260
[2] rubocop/rubocop-rails#526
[3] theforeman/foreman_ansible#357
m-bucher pushed a commit to ATIX-AG/katello that referenced this pull request Jun 21, 2024
The cop is buggy before rubocop-rails 2.12.0 [1][2], but to get that
we'd need a newer theforeman-rubocop, which generates more offenses than
I am willing to fix today. Disable the cop like it was disabled in
foreman_ansible too [3].

[1] rubocop/rubocop-rails#260
[2] rubocop/rubocop-rails#526
[3] theforeman/foreman_ansible#357

(cherry picked from commit 98c9fee)
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.

The Rails/ContentTag should only target the deprecated tag helper
3 participants