Commit fd40e9ec authored by Dylan Griffith's avatar Dylan Griffith

Merge branch '207745-fix-cop-inject-enterprise-edition-modul' into 'master'

Modify "Remove Rubocop::Cop::InjectEnterpriseEditionModule" to allow multiple injects

Closes #207745

See merge request gitlab-org/gitlab!32747
parents 783e44c2 48fe9b20
......@@ -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)
......@@ -2,9 +2,8 @@
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.
# Cop that blacklists the injecting of EE specific modules before any lines which are not already injecting another module.
# It allows multiple module 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,12 @@ 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
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
def ee_const?(node)
line = node.location.expression.source_line
......@@ -44,15 +45,11 @@ module RuboCop
line = node.location.line
buffer = node.location.expression.source_buffer
last_line = buffer.last_line
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]) }
# 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")
last_line_content = buffer.source.split("\n")[-1]
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)
......
......@@ -170,6 +170,20 @@ describe RuboCop::Cop::InjectEnterpriseEditionModule do
SOURCE
end
it 'does not flag the use of `prepend_if_ee EE` as long as all injections are at the end of the file' do
expect_no_offenses(<<~SOURCE)
class Foo
end
Foo.include_if_ee('EE::Foo')
Foo.prepend_if_ee('EE::Foo')
Foo.include(Bar)
# comment on prepending Bar
Foo.prepend(Bar)
SOURCE
end
it 'autocorrects offenses by just disabling the Cop' do
source = <<~SOURCE
class Foo
......
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