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.

52 lines
1.4 KiB
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
2023-01-13 00:05:48 +05:30
Prefer `Gitlab::Json` over calling `JSON` directly. See https://docs.gitlab.com/ee/development/json.html
2020-05-24 23:13:21 +05:30
EOL
2023-01-13 00:05:48 +05:30
AVAILABLE_METHODS = %i[parse parse! load decode dump generate encode pretty_generate].to_set.freeze
2020-05-24 23:13:21 +05:30
def_node_matcher :json_node?, <<~PATTERN
2023-01-13 00:05:48 +05:30
(send (const {nil? | (const nil? :ActiveSupport)} :JSON) $_ $...)
2020-05-24 23:13:21 +05:30
PATTERN
def on_send(node)
2023-01-13 00:05:48 +05:30
method_name, arg_source = match_node(node)
return unless method_name
2020-05-24 23:13:21 +05:30
2022-10-11 01:57:18 +05:30
add_offense(node) do |corrector|
2023-01-13 00:05:48 +05:30
replacement = "#{cbased(node)}Gitlab::Json.#{method_name}(#{arg_source})"
2020-05-24 23:13:21 +05:30
corrector.replace(node.source_range, replacement)
end
end
2023-01-13 00:05:48 +05:30
private
def match_node(node)
method_name, arg_nodes = json_node?(node)
# Only match if the method is implemented by Gitlab::Json
if method_name && AVAILABLE_METHODS.include?(method_name)
return [method_name, arg_nodes.map(&:source).join(", ")]
end
nil
end
def cbased(node)
return unless %r{/ee/}.match?(node.location.expression.source_buffer.name)
"::"
end
2020-05-24 23:13:21 +05:30
end
end
end
end