Commit cd68bb18 authored by Lin Jen-Shin's avatar Lin Jen-Shin

Simply the implementation and fix tests

We can much simply this now because call sites are no longer using long
form or other aliased names.

For `RuboCop::Cop::InjectEnterpriseEditionModule`, we change it that:

* `prepend_mod_with` must be used for extension modules, therefore
  we don't need to allow using it on regular modules.
parent d503dbe8
...@@ -51,4 +51,4 @@ class Groups::AutocompleteSourcesController < Groups::ApplicationController ...@@ -51,4 +51,4 @@ class Groups::AutocompleteSourcesController < Groups::ApplicationController
# rubocop: enable CodeReuse/ActiveRecord # rubocop: enable CodeReuse/ActiveRecord
end end
Groups::AutocompleteSourcesController.prepend_if_ee('EE::Groups::AutocompleteSourcesController') Groups::AutocompleteSourcesController.prepend_mod
...@@ -139,4 +139,4 @@ module Nav ...@@ -139,4 +139,4 @@ module Nav
end end
end end
Nav::TopNavHelper.prepend_ee_mod Nav::TopNavHelper.prepend_mod
...@@ -105,4 +105,4 @@ module Terraform ...@@ -105,4 +105,4 @@ module Terraform
end end
end end
Terraform::State.prepend_if_ee('EE::Terraform::State') Terraform::State.prepend_mod
...@@ -46,4 +46,4 @@ module Groups ...@@ -46,4 +46,4 @@ module Groups
end end
end end
Groups::AutocompleteService.prepend_if_ee('EE::Groups::AutocompleteService') Groups::AutocompleteService.prepend_mod
...@@ -145,4 +145,4 @@ module Namespaces ...@@ -145,4 +145,4 @@ module Namespaces
end end
end end
Namespaces::InProductMarketingEmailsService.prepend_ee_mod Namespaces::InProductMarketingEmailsService.prepend_mod
...@@ -3,47 +3,40 @@ ...@@ -3,47 +3,40 @@
require 'active_support/inflector' require 'active_support/inflector'
module InjectEnterpriseEditionModule module InjectEnterpriseEditionModule
def prepend_mod_with(constant_with_prefix, namespace: Object, with_descendants: false) def prepend_mod_with(constant_name, namespace: Object, with_descendants: false)
prepend_mod_for( each_extension_for(constant_name, namespace) do |constant|
constant_without_prefix(constant_with_prefix), prepend_module(constant, with_descendants)
namespace: namespace, end
with_descendants: with_descendants)
end end
def extend_mod_with(constant_with_prefix, namespace: Object) def extend_mod_with(constant_name, namespace: Object)
each_extension_for( each_extension_for(
constant_without_prefix(constant_with_prefix), constant_name,
namespace, namespace,
&method(:extend)) &method(:extend))
end end
def include_mod_with(constant_with_prefix, namespace: Object) def include_mod_with(constant_name, namespace: Object)
each_extension_for( each_extension_for(
constant_without_prefix(constant_with_prefix), constant_name,
namespace, namespace,
&method(:include)) &method(:include))
end end
def prepend_mod(with_descendants: false) def prepend_mod(with_descendants: false)
prepend_mod_for(name, with_descendants: with_descendants) prepend_mod_with(name, with_descendants: with_descendants) # rubocop: disable Cop/InjectEnterpriseEditionModule
end end
def extend_mod def extend_mod
each_extension_for(name, Object, &method(:extend)) extend_mod_with(name) # rubocop: disable Cop/InjectEnterpriseEditionModule
end end
def include_mod def include_mod
each_extension_for(name, Object, &method(:include)) include_mod_with(name) # rubocop: disable Cop/InjectEnterpriseEditionModule
end end
private private
def prepend_mod_for(constant_name, namespace: Object, with_descendants: false)
each_extension_for(constant_name, namespace) do |constant|
prepend_module(constant, with_descendants)
end
end
def prepend_module(mod, with_descendants) def prepend_module(mod, with_descendants)
prepend(mod) prepend(mod)
...@@ -75,12 +68,6 @@ module InjectEnterpriseEditionModule ...@@ -75,12 +68,6 @@ module InjectEnterpriseEditionModule
rescue NameError rescue NameError
false false
end end
def constant_without_prefix(constant_with_prefix)
constant_with_prefix
.delete_prefix('::') # TODO: Some calling sites are passing this prefix
.delete_prefix('EE::')
end
end end
Module.prepend(InjectEnterpriseEditionModule) Module.prepend(InjectEnterpriseEditionModule)
...@@ -175,4 +175,4 @@ module Banzai ...@@ -175,4 +175,4 @@ module Banzai
end end
end end
Banzai::Filter::References::ReferenceCache.prepend_if_ee('EE::Banzai::Filter::References::ReferenceCache') Banzai::Filter::References::ReferenceCache.prepend_mod
...@@ -2,16 +2,16 @@ ...@@ -2,16 +2,16 @@
module RuboCop module RuboCop
module Cop module Cop
# Cop that blacklists the injecting of EE specific modules before any lines which are not already injecting another module. # Cop that blacklists the injecting of extension 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. # It allows multiple module injections as long as they're all at the end.
class InjectEnterpriseEditionModule < RuboCop::Cop::Cop class InjectEnterpriseEditionModule < RuboCop::Cop::Cop
INVALID_LINE = 'Injecting EE modules must be done on the last line of this file' \ INVALID_LINE = 'Injecting extension modules must be done on the last line of this file' \
', outside of any class or module definitions' ', outside of any class or module definitions'
DISALLOWED_METHOD = DISALLOWED_METHOD =
'EE modules must be injected using `include_mod_with`, `extend_mod_with`, or `prepend_mod_with`' 'EE modules must be injected using `include_mod_with`, `extend_mod_with`, or `prepend_mod_with`'
INVALID_ARGUMENT = 'EE modules to inject must be specified as a String' INVALID_ARGUMENT = 'extension modules to inject must be specified as a String'
CHECK_LINE_METHODS = CHECK_LINE_METHODS =
Set.new(%i[include_mod_with extend_mod_with prepend_mod_with]).freeze Set.new(%i[include_mod_with extend_mod_with prepend_mod_with]).freeze
...@@ -67,10 +67,10 @@ module RuboCop ...@@ -67,10 +67,10 @@ module RuboCop
def check_method?(node) def check_method?(node)
name = node.children[1] name = node.children[1]
if CHECK_LINE_METHODS.include?(name) || DISALLOW_METHODS.include?(name) if DISALLOW_METHODS.include?(name)
ee_const?(node.children[2]) ee_const?(node.children[2])
else else
false CHECK_LINE_METHODS.include?(name)
end end
end end
......
...@@ -28,19 +28,7 @@ RSpec.describe InjectEnterpriseEditionModule do ...@@ -28,19 +28,7 @@ RSpec.describe InjectEnterpriseEditionModule do
it "calls #{method} with the extension module" do it "calls #{method} with the extension module" do
expect(fish_class).to receive(method).with(fish_extension) expect(fish_class).to receive(method).with(fish_extension)
fish_class.__send__("#{method}_if_ee", fish_name) fish_class.__send__("#{method}_mod_with", fish_name)
end
it "ignores EE prefix and calls #{method} with the extension module" do
expect(fish_class).to receive(method).with(fish_extension)
fish_class.__send__("#{method}_if_ee", "EE::#{fish_name}")
end
it "ignores ::EE prefix and calls #{method} with the extension module" do
expect(fish_class).to receive(method).with(fish_extension)
fish_class.__send__("#{method}_if_ee", "::EE::#{fish_name}")
end end
end end
...@@ -55,7 +43,7 @@ RSpec.describe InjectEnterpriseEditionModule do ...@@ -55,7 +43,7 @@ RSpec.describe InjectEnterpriseEditionModule do
it "calls #{method} with the extension module from the additional namespace" do it "calls #{method} with the extension module from the additional namespace" do
expect(fish_class).to receive(method).with(fish_extension) expect(fish_class).to receive(method).with(fish_extension)
fish_class.__send__("#{method}_if_ee", fish_name, namespace: another_namespace) fish_class.__send__("#{method}_mod_with", fish_name, namespace: another_namespace)
end end
end end
...@@ -67,7 +55,7 @@ RSpec.describe InjectEnterpriseEditionModule do ...@@ -67,7 +55,7 @@ RSpec.describe InjectEnterpriseEditionModule do
it "does not call #{method}" do it "does not call #{method}" do
expect(fish_class).not_to receive(method).with(fish_extension) expect(fish_class).not_to receive(method).with(fish_extension)
fish_class.__send__("#{method}_if_ee", fish_name) fish_class.__send__("#{method}_mod_with", fish_name)
end end
end end
...@@ -75,7 +63,7 @@ RSpec.describe InjectEnterpriseEditionModule do ...@@ -75,7 +63,7 @@ RSpec.describe InjectEnterpriseEditionModule do
it "does not call #{method}" do it "does not call #{method}" do
expect(fish_class).not_to receive(method).with(fish_extension) expect(fish_class).not_to receive(method).with(fish_extension)
fish_class.__send__("#{method}_if_ee", fish_name) fish_class.__send__("#{method}_mod_with", fish_name)
end end
end end
end end
......
...@@ -6,129 +6,33 @@ require_relative '../../../rubocop/cop/inject_enterprise_edition_module' ...@@ -6,129 +6,33 @@ require_relative '../../../rubocop/cop/inject_enterprise_edition_module'
RSpec.describe RuboCop::Cop::InjectEnterpriseEditionModule do RSpec.describe RuboCop::Cop::InjectEnterpriseEditionModule do
subject(:cop) { described_class.new } subject(:cop) { described_class.new }
it 'flags the use of `prepend_mod_with EE` in the middle of a file' do it 'flags the use of `prepend_mod_with` in the middle of a file' do
expect_offense(<<~SOURCE) expect_offense(<<~SOURCE)
class Foo class Foo
prepend_mod_with 'EE::Foo' prepend_mod_with('Foo')
^^^^^^^^^^^^^^^^^^^^^^^ Injecting EE modules must be done on the last line of this file, outside of any class or module definitions ^^^^^^^^^^^^^^^^^^^^^^^ Injecting extension modules must be done on the last line of this file, outside of any class or module definitions
end end
SOURCE SOURCE
end end
it 'flags the use of `prepend_mod_with QA::EE` in the middle of a file' do it 'flags the use of `include_mod_with` in the middle of a file' do
expect_offense(<<~SOURCE) expect_offense(<<~SOURCE)
class Foo class Foo
prepend_mod_with 'QA::EE::Foo' include_mod_with('Foo')
^^^^^^^^^^^^^^^^^^^^^^^^^^^ Injecting EE modules must be done on the last line of this file, outside of any class or module definitions ^^^^^^^^^^^^^^^^^^^^^^^ Injecting extension modules must be done on the last line of this file, outside of any class or module definitions
end end
SOURCE SOURCE
end end
it 'flags the use of `extend_mod_with` in the middle of a file' do
it 'does not flag the use of `prepend_mod_with EEFoo` in the middle of a file' do
expect_no_offenses(<<~SOURCE)
class Foo
prepend_mod_with 'EEFoo'
end
SOURCE
end
it 'flags the use of `prepend_mod_with EE::Foo::Bar` in the middle of a file' do
expect_offense(<<~SOURCE)
class Foo
prepend_mod_with '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_mod_with(EE::Foo::Bar)` in the middle of a file' do
expect_offense(<<~SOURCE)
class Foo
prepend_mod_with('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_mod_with EE::Foo::Bar::Baz` in the middle of a file' do
expect_offense(<<~SOURCE) expect_offense(<<~SOURCE)
class Foo class Foo
prepend_mod_with 'EE::Foo::Bar::Baz' extend_mod_with('Foo')
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Injecting EE modules must be done on the last line of this file, outside of any class or module definitions ^^^^^^^^^^^^^^^^^^^^^^ Injecting extension 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_mod_with ::EE` in the middle of a file' do
expect_offense(<<~SOURCE)
class Foo
prepend_mod_with '::EE::Foo'
^^^^^^^^^^^^^^^^^^^^^^^^^ 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 `include_mod_with EE` in the middle of a file' do
expect_offense(<<~SOURCE)
class Foo
include_mod_with 'EE::Foo'
^^^^^^^^^^^^^^^^^^^^^^^ 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 `include_mod_with ::EE` in the middle of a file' do
expect_offense(<<~SOURCE)
class Foo
include_mod_with '::EE::Foo'
^^^^^^^^^^^^^^^^^^^^^^^^^ 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 `extend_mod_with EE` in the middle of a file' do
expect_offense(<<~SOURCE)
class Foo
extend_mod_with 'EE::Foo'
^^^^^^^^^^^^^^^^^^^^^^ 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 `extend_mod_with ::EE` in the middle of a file' do
expect_offense(<<~SOURCE)
class Foo
extend_mod_with '::EE::Foo'
^^^^^^^^^^^^^^^^^^^^^^^^ Injecting EE modules must be done on the last line of this file, outside of any class or module definitions
end
SOURCE
end
it 'does not flag prepending of regular modules' do
expect_no_offenses(<<~SOURCE)
class Foo
prepend_mod_with 'Foo'
end
SOURCE
end
it 'does not flag including of regular modules' do
expect_no_offenses(<<~SOURCE)
class Foo
include_mod_with 'Foo'
end
SOURCE
end
it 'does not flag extending using regular modules' do
expect_no_offenses(<<~SOURCE)
class Foo
extend_mod_with 'Foo'
end end
SOURCE SOURCE
end end
it 'does not flag the use of `prepend_mod_with EE` on the last line' do it 'does not flag the use of `prepend_mod_with` on the last line' do
expect_no_offenses(<<~SOURCE) expect_no_offenses(<<~SOURCE)
class Foo class Foo
end end
...@@ -137,7 +41,7 @@ RSpec.describe RuboCop::Cop::InjectEnterpriseEditionModule do ...@@ -137,7 +41,7 @@ RSpec.describe RuboCop::Cop::InjectEnterpriseEditionModule do
SOURCE SOURCE
end end
it 'does not flag the use of `include_mod_with EE` on the last line' do it 'does not flag the use of `include_mod_with` on the last line' do
expect_no_offenses(<<~SOURCE) expect_no_offenses(<<~SOURCE)
class Foo class Foo
end end
...@@ -146,7 +50,7 @@ RSpec.describe RuboCop::Cop::InjectEnterpriseEditionModule do ...@@ -146,7 +50,7 @@ RSpec.describe RuboCop::Cop::InjectEnterpriseEditionModule do
SOURCE SOURCE
end end
it 'does not flag the use of `extend_mod_with EE` on the last line' do it 'does not flag the use of `extend_mod_with` on the last line' do
expect_no_offenses(<<~SOURCE) expect_no_offenses(<<~SOURCE)
class Foo class Foo
end end
...@@ -155,7 +59,7 @@ RSpec.describe RuboCop::Cop::InjectEnterpriseEditionModule do ...@@ -155,7 +59,7 @@ RSpec.describe RuboCop::Cop::InjectEnterpriseEditionModule do
SOURCE SOURCE
end end
it 'does not flag the double use of `X_if_ee` on the last line' do it 'does not flag the double use of `X_mod_with` on the last line' do
expect_no_offenses(<<~SOURCE) expect_no_offenses(<<~SOURCE)
class Foo class Foo
end end
...@@ -166,7 +70,7 @@ RSpec.describe RuboCop::Cop::InjectEnterpriseEditionModule do ...@@ -166,7 +70,7 @@ RSpec.describe RuboCop::Cop::InjectEnterpriseEditionModule do
SOURCE SOURCE
end end
it 'does not flag the use of `prepend_mod_with EE` as long as all injections are at the end of the file' do it 'does not flag the use of `prepend_mod_with` as long as all injections are at the end of the file' do
expect_no_offenses(<<~SOURCE) expect_no_offenses(<<~SOURCE)
class Foo class Foo
end end
...@@ -183,21 +87,21 @@ RSpec.describe RuboCop::Cop::InjectEnterpriseEditionModule do ...@@ -183,21 +87,21 @@ RSpec.describe RuboCop::Cop::InjectEnterpriseEditionModule do
it 'autocorrects offenses by just disabling the Cop' do it 'autocorrects offenses by just disabling the Cop' do
expect_offense(<<~SOURCE) expect_offense(<<~SOURCE)
class Foo class Foo
prepend_mod_with 'EE::Foo' prepend_mod_with('Foo')
^^^^^^^^^^^^^^^^^^^^^^^ Injecting EE modules must be done on the last line of this file, outside of any class or module definitions ^^^^^^^^^^^^^^^^^^^^^^^ Injecting extension modules must be done on the last line of this file, outside of any class or module definitions
include_mod_with 'Bar' include Bar
end end
SOURCE SOURCE
expect_correction(<<~SOURCE) expect_correction(<<~SOURCE)
class Foo class Foo
prepend_mod_with 'EE::Foo' # rubocop: disable Cop/InjectEnterpriseEditionModule prepend_mod_with('Foo') # rubocop: disable Cop/InjectEnterpriseEditionModule
include_mod_with 'Bar' include Bar
end end
SOURCE SOURCE
end end
it 'disallows the use of prepend to inject an EE module' do it 'disallows the use of prepend to inject an extension module' do
expect_offense(<<~SOURCE) expect_offense(<<~SOURCE)
class Foo class Foo
end end
...@@ -242,8 +146,8 @@ RSpec.describe RuboCop::Cop::InjectEnterpriseEditionModule do ...@@ -242,8 +146,8 @@ RSpec.describe RuboCop::Cop::InjectEnterpriseEditionModule do
class Foo class Foo
end end
Foo.prepend_mod_with(EE::Foo) Foo.prepend_mod_with(Foo)
^^^^^^^ EE modules to inject must be specified as a String ^^^ extension modules to inject must be specified as a String
SOURCE SOURCE
end end
...@@ -252,8 +156,8 @@ RSpec.describe RuboCop::Cop::InjectEnterpriseEditionModule do ...@@ -252,8 +156,8 @@ RSpec.describe RuboCop::Cop::InjectEnterpriseEditionModule do
class Foo class Foo
end end
Foo.include_mod_with(EE::Foo) Foo.include_mod_with(Foo)
^^^^^^^ EE modules to inject must be specified as a String ^^^ extension modules to inject must be specified as a String
SOURCE SOURCE
end end
...@@ -262,8 +166,8 @@ RSpec.describe RuboCop::Cop::InjectEnterpriseEditionModule do ...@@ -262,8 +166,8 @@ RSpec.describe RuboCop::Cop::InjectEnterpriseEditionModule do
class Foo class Foo
end end
Foo.extend_mod_with(EE::Foo) Foo.extend_mod_with(Foo)
^^^^^^^ EE modules to inject must be specified as a String ^^^ extension modules to inject must be specified as a String
SOURCE SOURCE
end end
end end
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