Commit 62cd8ed2 authored by nmilojevic1's avatar nmilojevic1

Allow multiple prepends if they're all at the end

parent b7524f8f
......@@ -249,9 +249,7 @@ module Projects
end
end
# rubocop: disable Cop/InjectEnterpriseEditionModule
Projects::CreateService.prepend_if_ee('EE::Projects::CreateService')
# rubocop: enable Cop/InjectEnterpriseEditionModule
# Measurable should be at the bottom of the ancestor chain, so it will measure execution of EE::Projects::CreateService as well
Projects::CreateService.prepend(Measurable)
......@@ -149,9 +149,7 @@ module Projects
end
end
# rubocop: disable Cop/InjectEnterpriseEditionModule
Projects::ImportService.prepend_if_ee('EE::Projects::ImportService')
# rubocop: enable Cop/InjectEnterpriseEditionModule
# Measurable should be at the bottom of the ancestor chain, so it will measure execution of EE::Projects::ImportService as well
Projects::ImportService.prepend(Measurable)
......@@ -3,8 +3,7 @@
module RuboCop
module Cop
# Cop that blacklists the injecting of EE specific modules anywhere but on
# the last line of a file. Injecting a module in the middle of a file will
# cause merge conflicts, while placing it on the last line will not.
# the last line of a file. It allows multiple EE injections as long as they're all at the end.
class InjectEnterpriseEditionModule < RuboCop::Cop::Cop
INVALID_LINE = 'Injecting EE modules must be done on the last line of this file' \
', outside of any class or module definitions'
......@@ -17,10 +16,10 @@ module RuboCop
CHECK_LINE_METHODS =
Set.new(%i[include_if_ee extend_if_ee prepend_if_ee]).freeze
CHECK_LINE_METHODS_REGEXP = Regexp.union(CHECK_LINE_METHODS.map(&:to_s)).freeze
DISALLOW_METHODS = Set.new(%i[include extend prepend]).freeze
CHECK_LINE_METHODS_REGEXP = Regexp.union((CHECK_LINE_METHODS + DISALLOW_METHODS).map(&:to_s) + [/^\s*(#.*|$)/]).freeze
def ee_const?(node)
line = node.location.expression.source_line
......@@ -45,14 +44,18 @@ module RuboCop
buffer = node.location.expression.source_buffer
last_line = buffer.last_line
# Parser treats the final newline (if present) as a separate line,
# meaning that a simple `line < last_line` would yield true even though
# the expression is the last line _of code_.
last_line -= 1 if buffer.source.end_with?("\n")
# We allow multiple includes, extends and prepends as long as they're all ath the end.
allowed_line = true
index_line = last_line - line + 1
content = buffer.source.split("\n")
last_line_content = buffer.source.split("\n")[-1]
while index_line > 0 && allowed_line
line_content = content[-index_line]
allowed_line = CHECK_LINE_METHODS_REGEXP.match?(line_content)
index_line -= 1
end
if CHECK_LINE_METHODS_REGEXP.match?(last_line_content)
if allowed_line
ignore_node(node)
elsif line < last_line
add_offense(node, message: INVALID_LINE)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment