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

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

91 lines
3 KiB
Ruby
Raw Normal View History

2019-02-15 15:39:39 +05:30
# frozen_string_literal: true
module RuboCop
module Cop
2021-06-08 01:23:25 +05:30
# Cop that blacklists the injecting of extension specific modules before any lines which are not already injecting another module.
2020-06-23 00:09:42 +05:30
# It allows multiple module injections as long as they're all at the end.
2019-02-15 15:39:39 +05:30
class InjectEnterpriseEditionModule < RuboCop::Cop::Cop
2021-06-08 01:23:25 +05:30
INVALID_LINE = 'Injecting extension modules must be done on the last line of this file' \
2019-10-12 21:52:04 +05:30
', outside of any class or module definitions'
2019-02-15 15:39:39 +05:30
2019-10-12 21:52:04 +05:30
DISALLOWED_METHOD =
2021-06-08 01:23:25 +05:30
'EE modules must be injected using `include_mod_with`, `extend_mod_with`, or `prepend_mod_with`'
2019-10-12 21:52:04 +05:30
2021-06-08 01:23:25 +05:30
INVALID_ARGUMENT = 'extension modules to inject must be specified as a String'
2019-10-12 21:52:04 +05:30
CHECK_LINE_METHODS =
2021-06-08 01:23:25 +05:30
Set.new(%i[include_mod_with extend_mod_with prepend_mod_with]).freeze
2019-10-12 21:52:04 +05:30
DISALLOW_METHODS = Set.new(%i[include extend prepend]).freeze
2019-02-15 15:39:39 +05:30
2020-06-23 00:09:42 +05:30
COMMENT_OR_EMPTY_LINE = /^\s*(#.*|$)/.freeze
CHECK_LINE_METHODS_REGEXP = Regexp.union((CHECK_LINE_METHODS + DISALLOW_METHODS).map(&:to_s) + [COMMENT_OR_EMPTY_LINE]).freeze
2019-03-02 22:35:43 +05:30
def ee_const?(node)
line = node.location.expression.source_line
# We use `match?` here instead of RuboCop's AST matching, as this makes
# it far easier to handle nested constants such as `EE::Foo::Bar::Baz`.
2019-12-04 20:38:33 +05:30
line.match?(/(\s|\()('|")?(::)?(QA::)?EE::/)
2019-03-02 22:35:43 +05:30
end
2019-02-15 15:39:39 +05:30
def on_send(node)
2019-10-12 21:52:04 +05:30
return unless check_method?(node)
2019-02-15 15:39:39 +05:30
2019-10-12 21:52:04 +05:30
if DISALLOW_METHODS.include?(node.children[1])
add_offense(node, message: DISALLOWED_METHOD)
else
verify_line_number(node)
verify_argument_type(node)
end
end
def verify_line_number(node)
2019-02-15 15:39:39 +05:30
line = node.location.line
buffer = node.location.expression.source_buffer
last_line = buffer.last_line
2020-06-23 00:09:42 +05:30
lines = buffer.source.split("\n")
# We allow multiple includes, extends and prepends as long as they're all at the end.
allowed_line = (line...last_line).all? { |i| CHECK_LINE_METHODS_REGEXP.match?(lines[i - 1]) }
2019-02-15 15:39:39 +05:30
2020-06-23 00:09:42 +05:30
if allowed_line
2020-05-24 23:13:21 +05:30
ignore_node(node)
elsif line < last_line
add_offense(node, message: INVALID_LINE)
end
2019-10-12 21:52:04 +05:30
end
def verify_argument_type(node)
argument = node.children[2]
return if argument.str_type?
add_offense(argument, message: INVALID_ARGUMENT)
end
def check_method?(node)
name = node.children[1]
2021-06-08 01:23:25 +05:30
if DISALLOW_METHODS.include?(name)
2019-10-12 21:52:04 +05:30
ee_const?(node.children[2])
else
2021-06-08 01:23:25 +05:30
CHECK_LINE_METHODS.include?(name)
2019-10-12 21:52:04 +05:30
end
2019-02-15 15:39:39 +05:30
end
# Automatically correcting these offenses is not always possible, as
# sometimes code needs to be refactored to make this work. As such, we
# only allow developers to easily blacklist existing offenses.
def autocorrect(node)
lambda do |corrector|
corrector.insert_after(
node.source_range,
" # rubocop: disable #{cop_name}"
)
end
end
end
end
end