summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMario de la Ossa <mariodelaossa@gmail.com>2018-05-19 15:32:07 -0600
committerMario de la Ossa <mariodelaossa@gmail.com>2018-05-19 16:10:11 -0600
commit50b2e74658f30a2cdcc4e9328fcfee35f30e853f (patch)
tree4c404163a208b8f2a21706d7f6c43e48fa2d2836
parentad9e00917fdff0c311f4755e8be764016ddd18e1 (diff)
downloadgitlab-ce-28109-unqualified-constants-cop.tar.gz
Add Rubocop Cop that ensures bare constants are not used28109-unqualified-constants-cop
-rw-r--r--rubocop/cop/unqualified_constants.rb31
-rw-r--r--rubocop/rubocop.rb1
-rw-r--r--spec/rubocop/cop/unqualified_constants_spec.rb159
3 files changed, 191 insertions, 0 deletions
diff --git a/rubocop/cop/unqualified_constants.rb b/rubocop/cop/unqualified_constants.rb
new file mode 100644
index 00000000000..54d1baa878c
--- /dev/null
+++ b/rubocop/cop/unqualified_constants.rb
@@ -0,0 +1,31 @@
+require_relative '../spec_helpers'
+
+module RuboCop
+ module Cop
+ # Cop that makes sure workers include `ApplicationWorker`, not `Sidekiq::Worker`.
+ class FullyQualifiedConstants < RuboCop::Cop::Cop
+ include RuboCop::SpecHelpers
+
+ MSG = 'Do not use unqualified constants (ex. Use `Gitlab::Util` instead of `Util`).'.freeze
+
+ def_node_matcher :extend_include_prepend_bare?, <<~PATTERN
+ (send nil? {:extend :include :prepend} #bare_constant?)
+ PATTERN
+
+ def_node_matcher :send_to_bare_constant?, <<~PATTERN
+ (send #bare_constant? ...)
+ PATTERN
+
+ def_node_matcher :bare_constant?, <<~PATTERN
+ (const nil? _)
+ PATTERN
+
+ def on_send(node)
+ return if in_spec?(node)
+
+ add_offense(node.arguments.first, location: :expression) if extend_include_prepend_bare?(node)
+ add_offense(node, location: :expression) if send_to_bare_constant?(node)
+ end
+ end
+ end
+end
diff --git a/rubocop/rubocop.rb b/rubocop/rubocop.rb
index f05990232ab..00b7690ba69 100644
--- a/rubocop/rubocop.rb
+++ b/rubocop/rubocop.rb
@@ -25,3 +25,4 @@ require_relative 'cop/project_path_helper'
require_relative 'cop/rspec/env_assignment'
require_relative 'cop/rspec/factories_in_migration_specs'
require_relative 'cop/sidekiq_options_queue'
+require_relative 'cop/unqualified_constants'
diff --git a/spec/rubocop/cop/unqualified_constants_spec.rb b/spec/rubocop/cop/unqualified_constants_spec.rb
new file mode 100644
index 00000000000..c4e37142930
--- /dev/null
+++ b/spec/rubocop/cop/unqualified_constants_spec.rb
@@ -0,0 +1,159 @@
+
+require 'spec_helper'
+require 'rubocop'
+require 'rubocop/rspec/support'
+require_relative '../../../rubocop/cop/unqualified_constants'
+
+describe RuboCop::Cop::FullyQualifiedConstants do
+ include CopHelper
+
+ subject(:cop) { described_class.new }
+
+ shared_examples('registering offense') do |options|
+ let(:offending_lines) { options[:offending_lines] }
+
+ it 'registers an offense when not using a fully-qualified constant' do
+ inspect_source(source)
+
+ aggregate_failures do
+ expect(cop.offenses.size).to eq(offending_lines.size)
+ expect(cop.offenses.map(&:line)).to eq(offending_lines)
+ end
+ end
+ end
+
+ shared_examples('not registering offense') do
+ it 'does not register offenses' do
+ inspect_source(source)
+
+ expect(cop.offenses).to be_empty
+ end
+ end
+
+ context 'when extending' do
+ context 'with unqualified constant' do
+ it_behaves_like 'registering offense', offending_lines: [2] do
+ let(:source) do
+ <<~RUBY
+ class C
+ extend Test
+ def really?
+ @really ||= true
+ end
+ end
+ RUBY
+ end
+ end
+ end
+
+ context 'with qualified constant' do
+ it_behaves_like 'not registering offense' do
+ let(:source) do
+ <<~RUBY
+ class C
+ extend Gitlab::Test
+ def really?
+ @really ||= ::User.find(some_var)
+ end
+ end
+ RUBY
+ end
+ end
+ end
+ end
+
+ context 'when including' do
+ context 'with unqualified constant' do
+ it_behaves_like 'registering offense', offending_lines: [2] do
+ let(:source) do
+ <<~RUBY
+ class C
+ include Test
+ def really?
+ @really ||= true
+ end
+ end
+ RUBY
+ end
+ end
+ end
+
+ context 'with qualified constant' do
+ it_behaves_like 'not registering offense' do
+ let(:source) do
+ <<~RUBY
+ class C
+ include Gitlab::Test
+ def really?
+ @really ||= true
+ end
+ end
+ RUBY
+ end
+ end
+ end
+ end
+
+ context 'when prepending' do
+ context 'with unqualified constant' do
+ it_behaves_like 'registering offense', offending_lines: [2] do
+ let(:source) do
+ <<~RUBY
+ class C
+ prepend Test
+ def really?
+ @really ||= true
+ end
+ end
+ RUBY
+ end
+ end
+ end
+
+ context 'with qualified constant' do
+ it_behaves_like 'not registering offense' do
+ let(:source) do
+ <<~RUBY
+ class C
+ prepend Another::Test
+ def really?
+ @really ||= true
+ end
+ end
+ RUBY
+ end
+ end
+ end
+ end
+
+ context 'when sending' do
+ context 'with qualified constant' do
+ it_behaves_like 'registering offense', offending_lines: [3, 4] do
+ let(:source) do
+ <<~RUBY
+ class C
+ def really?
+ @really ||= User.find(1)
+ @other = User.find(2)
+ end
+ end
+ RUBY
+ end
+ end
+ end
+
+ context 'with unqualified constant' do
+ it_behaves_like 'not registering offense' do
+ let(:source) do
+ <<~RUBY
+ class C
+ def really?
+ @really ||= ::User.find(1)
+ end
+ end
+ RUBY
+ end
+ end
+ end
+ end
+end