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

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

41 lines
1.1 KiB
Ruby
Raw Normal View History

2019-12-21 20:55:43 +05:30
# frozen_string_literal: true
module RuboCop
module Cop
module Gitlab
# Cop that encourages usage of inherit=false for 2nd argument when using const_get.
#
# See https://gitlab.com/gitlab-org/gitlab/issues/27678
2022-10-11 01:57:18 +05:30
class ConstGetInheritFalse < RuboCop::Cop::Base
extend RuboCop::Cop::AutoCorrector
2019-12-21 20:55:43 +05:30
MSG = 'Use inherit=false when using const_get.'
def_node_matcher :const_get?, <<~PATTERN
(send _ :const_get ...)
PATTERN
def on_send(node)
return unless const_get?(node)
return if second_argument(node)&.false_type?
2022-10-11 01:57:18 +05:30
add_offense(node.loc.selector) do |corrector|
2019-12-21 20:55:43 +05:30
if arg = second_argument(node)
corrector.replace(arg.source_range, 'false')
else
first_argument = node.arguments[0]
corrector.insert_after(first_argument.source_range, ', false')
end
end
end
private
def second_argument(node)
node.arguments[1]
end
end
end
end
end