Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
G
gitlab-ce
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
1
Merge Requests
1
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
nexedi
gitlab-ce
Commits
a711cbf1
Commit
a711cbf1
authored
Sep 09, 2020
by
Adam Hegyi
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add static translation check on ivars
parent
53a3e898
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
43 additions
and
0 deletions
+43
-0
rubocop/cop/static_translation_definition.rb
rubocop/cop/static_translation_definition.rb
+18
-0
spec/rubocop/cop/static_translation_definition_spec.rb
spec/rubocop/cop/static_translation_definition_spec.rb
+25
-0
No files found.
rubocop/cop/static_translation_definition.rb
View file @
a711cbf1
...
...
@@ -21,6 +21,8 @@ module RuboCop
method_name
=
node
.
children
[
1
]
return
unless
TRANSLATION_METHODS
.
include?
(
method_name
)
translation_memoized
=
false
node
.
each_ancestor
do
|
ancestor
|
receiver
,
_
=
*
ancestor
break
if
lambda_node?
(
receiver
)
# translations defined in lambda nodes should be allowed
...
...
@@ -30,6 +32,14 @@ module RuboCop
break
end
translation_memoized
=
true
if
memoization?
(
ancestor
)
if
translation_memoized
&&
class_method_definition?
(
ancestor
)
add_offense
(
node
,
location: :expression
)
break
end
end
end
...
...
@@ -38,6 +48,14 @@ module RuboCop
def
constant_assignment?
(
node
)
node
.
type
==
:casgn
end
def
memoization?
(
node
)
node
.
type
==
:or_asgn
end
def
class_method_definition?
(
node
)
node
.
type
==
:defs
end
end
end
end
spec/rubocop/cop/static_translation_definition_spec.rb
View file @
a711cbf1
...
...
@@ -38,6 +38,17 @@ RSpec.describe RuboCop::Cop::StaticTranslationDefinition, type: :rubocop do
[
'A = _("a")'
,
'_("a")'
,
1
],
[
'B = s_("b")'
,
's_("b")'
,
1
],
[
'C = n_("c")'
,
'n_("c")'
,
1
],
[
<<~
CODE
,
class MyClass
def self.translations
@cache ||= { hello: _("hello") }
end
end
CODE
'_("hello")'
,
3
],
[
<<~
CODE
,
module MyModule
...
...
@@ -78,6 +89,20 @@ RSpec.describe RuboCop::Cop::StaticTranslationDefinition, type: :rubocop do
'CONSTANT_1 = __("a")'
,
'CONSTANT_2 = s__("a")'
,
'CONSTANT_3 = n__("a")'
,
<<~
CODE
,
class MyClass
def self.method
@cache ||= { hello: -> { _("hello") } }
end
end
CODE
<<~
CODE
,
class MyClass
def method
@cache ||= { hello: _("hello") }
end
end
CODE
<<~
CODE
,
def method
s_('a')
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment