diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2020-05-20 17:34:42 +0300 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2020-05-20 17:34:42 +0300 |
commit | 9f46488805e86b1bc341ea1620b866016c2ce5ed (patch) | |
tree | f9748c7e287041e37d6da49e0a29c9511dc34768 /rubocop/cop/gitlab/json.rb | |
parent | dfc92d081ea0332d69c8aca2f0e745cb48ae5e6d (diff) |
Add latest changes from gitlab-org/gitlab@13-0-stable-ee
Diffstat (limited to 'rubocop/cop/gitlab/json.rb')
-rw-r--r-- | rubocop/cop/gitlab/json.rb | 36 |
1 files changed, 36 insertions, 0 deletions
diff --git a/rubocop/cop/gitlab/json.rb b/rubocop/cop/gitlab/json.rb new file mode 100644 index 00000000000..8c9027223aa --- /dev/null +++ b/rubocop/cop/gitlab/json.rb @@ -0,0 +1,36 @@ +# 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 |