Skip to content

Commit 41245e0

Browse files
authored
Merge pull request #544 from koic/fix_error_for_rails_content_tag
[Fix #543] Fix an error for `Rails/ContentTag`
2 parents 5e61e3d + 0e2e434 commit 41245e0

File tree

3 files changed

+21
-6
lines changed

3 files changed

+21
-6
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
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][])

lib/rubocop/cop/rails/content_tag.rb

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ def on_new_investigation
3333
end
3434

3535
def on_send(node)
36+
return unless node.receiver.nil?
37+
3638
first_argument = node.first_argument
3739
return if !first_argument ||
3840
allowed_argument?(first_argument) ||
@@ -41,12 +43,7 @@ def on_send(node)
4143
preferred_method = node.first_argument.value.to_s.underscore
4244
message = format(MSG, preferred_method: preferred_method, current_argument: first_argument.source)
4345

44-
add_offense(node, message: message) do |corrector|
45-
autocorrect(corrector, node, preferred_method)
46-
47-
@corrected_nodes ||= Set.new.compare_by_identity
48-
@corrected_nodes.add(node)
49-
end
46+
register_offense(node, message, preferred_method)
5047
end
5148

5249
private
@@ -63,6 +60,15 @@ def allowed_argument?(argument)
6360
allowed_name?(argument)
6461
end
6562

63+
def register_offense(node, message, preferred_method)
64+
add_offense(node, message: message) do |corrector|
65+
autocorrect(corrector, node, preferred_method)
66+
67+
@corrected_nodes ||= Set.new.compare_by_identity
68+
@corrected_nodes.add(node)
69+
end
70+
end
71+
6672
def autocorrect(corrector, node, preferred_method)
6773
range = correction_range(node)
6874

spec/rubocop/cop/rails/content_tag_spec.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,5 +168,13 @@
168168
RUBY
169169
end
170170
end
171+
172+
context 'when `tag` is not a top-level method (e.g. using intercom-ruby)' do
173+
it 'does not register an offense' do
174+
expect_no_offenses(<<~RUBY)
175+
intercom.tags.tag(foo: 'foo', bar: 'bar')
176+
RUBY
177+
end
178+
end
171179
end
172180
end

0 commit comments

Comments
 (0)