Skip to content

Commit

Permalink
Merge pull request #544 from koic/fix_error_for_rails_content_tag
Browse files Browse the repository at this point in the history
[Fix #543] Fix an error for `Rails/ContentTag`
  • Loading branch information
koic authored Sep 14, 2021
2 parents 5e61e3d + 0e2e434 commit 41245e0
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 6 deletions.
1 change: 1 addition & 0 deletions changelog/fix_error_for_rails_content_tag.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* [#543](https://github.com/rubocop/rubocop-rails/issues/543): Fix an error for `Rails/ContentTag` when `tag` is not a top-level method. ([@koic][])
18 changes: 12 additions & 6 deletions lib/rubocop/cop/rails/content_tag.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ def on_new_investigation
end

def on_send(node)
return unless node.receiver.nil?

first_argument = node.first_argument
return if !first_argument ||
allowed_argument?(first_argument) ||
Expand All @@ -41,12 +43,7 @@ def on_send(node)
preferred_method = node.first_argument.value.to_s.underscore
message = format(MSG, preferred_method: preferred_method, current_argument: first_argument.source)

add_offense(node, message: message) do |corrector|
autocorrect(corrector, node, preferred_method)

@corrected_nodes ||= Set.new.compare_by_identity
@corrected_nodes.add(node)
end
register_offense(node, message, preferred_method)
end

private
Expand All @@ -63,6 +60,15 @@ def allowed_argument?(argument)
allowed_name?(argument)
end

def register_offense(node, message, preferred_method)
add_offense(node, message: message) do |corrector|
autocorrect(corrector, node, preferred_method)

@corrected_nodes ||= Set.new.compare_by_identity
@corrected_nodes.add(node)
end
end

def autocorrect(corrector, node, preferred_method)
range = correction_range(node)

Expand Down
8 changes: 8 additions & 0 deletions spec/rubocop/cop/rails/content_tag_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -168,5 +168,13 @@
RUBY
end
end

context 'when `tag` is not a top-level method (e.g. using intercom-ruby)' do
it 'does not register an offense' do
expect_no_offenses(<<~RUBY)
intercom.tags.tag(foo: 'foo', bar: 'bar')
RUBY
end
end
end
end

0 comments on commit 41245e0

Please sign in to comment.