debian-mirror-gitlab/rubocop/cop/gitlab/json.rb

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

33 lines
845 B
Ruby
Raw Normal View History

2020-05-24 23:13:21 +05:30
# frozen_string_literal: true
module RuboCop
module Cop
module Gitlab
2022-10-11 01:57:18 +05:30
class Json < RuboCop::Cop::Base
extend RuboCop::Cop::AutoCorrector
2021-03-11 19:13:27 +05:30
MSG = <<~EOL
2020-05-24 23:13:21 +05:30
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
2021-10-27 15:23:28 +05:30
(send (const {nil? | (const nil? :ActiveSupport)} :JSON)...)
2020-05-24 23:13:21 +05:30
PATTERN
def on_send(node)
2022-10-11 01:57:18 +05:30
return unless json_node?(node)
2020-05-24 23:13:21 +05:30
2022-10-11 01:57:18 +05:30
add_offense(node) do |corrector|
_, method_name, *arg_nodes = *node
2020-05-24 23:13:21 +05:30
2022-10-11 01:57:18 +05:30
replacement = "Gitlab::Json.#{method_name}(#{arg_nodes.map(&:source).join(', ')})"
2020-05-24 23:13:21 +05:30
corrector.replace(node.source_range, replacement)
end
end
end
end
end
end