Welcome to mirror list, hosted at ThFree Co, Russian Federation.

gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
Diffstat (limited to 'rubocop/cop/inject_enterprise_edition_module.rb')
-rw-r--r--rubocop/cop/inject_enterprise_edition_module.rb23
1 files changed, 10 insertions, 13 deletions
diff --git a/rubocop/cop/inject_enterprise_edition_module.rb b/rubocop/cop/inject_enterprise_edition_module.rb
index 7edce94eaee..16e4b647265 100644
--- a/rubocop/cop/inject_enterprise_edition_module.rb
+++ b/rubocop/cop/inject_enterprise_edition_module.rb
@@ -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)