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
e225b5c1
Commit
e225b5c1
authored
Apr 07, 2020
by
Robert May
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add JSON wrapper cop
parent
2d2ce63d
Changes
3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
82 additions
and
0 deletions
+82
-0
.rubocop.yml
.rubocop.yml
+7
-0
rubocop/cop/gitlab/json.rb
rubocop/cop/gitlab/json.rb
+36
-0
spec/rubocop/cop/gitlab/json_spec.rb
spec/rubocop/cop/gitlab/json_spec.rb
+39
-0
No files found.
.rubocop.yml
View file @
e225b5c1
...
...
@@ -211,6 +211,13 @@ Gitlab/HTTParty:
-
'
spec/**/*'
-
'
ee/spec/**/*'
Gitlab/Json
:
Enabled
:
false
Exclude
:
-
'
db/**/*'
-
'
qa/**/*'
-
'
scripts/**/*'
GitlabSecurity/PublicSend
:
Enabled
:
true
Exclude
:
...
...
rubocop/cop/gitlab/json.rb
0 → 100644
View file @
e225b5c1
# frozen_string_literal: true
module
RuboCop
module
Cop
module
Gitlab
class
Json
<
RuboCop
::
Cop
::
Cop
MSG_SEND
=
<<~
EOL
.
freeze
Avoid calling `JSON` directly. Instead, use the `Gitlab::Json`
wrapper. This allows us to alter the JSON parser being used.
EOL
def_node_matcher
:json_node?
,
<<~
PATTERN
(send (const nil? :JSON)...)
PATTERN
def
on_send
(
node
)
add_offense
(
node
,
location: :expression
,
message:
MSG_SEND
)
if
json_node?
(
node
)
end
def
autocorrect
(
node
)
autocorrect_json_node
(
node
)
end
def
autocorrect_json_node
(
node
)
_
,
method_name
,
*
arg_nodes
=
*
node
replacement
=
"Gitlab::Json.
#{
method_name
}
(
#{
arg_nodes
.
map
(
&
:source
).
join
(
', '
)
}
)"
lambda
do
|
corrector
|
corrector
.
replace
(
node
.
source_range
,
replacement
)
end
end
end
end
end
end
spec/rubocop/cop/gitlab/json_spec.rb
0 → 100644
View file @
e225b5c1
# frozen_string_literal: true
require
'spec_helper'
require
'rubocop'
require
'rubocop/rspec/support'
require_relative
'../../../../rubocop/cop/gitlab/json'
describe
RuboCop
::
Cop
::
Gitlab
::
Json
do
include
CopHelper
subject
(
:cop
)
{
described_class
.
new
}
shared_examples
(
'registering call offense'
)
do
|
options
|
let
(
:offending_lines
)
{
options
[
:offending_lines
]
}
it
'registers an offense when the class calls JSON'
do
inspect_source
(
source
)
aggregate_failures
do
expect
(
cop
.
offenses
.
size
).
to
eq
(
offending_lines
.
size
)
expect
(
cop
.
offenses
.
map
(
&
:line
)).
to
eq
(
offending_lines
)
end
end
end
context
'when JSON is called'
do
it_behaves_like
'registering call offense'
,
offending_lines:
[
3
]
do
let
(
:source
)
do
<<~
RUBY
class Foo
def bar
JSON.parse('{ "foo": "bar" }')
end
end
RUBY
end
end
end
end
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