Skip to content

Commit

Permalink
Merge branch 'fix-rubocop-ee-module-injection-detection' into 'master'
Browse files Browse the repository at this point in the history
Fix detecting nested EE constants in RuboCop

See merge request gitlab-org/gitlab-ce!24430
  • Loading branch information
rymai committed Jan 16, 2019
2 parents e058ab1 + 9722158 commit 941f95e
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 3 deletions.
10 changes: 7 additions & 3 deletions rubocop/cop/inject_enterprise_edition_module.rb
Expand Up @@ -11,9 +11,13 @@ class InjectEnterpriseEditionModule < RuboCop::Cop::Cop

METHODS = Set.new(%i[include extend prepend]).freeze

def_node_matcher :ee_const?, <<~PATTERN
(const (const _ :EE) _)
PATTERN
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`.
line.match?(/(\s|\()(::)?EE::/)
end

def on_send(node)
return unless METHODS.include?(node.children[1])
Expand Down
35 changes: 35 additions & 0 deletions spec/rubocop/cop/inject_enterprise_edition_module_spec.rb
Expand Up @@ -19,6 +19,41 @@ class Foo
SOURCE
end

it 'does not flag the use of `prepend EEFoo` in the middle of a file' do
expect_no_offenses(<<~SOURCE)
class Foo
prepend EEFoo
end
SOURCE
end

it 'flags the use of `prepend EE::Foo::Bar` in the middle of a file' do
expect_offense(<<~SOURCE)
class Foo
prepend EE::Foo::Bar
^^^^^^^^^^^^^^^^^^^^ Injecting EE modules must be done on the last line of this file, outside of any class or module definitions
end
SOURCE
end

it 'flags the use of `prepend(EE::Foo::Bar)` in the middle of a file' do
expect_offense(<<~SOURCE)
class Foo
prepend(EE::Foo::Bar)
^^^^^^^^^^^^^^^^^^^^^ Injecting EE modules must be done on the last line of this file, outside of any class or module definitions
end
SOURCE
end

it 'flags the use of `prepend EE::Foo::Bar::Baz` in the middle of a file' do
expect_offense(<<~SOURCE)
class Foo
prepend EE::Foo::Bar::Baz
^^^^^^^^^^^^^^^^^^^^^^^^^ Injecting EE modules must be done on the last line of this file, outside of any class or module definitions
end
SOURCE
end

it 'flags the use of `prepend ::EE` in the middle of a file' do
expect_offense(<<~SOURCE)
class Foo
Expand Down

0 comments on commit 941f95e

Please sign in to comment.