2021-03-11 19:13:27 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2020-03-13 15:44:24 +05:30
|
|
|
module RuboCop
|
|
|
|
module Cop
|
|
|
|
module Gitlab
|
2022-10-11 01:57:18 +05:30
|
|
|
# Detects the use of `.keys.first` or `.values.first` and suggests a
|
|
|
|
# change to `.each_key.first` or `.each_value.first`. This reduces
|
|
|
|
# memory usage and execution time.
|
|
|
|
#
|
|
|
|
# @example
|
|
|
|
#
|
|
|
|
# # bad
|
|
|
|
#
|
|
|
|
# hash.keys.first
|
|
|
|
# hash.values.first
|
|
|
|
#
|
|
|
|
# # good
|
|
|
|
#
|
|
|
|
# hash.each_key.first
|
|
|
|
# hash.each_value.first
|
|
|
|
#
|
|
|
|
class KeysFirstAndValuesFirst < RuboCop::Cop::Base
|
|
|
|
extend RuboCop::Cop::AutoCorrector
|
|
|
|
|
|
|
|
MSG = 'Prefer `.%{autocorrect_method}.first` over `.%{current_method}.first`. ' \
|
|
|
|
'This reduces memory usage and execution time.'
|
|
|
|
|
|
|
|
AUTOCORRECT_METHOD = {
|
|
|
|
keys: 'each_key',
|
|
|
|
values: 'each_value'
|
|
|
|
}.freeze
|
|
|
|
|
|
|
|
def_node_matcher :keys_or_values_first, <<~PATTERN
|
|
|
|
(send (send ({send const hash lvar} ...) ${:keys :values}) :first)
|
|
|
|
PATTERN
|
2020-03-13 15:44:24 +05:30
|
|
|
|
|
|
|
def on_send(node)
|
2022-10-11 01:57:18 +05:30
|
|
|
current_method = keys_or_values_first(node)
|
|
|
|
return unless current_method
|
|
|
|
|
|
|
|
autocorrect_method = AUTOCORRECT_METHOD.fetch(current_method)
|
|
|
|
msg = format(MSG, autocorrect_method: autocorrect_method, current_method: current_method)
|
2020-03-13 15:44:24 +05:30
|
|
|
|
2022-10-11 01:57:18 +05:30
|
|
|
add_offense(node.loc.selector, message: msg) do |corrector|
|
|
|
|
replacement = "#{autocorrect_expression(node)}.#{autocorrect_method}.first"
|
|
|
|
corrector.replace(node, replacement)
|
2020-03-13 15:44:24 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-10-11 01:57:18 +05:30
|
|
|
private
|
2020-03-13 15:44:24 +05:30
|
|
|
|
2022-10-11 01:57:18 +05:30
|
|
|
def autocorrect_expression(node)
|
|
|
|
node.receiver.receiver.source
|
2020-03-13 15:44:24 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|