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
......@@ -8,11 +8,15 @@ module RuboCop
TRANSLATION_METHODS = %i[_ s_ n_].freeze
def_node_matcher :translation_method?, <<~PATTERN
(send _ _ str*)
(send _ _ str*)
PATTERN
def_node_matcher :lambda_node?, <<~PATTERN
(send _ :lambda)
(send _ :lambda)
PATTERN
def_node_matcher :struct_constant_assignment?, <<~PATTERN
(casgn _ _ `(const _ :Struct))
PATTERN
def on_send(node)
......@@ -27,7 +31,7 @@ module RuboCop
receiver, _ = *ancestor
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)
break
......
......@@ -112,7 +112,7 @@ RSpec.describe RuboCop::Cop::StaticTranslationDefinition do
}
end
CODE
<<~CODE
<<~CODE,
class MyClass
def hello
{
......@@ -121,6 +121,20 @@ RSpec.describe RuboCop::Cop::StaticTranslationDefinition do
end
end
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
......
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