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 ...@@ -8,11 +8,15 @@ module RuboCop
TRANSLATION_METHODS = %i[_ s_ n_].freeze TRANSLATION_METHODS = %i[_ s_ n_].freeze
def_node_matcher :translation_method?, <<~PATTERN def_node_matcher :translation_method?, <<~PATTERN
(send _ _ str*) (send _ _ str*)
PATTERN PATTERN
def_node_matcher :lambda_node?, <<~PATTERN def_node_matcher :lambda_node?, <<~PATTERN
(send _ :lambda) (send _ :lambda)
PATTERN
def_node_matcher :struct_constant_assignment?, <<~PATTERN
(casgn _ _ `(const _ :Struct))
PATTERN PATTERN
def on_send(node) def on_send(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