Commit 8c56c779 authored by Peter Leitzen's avatar Peter Leitzen

Cop/StaticTranslationDefinition: Allow constant assignments for Structs

Prior this commit, Cop/StaticTranslationDefinition would flag the
following code:

    SomeClass = Struct.new do
      def text
        _('Some translated text')
      end
    end

This commit allow such style again so workarounds are not longer needed
like:

    Struct.new('SomeClass') do
      def text
        _('Some translated text')
      end
    end
parent d62e2dcd
...@@ -15,6 +15,10 @@ module RuboCop ...@@ -15,6 +15,10 @@ module RuboCop
(send _ :lambda) (send _ :lambda)
PATTERN PATTERN
def_node_matcher :struct_constant_assignment?, <<~PATTERN
(casgn _ _ `(const _ :Struct))
PATTERN
def on_send(node) def on_send(node)
return unless translation_method?(node) return unless translation_method?(node)
...@@ -27,7 +31,7 @@ module RuboCop ...@@ -27,7 +31,7 @@ module RuboCop
receiver, _ = *ancestor receiver, _ = *ancestor
break if lambda_node?(receiver) # translations defined in lambda nodes should be allowed break if lambda_node?(receiver) # translations defined in lambda nodes should be allowed
if constant_assignment?(ancestor) if constant_assignment?(ancestor) && !struct_constant_assignment?(ancestor)
add_offense(node, location: :expression) add_offense(node, location: :expression)
break break
......
...@@ -112,7 +112,7 @@ RSpec.describe RuboCop::Cop::StaticTranslationDefinition do ...@@ -112,7 +112,7 @@ RSpec.describe RuboCop::Cop::StaticTranslationDefinition do
} }
end end
CODE CODE
<<~CODE <<~CODE,
class MyClass class MyClass
def hello def hello
{ {
...@@ -121,6 +121,20 @@ RSpec.describe RuboCop::Cop::StaticTranslationDefinition do ...@@ -121,6 +121,20 @@ RSpec.describe RuboCop::Cop::StaticTranslationDefinition do
end end
end end
CODE CODE
<<~CODE,
SomeClass = Struct.new do
def text
_('Some translated text')
end
end
CODE
<<~CODE
Struct.new('SomeClass') do
def text
_('Some translated text')
end
end
CODE
] ]
end end
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment