From 9f46488805e86b1bc341ea1620b866016c2ce5ed Mon Sep 17 00:00:00 2001 From: GitLab Bot Date: Wed, 20 May 2020 14:34:42 +0000 Subject: Add latest changes from gitlab-org/gitlab@13-0-stable-ee --- rubocop/cop/gitlab/json.rb | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 rubocop/cop/gitlab/json.rb (limited to 'rubocop/cop/gitlab/json.rb') 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 -- cgit v1.2.3