-
-
Notifications
You must be signed in to change notification settings - Fork 263
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
Add new Rails/ContentTag cop #242
Conversation
Can you add a test case for using block? e.g: content_tag(:div) { content_tag(:strong, 'Hi') } |
@koic |
config/default.yml
Outdated
- 'https://github.com/rails/rails/issues/25195' | ||
- 'https://api.rubyonrails.org/classes/ActionView/Helpers/TagHelper.html#method-i-content_tag' | ||
Enabled: true | ||
Safe: true |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, can you omit Safe: true
because it is safe by default?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh, I see.
This looks good to me. Can you squash your commits into one? |
Remove unnecessary |
Thank you! |
@koic Would you be open to publishing a new release that includes this change (and the other changes since https://github.com/rubocop-hq/rubocop-rails/releases/tag/v2.5.2)? |
I've found several issues with this cop and I plan to release 2.6 that includes this one after fixing them. |
Gotcha, thank you for the update (and the speedy reply)! |
Follow up to rubocop#242. `content_tag(name)` cannot be corrected to `tag.name` when the first argument is a variable. So use `public_send` and prevent the following errors in that case: ## Case 1: Using `public_send` to prevent the following `NoMethodError` Original code: ```ruby content_tag(name, 'foo', class: 'bar') ``` Auto-corrected code (before): ```ruby tag(name, 'foo', class: 'bar') #=> NoMethodError (undefined method `each_pair' for "foo":String) ``` Auto-corrected code (after): ```ruby tag.public_send(name, 'foo', class: 'bar') ``` ## Case 2: Using `symbolize_keys` to prevent the following `ArgumentError` Original code: ```ruby content_tag(name, 'foo', {'class' => 'bar'}) ``` Auto-corrected code (before): ```ruby tag.public_send(name, 'foo', {'class' => 'bar'}) #=> `ArgumentError (wrong number of arguments (given 3, expected 1..2))` ``` Auto-corrected code (after): ```ruby tag.public_send(name, 'foo', {'class' => 'bar'}.symbolize_keys) ``` The `symbolize_keys` may not be needed, but for safe auto-correction it will be added if optional argument keys are not all symbols. ## Case 3: Using `o ? o.symbolize_keys : {}` to prevent the following `ArgumentError` Original code: ```ruby content_tag(name, 'foo', options) ``` Auto-corrected code (before): When the third argument is `nil`. ```ruby options = nil tag.public_send(name, 'foo', options) #=> `ArgumentError (wrong number of arguments (given 3, expected 1..2))` ``` Auto-corrected code (after): ```ruby tag.public_send(name, 'foo', options ? options.symbolize_keys : {}) ``` Guard with the empty hash in case the third argument is `nil`.
Follow up to rubocop#242. This PR allows `content_tag` when the first argument is a variable because `content_tag(name)` is simpler rather than `tag.public_send(name)`. When `public_send` is used, it becomes complicated as follows. First, `content_tag(name)` cannot be corrected to `tag.name` when the first argument is a variable. So use `public_send` and prevent the following errors in that case: ## Case 1: Using `public_send` to prevent the following `NoMethodError` Original code: ```ruby content_tag(name, 'foo', class: 'bar') ``` Auto-corrected code (before): ```ruby tag(name, 'foo', class: 'bar') #=> NoMethodError (undefined method `each_pair' for "foo":String) ``` Auto-corrected code (after): ```ruby tag.public_send(name, 'foo', class: 'bar') ``` ## Case 2: Using `symbolize_keys` to prevent the following `ArgumentError` Original code: ```ruby content_tag(name, 'foo', {'class' => 'bar'}) ``` Auto-corrected code (before): ```ruby tag.public_send(name, 'foo', {'class' => 'bar'}) #=> `ArgumentError (wrong number of arguments (given 3, expected 1..2))` ``` Auto-corrected code (after): ```ruby tag.public_send(name, 'foo', {'class' => 'bar'}.symbolize_keys) ``` The `symbolize_keys` may not be needed, but for safe auto-correction it will be added if optional argument keys are not all symbols. ## Case 3: Using `o ? o.symbolize_keys : {}` to prevent the following `ArgumentError` Original code: ```ruby content_tag(name, 'foo', options) ``` Auto-corrected code (before): When the third argument is `nil`. ```ruby options = nil tag.public_send(name, 'foo', options) #=> `ArgumentError (wrong number of arguments (given 3, expected 1..2))` ``` Auto-corrected code (after): ```ruby tag.public_send(name, 'foo', options ? options.symbolize_keys : {}) ``` Guard with the empty hash in case the third argument is `nil`.
Follow up to rubocop#242. This PR allows `content_tag` when the first argument is a variable because `content_tag(name)` is simpler rather than `tag.public_send(name)`. When `public_send` is used, it becomes complicated as follows. First, `content_tag(name)` cannot be corrected to `tag.name` when the first argument is a variable. So use `public_send` and prevent the following errors in that case: ## Case 1: Using `public_send` to prevent the following `NoMethodError` Original code: ```ruby content_tag(name, 'foo', class: 'bar') ``` Auto-corrected code (before): ```ruby tag(name, 'foo', class: 'bar') #=> NoMethodError (undefined method `each_pair' for "foo":String) ``` Auto-corrected code (after): ```ruby tag.public_send(name, 'foo', class: 'bar') ``` ## Case 2: Using `symbolize_keys` to prevent the following `ArgumentError` Original code: ```ruby content_tag(name, 'foo', {'class' => 'bar'}) ``` Auto-corrected code (before): ```ruby tag.public_send(name, 'foo', {'class' => 'bar'}) #=> `ArgumentError (wrong number of arguments (given 3, expected 1..2))` ``` Auto-corrected code (after): ```ruby tag.public_send(name, 'foo', {'class' => 'bar'}.symbolize_keys) ``` The `symbolize_keys` may not be needed, but for safe auto-correction it will be added if optional argument keys are not all symbols. ## Case 3: Using `o ? o.symbolize_keys : {}` to prevent the following `ArgumentError` Original code: ```ruby content_tag(name, 'foo', options) ``` Auto-corrected code (before): When the third argument is `nil`. ```ruby options = nil tag.public_send(name, 'foo', options) #=> `ArgumentError (wrong number of arguments (given 3, expected 1..2))` ``` Auto-corrected code (after): ```ruby tag.public_send(name, 'foo', options ? options.symbolize_keys : {}) ``` Guard with the empty hash in case the third argument is `nil`.
Follow up to rubocop#242. This PR allows `content_tag` when the first argument is a variable because `content_tag(name)` is simpler rather than `tag.public_send(name)`. When `public_send` is used, it becomes complicated as follows. First, `content_tag(name)` cannot be corrected to `tag.name` when the first argument is a variable. So use `public_send` and prevent the following errors in that case: ## Case 1: Using `public_send` to prevent the following `NoMethodError` Original code: ```ruby content_tag(name, 'foo', class: 'bar') ``` Auto-corrected code (before): ```ruby tag(name, 'foo', class: 'bar') #=> NoMethodError (undefined method `each_pair' for "foo":String) ``` Auto-corrected code (after): ```ruby tag.public_send(name, 'foo', class: 'bar') ``` ## Case 2: Using `symbolize_keys` to prevent the following `ArgumentError` Original code: ```ruby content_tag(name, 'foo', {'class' => 'bar'}) ``` Auto-corrected code (before): ```ruby tag.public_send(name, 'foo', {'class' => 'bar'}) #=> `ArgumentError (wrong number of arguments (given 3, expected 1..2))` ``` Auto-corrected code (after): ```ruby tag.public_send(name, 'foo', {'class' => 'bar'}.symbolize_keys) ``` The `symbolize_keys` may not be needed, but for safe auto-correction it will be added if optional argument keys are not all symbols. ## Case 3: Using `o ? o.symbolize_keys : {}` to prevent the following `ArgumentError` Original code: ```ruby content_tag(name, 'foo', options) ``` Auto-corrected code (before): When the third argument is `nil`. ```ruby options = nil tag.public_send(name, 'foo', options) #=> `ArgumentError (wrong number of arguments (given 3, expected 1..2))` ``` Auto-corrected code (after): ```ruby tag.public_send(name, 'foo', options ? options.symbolize_keys : {}) ``` Guard with the empty hash in case the third argument is `nil`.
Follow up to rubocop#242. This PR allows `content_tag` when the first argument is a variable because `content_tag(name)` is simpler rather than `tag.public_send(name)`. When `public_send` is used, it becomes complicated as follows. First, `content_tag(name)` cannot be corrected to `tag.name` when the first argument is a variable. So use `public_send` and prevent the following errors in that case: ## Case 1: Using `public_send` to prevent the following `NoMethodError` Original code: ```ruby content_tag(name, 'foo', class: 'bar') ``` Auto-corrected code (before): ```ruby tag(name, 'foo', class: 'bar') #=> NoMethodError (undefined method `each_pair' for "foo":String) ``` Auto-corrected code (after): ```ruby tag.public_send(name, 'foo', class: 'bar') ``` ## Case 2: Using `symbolize_keys` to prevent the following `ArgumentError` Original code: ```ruby content_tag(name, 'foo', {'class' => 'bar'}) ``` Auto-corrected code (before): ```ruby tag.public_send(name, 'foo', {'class' => 'bar'}) #=> `ArgumentError (wrong number of arguments (given 3, expected 1..2))` ``` Auto-corrected code (after): ```ruby tag.public_send(name, 'foo', {'class' => 'bar'}.symbolize_keys) ``` The `symbolize_keys` may not be needed, but for safe auto-correction it will be added if optional argument keys are not all symbols. ## Case 3: Using `o ? o.symbolize_keys : {}` to prevent the following `ArgumentError` Original code: ```ruby content_tag(name, 'foo', options) ``` Auto-corrected code (before): When the third argument is `nil`. ```ruby options = nil tag.public_send(name, 'foo', options) #=> `ArgumentError (wrong number of arguments (given 3, expected 1..2))` ``` Auto-corrected code (after): ```ruby tag.public_send(name, 'foo', options ? options.symbolize_keys : {}) ``` Guard with the empty hash in case the third argument is `nil`.
Follow up to rubocop#242. This PR allows `content_tag` when the first argument is a variable because `content_tag(name)` is simpler rather than `tag.public_send(name)`. When `public_send` is used, it becomes complicated as follows. First, `content_tag(name)` cannot be corrected to `tag.name` when the first argument is a variable. So use `public_send` and prevent the following errors in that case: ## Case 1: Using `public_send` to prevent the following `NoMethodError` Original code: ```ruby content_tag(name, 'foo', class: 'bar') ``` Auto-corrected code (before): ```ruby tag(name, 'foo', class: 'bar') #=> NoMethodError (undefined method `each_pair' for "foo":String) ``` Auto-corrected code (after): ```ruby tag.public_send(name, 'foo', class: 'bar') ``` ## Case 2: Using `symbolize_keys` to prevent the following `ArgumentError` Original code: ```ruby content_tag(name, 'foo', {'class' => 'bar'}) ``` Auto-corrected code (before): ```ruby tag.public_send(name, 'foo', {'class' => 'bar'}) #=> `ArgumentError (wrong number of arguments (given 3, expected 1..2))` ``` Auto-corrected code (after): ```ruby tag.public_send(name, 'foo', {'class' => 'bar'}.symbolize_keys) ``` The `symbolize_keys` may not be needed, but for safe auto-correction it will be added if optional argument keys are not all symbols. ## Case 3: Using `o ? o.symbolize_keys : {}` to prevent the following `ArgumentError` Original code: ```ruby content_tag(name, 'foo', options) ``` Auto-corrected code (before): When the third argument is `nil`. ```ruby options = nil tag.public_send(name, 'foo', options) #=> `ArgumentError (wrong number of arguments (given 3, expected 1..2))` ``` Auto-corrected code (after): ```ruby tag.public_send(name, 'foo', options ? options.symbolize_keys : {}) ``` Guard with the empty hash in case the third argument is `nil`.
This PR introduces a cop to check that
tag
is used instead ofcontent_tag
, becausecontent_tag
is legacy syntax.Before submitting the PR make sure the following are checked:
[Fix #issue-number]
(if the related issue exists).master
(if not - rebase it).and description in grammatically correct, complete sentences.
bundle exec rake default
. It executes all tests and RuboCop for itself, and generates the documentation.