Skip to content

Commit

Permalink
Merge pull request rubocop#327 from Jetbuilt/content-tag-with-content
Browse files Browse the repository at this point in the history
Fixes auto-correct of content_tag with hyphenated tag name
  • Loading branch information
koic committed Aug 14, 2020
2 parents e68327c + 9c1a40e commit c22496c
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 5 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Expand Up @@ -13,6 +13,7 @@
* [#315](https://github.com/rubocop-hq/rubocop-rails/pull/315): Allow to use frozen scope for `Rails/UniqueValidationWithoutIndex`. ([@krim][])
* [#313](https://github.com/rubocop-hq/rubocop-rails/pull/313): Fix `Rails/ActiveRecordCallbacksOrder` to preserve the original callback execution order. ([@eugeneius][])
* [#319](https://github.com/rubocop-hq/rubocop-rails/issues/319): Fix a false positive for `Rails/Inquiry` when `#inquiry`'s receiver is a variable. ([@koic][])
* [#327](https://github.com/rubocop-hq/rubocop-rails/pull/327): Fix `Rails/ContentTag` autocorrect to handle html5 tag names with hyphens. ([@jaredmoody][])

### Changes

Expand Down Expand Up @@ -266,3 +267,4 @@
[@krim]: https://github.com/krim
[@philcoggins]: https://github.com/philcoggins
[@kunitoo]: https://github.com/kunitoo
[@jaredmoody]: https://github.com/jaredmoody
4 changes: 2 additions & 2 deletions lib/rubocop/cop/rails/content_tag.rb
Expand Up @@ -43,7 +43,7 @@ def autocorrect(node)
range = correction_range(node)

rest_args = node.arguments.drop(1)
replacement = "tag.#{node.first_argument.value}(#{rest_args.map(&:source).join(', ')})"
replacement = "tag.#{node.first_argument.value.to_s.underscore}(#{rest_args.map(&:source).join(', ')})"

corrector.replace(range, replacement)
else
Expand All @@ -57,7 +57,7 @@ def autocorrect(node)
def method_name?(node)
return false unless node.str_type? || node.sym_type?

/^[a-zA-Z_][a-zA-Z_0-9]*$/.match?(node.value)
/^[a-zA-Z_][a-zA-Z_\-0-9]*$/.match?(node.value)
end

def correction_range(node)
Expand Down
6 changes: 3 additions & 3 deletions spec/rubocop/cop/rails/content_tag_spec.rb
Expand Up @@ -83,11 +83,11 @@

it 'corrects an offence when first argument is non-identifier string' do
expect_offense(<<~RUBY)
content_tag('foo-bar')
^^^^^^^^^^^^^^^^^^^^^^ Use `tag` instead of `content_tag`.
content_tag('foo-bar', 'baz', class: 'strong')
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use `tag` instead of `content_tag`.
RUBY
expect_correction(<<~RUBY)
tag('foo-bar')
tag.foo_bar('baz', class: 'strong')
RUBY
end

Expand Down

0 comments on commit c22496c

Please sign in to comment.