Commit c5cb8ce8 authored by Stan Hu's avatar Stan Hu

Merge branch 'pl-rubocop-struct-static-translation-definition' into 'master'

Cop/StaticTranslationDefinition: Allow constant assignments for Structs

See merge request gitlab-org/gitlab!74823
parents 714a1296 8c56c779
......@@ -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