summaryrefslogtreecommitdiff
path: root/rubocop
diff options
context:
space:
mode:
authorGitLab Bot <gitlab-bot@gitlab.com>2021-02-18 10:34:06 +0000
committerGitLab Bot <gitlab-bot@gitlab.com>2021-02-18 10:34:06 +0000
commit859a6fb938bb9ee2a317c46dfa4fcc1af49608f0 (patch)
treed7f2700abe6b4ffcb2dcfc80631b2d87d0609239 /rubocop
parent446d496a6d000c73a304be52587cd9bbc7493136 (diff)
downloadgitlab-ce-859a6fb938bb9ee2a317c46dfa4fcc1af49608f0.tar.gz
Add latest changes from gitlab-org/gitlab@13-9-stable-eev13.9.0-rc42
Diffstat (limited to 'rubocop')
-rw-r--r--rubocop/cop/avoid_keyword_arguments_in_sidekiq_workers.rb2
-rw-r--r--rubocop/cop/gitlab/finder_with_find_by.rb2
-rw-r--r--rubocop/cop/gitlab/httparty.rb4
-rw-r--r--rubocop/cop/gitlab/json.rb4
-rw-r--r--rubocop/cop/gitlab/keys-first-and-values-first.rb2
-rw-r--r--rubocop/cop/gitlab/module_with_instance_variables.rb4
-rw-r--r--rubocop/cop/gitlab/namespaced_class.rb39
-rw-r--r--rubocop/cop/gitlab/predicate_memoization.rb6
-rw-r--r--rubocop/cop/migration/add_concurrent_foreign_key.rb2
-rw-r--r--rubocop/cop/migration/add_concurrent_index.rb2
-rw-r--r--rubocop/cop/migration/add_index.rb2
-rw-r--r--rubocop/cop/migration/add_limit_to_text_columns.rb31
-rw-r--r--rubocop/cop/migration/add_timestamps.rb2
-rw-r--r--rubocop/cop/migration/datetime.rb2
-rw-r--r--rubocop/cop/migration/hash_index.rb2
-rw-r--r--rubocop/cop/migration/remove_column.rb2
-rw-r--r--rubocop/cop/migration/remove_concurrent_index.rb2
-rw-r--r--rubocop/cop/migration/remove_index.rb2
-rw-r--r--rubocop/cop/migration/safer_boolean_column.rb2
-rw-r--r--rubocop/cop/migration/timestamps.rb2
-rw-r--r--rubocop/cop/migration/update_column_in_batches.rb2
-rw-r--r--rubocop/cop/project_path_helper.rb2
-rw-r--r--rubocop/cop/rspec/factories_in_migration_specs.rb2
-rw-r--r--rubocop/migration_helpers.rb11
-rw-r--r--rubocop/qa_helpers.rb2
-rw-r--r--rubocop/rubocop-migrations.yml1
-rw-r--r--rubocop/rubocop-usage-data.yml2
27 files changed, 118 insertions, 20 deletions
diff --git a/rubocop/cop/avoid_keyword_arguments_in_sidekiq_workers.rb b/rubocop/cop/avoid_keyword_arguments_in_sidekiq_workers.rb
index eba38c1630f..e1c6a984e75 100644
--- a/rubocop/cop/avoid_keyword_arguments_in_sidekiq_workers.rb
+++ b/rubocop/cop/avoid_keyword_arguments_in_sidekiq_workers.rb
@@ -1,3 +1,5 @@
+# frozen_string_literal: true
+
module RuboCop
module Cop
# Cop that blacklists keyword arguments usage in Sidekiq workers
diff --git a/rubocop/cop/gitlab/finder_with_find_by.rb b/rubocop/cop/gitlab/finder_with_find_by.rb
index 764a5073143..8fa9fe4a2f9 100644
--- a/rubocop/cop/gitlab/finder_with_find_by.rb
+++ b/rubocop/cop/gitlab/finder_with_find_by.rb
@@ -1,3 +1,5 @@
+# frozen_string_literal: true
+
module RuboCop
module Cop
module Gitlab
diff --git a/rubocop/cop/gitlab/httparty.rb b/rubocop/cop/gitlab/httparty.rb
index 8acebff624d..20f0c381e11 100644
--- a/rubocop/cop/gitlab/httparty.rb
+++ b/rubocop/cop/gitlab/httparty.rb
@@ -4,13 +4,13 @@ module RuboCop
module Cop
module Gitlab
class HTTParty < RuboCop::Cop::Cop
- MSG_SEND = <<~EOL.freeze
+ MSG_SEND = <<~EOL
Avoid calling `HTTParty` directly. Instead, use the Gitlab::HTTP
wrapper. To allow request to localhost or the private network set
the option :allow_local_requests in the request call.
EOL
- MSG_INCLUDE = <<~EOL.freeze
+ MSG_INCLUDE = <<~EOL
Avoid including `HTTParty` directly. Instead, use the Gitlab::HTTP
wrapper. To allow request to localhost or the private network set
the option :allow_local_requests in the request call.
diff --git a/rubocop/cop/gitlab/json.rb b/rubocop/cop/gitlab/json.rb
index 8c9027223aa..7cc719aca09 100644
--- a/rubocop/cop/gitlab/json.rb
+++ b/rubocop/cop/gitlab/json.rb
@@ -4,7 +4,7 @@ module RuboCop
module Cop
module Gitlab
class Json < RuboCop::Cop::Cop
- MSG_SEND = <<~EOL.freeze
+ MSG = <<~EOL
Avoid calling `JSON` directly. Instead, use the `Gitlab::Json`
wrapper. This allows us to alter the JSON parser being used.
EOL
@@ -14,7 +14,7 @@ module RuboCop
PATTERN
def on_send(node)
- add_offense(node, location: :expression, message: MSG_SEND) if json_node?(node)
+ add_offense(node) if json_node?(node)
end
def autocorrect(node)
diff --git a/rubocop/cop/gitlab/keys-first-and-values-first.rb b/rubocop/cop/gitlab/keys-first-and-values-first.rb
index 544f9800304..e9bf266cdd7 100644
--- a/rubocop/cop/gitlab/keys-first-and-values-first.rb
+++ b/rubocop/cop/gitlab/keys-first-and-values-first.rb
@@ -1,3 +1,5 @@
+# frozen_string_literal: true
+
module RuboCop
module Cop
module Gitlab
diff --git a/rubocop/cop/gitlab/module_with_instance_variables.rb b/rubocop/cop/gitlab/module_with_instance_variables.rb
index dd8bd2dfdf0..40cdc0d3a57 100644
--- a/rubocop/cop/gitlab/module_with_instance_variables.rb
+++ b/rubocop/cop/gitlab/module_with_instance_variables.rb
@@ -1,8 +1,10 @@
+# frozen_string_literal: true
+
module RuboCop
module Cop
module Gitlab
class ModuleWithInstanceVariables < RuboCop::Cop::Cop
- MSG = <<~EOL.freeze
+ MSG = <<~EOL
Do not use instance variables in a module. Please read this
for the rationale behind it:
diff --git a/rubocop/cop/gitlab/namespaced_class.rb b/rubocop/cop/gitlab/namespaced_class.rb
new file mode 100644
index 00000000000..1f1fd280922
--- /dev/null
+++ b/rubocop/cop/gitlab/namespaced_class.rb
@@ -0,0 +1,39 @@
+# frozen_string_literal: true
+
+module RuboCop
+ module Cop
+ module Gitlab
+ # Cop that enforces use of namespaced classes in order to better identify
+ # high level domains within the codebase.
+
+ # @example
+ # # bad
+ # class MyClass
+ # end
+ #
+ # # good
+ # module MyDomain
+ # class MyClass
+ # end
+ # end
+
+ class NamespacedClass < RuboCop::Cop::Cop
+ MSG = 'Classes must be declared inside a module indicating a product domain namespace. For more info: https://gitlab.com/gitlab-org/gitlab/-/issues/212156'
+
+ def_node_matcher :compact_namespaced_class?, <<~PATTERN
+ (class (const (const ...) ...) ...)
+ PATTERN
+
+ def on_module(node)
+ @namespaced = true
+ end
+
+ def on_class(node)
+ return if @namespaced
+
+ add_offense(node) unless compact_namespaced_class?(node)
+ end
+ end
+ end
+ end
+end
diff --git a/rubocop/cop/gitlab/predicate_memoization.rb b/rubocop/cop/gitlab/predicate_memoization.rb
index 3c25d61d087..4c851f90238 100644
--- a/rubocop/cop/gitlab/predicate_memoization.rb
+++ b/rubocop/cop/gitlab/predicate_memoization.rb
@@ -1,8 +1,10 @@
+# frozen_string_literal: true
+
module RuboCop
module Cop
module Gitlab
class PredicateMemoization < RuboCop::Cop::Cop
- MSG = <<~EOL.freeze
+ MSG = <<~EOL
Avoid using `@value ||= query` inside predicate methods in order to
properly memoize `false` or `nil` values.
https://docs.gitlab.com/ee/development/utilities.html#strongmemoize
@@ -12,7 +14,7 @@ module RuboCop
return unless predicate_method?(node)
select_offenses(node).each do |offense|
- add_offense(offense, location: :expression)
+ add_offense(offense)
end
end
diff --git a/rubocop/cop/migration/add_concurrent_foreign_key.rb b/rubocop/cop/migration/add_concurrent_foreign_key.rb
index 31cf426b2d4..957bd30af63 100644
--- a/rubocop/cop/migration/add_concurrent_foreign_key.rb
+++ b/rubocop/cop/migration/add_concurrent_foreign_key.rb
@@ -1,3 +1,5 @@
+# frozen_string_literal: true
+
require_relative '../../migration_helpers'
module RuboCop
diff --git a/rubocop/cop/migration/add_concurrent_index.rb b/rubocop/cop/migration/add_concurrent_index.rb
index a2e4ac72565..510f98ce373 100644
--- a/rubocop/cop/migration/add_concurrent_index.rb
+++ b/rubocop/cop/migration/add_concurrent_index.rb
@@ -1,3 +1,5 @@
+# frozen_string_literal: true
+
require_relative '../../migration_helpers'
module RuboCop
diff --git a/rubocop/cop/migration/add_index.rb b/rubocop/cop/migration/add_index.rb
index 4aea3c0cce3..7415880e554 100644
--- a/rubocop/cop/migration/add_index.rb
+++ b/rubocop/cop/migration/add_index.rb
@@ -1,3 +1,5 @@
+# frozen_string_literal: true
+
require_relative '../../migration_helpers'
module RuboCop
diff --git a/rubocop/cop/migration/add_limit_to_text_columns.rb b/rubocop/cop/migration/add_limit_to_text_columns.rb
index b2e37ad5137..126e4e21f22 100644
--- a/rubocop/cop/migration/add_limit_to_text_columns.rb
+++ b/rubocop/cop/migration/add_limit_to_text_columns.rb
@@ -20,6 +20,10 @@ module RuboCop
(def :down ...)
PATTERN
+ def_node_matcher :set_text_limit?, <<~PATTERN
+ (send _ :text_limit ...)
+ PATTERN
+
def_node_matcher :add_text_limit?, <<~PATTERN
(send _ :add_text_limit ...)
PATTERN
@@ -111,20 +115,31 @@ module RuboCop
limit_found = false
node.each_descendant(:send) do |send_node|
- next unless add_text_limit?(send_node)
-
- limit_table = send_node.children[2].value
- limit_attribute = send_node.children[3].value
-
- if limit_table == table_name && limit_attribute == attribute_name
- limit_found = true
- break
+ if set_text_limit?(send_node)
+ limit_found = matching_set_text_limit?(send_node, attribute_name)
+ elsif add_text_limit?(send_node)
+ limit_found = matching_add_text_limit?(send_node, table_name, attribute_name)
end
+
+ break if limit_found
end
!limit_found
end
+ def matching_set_text_limit?(send_node, attribute_name)
+ limit_attribute = send_node.children[2].value
+
+ limit_attribute == attribute_name
+ end
+
+ def matching_add_text_limit?(send_node, table_name, attribute_name)
+ limit_table = send_node.children[2].value
+ limit_attribute = send_node.children[3].value
+
+ limit_table == table_name && limit_attribute == attribute_name
+ end
+
def encrypted_attribute_name?(attribute_name)
attribute_name.to_s.start_with?('encrypted_')
end
diff --git a/rubocop/cop/migration/add_timestamps.rb b/rubocop/cop/migration/add_timestamps.rb
index ba32d6a9960..d16e8b1f45b 100644
--- a/rubocop/cop/migration/add_timestamps.rb
+++ b/rubocop/cop/migration/add_timestamps.rb
@@ -1,3 +1,5 @@
+# frozen_string_literal: true
+
require_relative '../../migration_helpers'
module RuboCop
diff --git a/rubocop/cop/migration/datetime.rb b/rubocop/cop/migration/datetime.rb
index 5a6cdc74ca4..51e0c3e5a22 100644
--- a/rubocop/cop/migration/datetime.rb
+++ b/rubocop/cop/migration/datetime.rb
@@ -1,3 +1,5 @@
+# frozen_string_literal: true
+
require_relative '../../migration_helpers'
module RuboCop
diff --git a/rubocop/cop/migration/hash_index.rb b/rubocop/cop/migration/hash_index.rb
index 3206b73bd3d..dba202ef0e3 100644
--- a/rubocop/cop/migration/hash_index.rb
+++ b/rubocop/cop/migration/hash_index.rb
@@ -1,3 +1,5 @@
+# frozen_string_literal: true
+
require 'set'
require_relative '../../migration_helpers'
diff --git a/rubocop/cop/migration/remove_column.rb b/rubocop/cop/migration/remove_column.rb
index fffb4ab7fab..f63df71467c 100644
--- a/rubocop/cop/migration/remove_column.rb
+++ b/rubocop/cop/migration/remove_column.rb
@@ -1,3 +1,5 @@
+# frozen_string_literal: true
+
require_relative '../../migration_helpers'
module RuboCop
diff --git a/rubocop/cop/migration/remove_concurrent_index.rb b/rubocop/cop/migration/remove_concurrent_index.rb
index 2328740cf36..8c2c6fb157e 100644
--- a/rubocop/cop/migration/remove_concurrent_index.rb
+++ b/rubocop/cop/migration/remove_concurrent_index.rb
@@ -1,3 +1,5 @@
+# frozen_string_literal: true
+
require_relative '../../migration_helpers'
module RuboCop
diff --git a/rubocop/cop/migration/remove_index.rb b/rubocop/cop/migration/remove_index.rb
index 4df3b1ba756..15c2f37b4b0 100644
--- a/rubocop/cop/migration/remove_index.rb
+++ b/rubocop/cop/migration/remove_index.rb
@@ -1,3 +1,5 @@
+# frozen_string_literal: true
+
require_relative '../../migration_helpers'
module RuboCop
diff --git a/rubocop/cop/migration/safer_boolean_column.rb b/rubocop/cop/migration/safer_boolean_column.rb
index 22d5d37a83d..06bb24707bd 100644
--- a/rubocop/cop/migration/safer_boolean_column.rb
+++ b/rubocop/cop/migration/safer_boolean_column.rb
@@ -1,3 +1,5 @@
+# frozen_string_literal: true
+
require_relative '../../migration_helpers'
module RuboCop
diff --git a/rubocop/cop/migration/timestamps.rb b/rubocop/cop/migration/timestamps.rb
index 6cf5648b996..5584d49ee8c 100644
--- a/rubocop/cop/migration/timestamps.rb
+++ b/rubocop/cop/migration/timestamps.rb
@@ -1,3 +1,5 @@
+# frozen_string_literal: true
+
require_relative '../../migration_helpers'
module RuboCop
diff --git a/rubocop/cop/migration/update_column_in_batches.rb b/rubocop/cop/migration/update_column_in_batches.rb
index b1c43393f6a..d23e0d28380 100644
--- a/rubocop/cop/migration/update_column_in_batches.rb
+++ b/rubocop/cop/migration/update_column_in_batches.rb
@@ -1,3 +1,5 @@
+# frozen_string_literal: true
+
require_relative '../../migration_helpers'
module RuboCop
diff --git a/rubocop/cop/project_path_helper.rb b/rubocop/cop/project_path_helper.rb
index f3810622eb1..bc2454e5b1f 100644
--- a/rubocop/cop/project_path_helper.rb
+++ b/rubocop/cop/project_path_helper.rb
@@ -1,3 +1,5 @@
+# frozen_string_literal: true
+
module RuboCop
module Cop
class ProjectPathHelper < RuboCop::Cop::Cop
diff --git a/rubocop/cop/rspec/factories_in_migration_specs.rb b/rubocop/cop/rspec/factories_in_migration_specs.rb
index 65c7638a0f4..732e0d92bbd 100644
--- a/rubocop/cop/rspec/factories_in_migration_specs.rb
+++ b/rubocop/cop/rspec/factories_in_migration_specs.rb
@@ -15,7 +15,7 @@ module RuboCop
# let(:user) { users.create!(name: 'User 1', username: 'user1') }
class FactoriesInMigrationSpecs < RuboCop::Cop::Cop
MESSAGE = "Don't use FactoryBot.%s in migration specs, use `table` instead.".freeze
- FORBIDDEN_METHODS = %i[build build_list create create_list].freeze
+ FORBIDDEN_METHODS = %i[build build_list create create_list attributes_for].freeze
def_node_search :forbidden_factory_usage?, <<~PATTERN
(send {(const nil? :FactoryBot) nil?} {#{FORBIDDEN_METHODS.map(&:inspect).join(' ')}} ...)
diff --git a/rubocop/migration_helpers.rb b/rubocop/migration_helpers.rb
index e9533fb65b2..63b3766e126 100644
--- a/rubocop/migration_helpers.rb
+++ b/rubocop/migration_helpers.rb
@@ -1,3 +1,5 @@
+# frozen_string_literal: true
+
module RuboCop
# Module containing helper methods for writing migration cops.
module MigrationHelpers
@@ -7,18 +9,19 @@ module RuboCop
plan_limits
].freeze
- # Tables with large number of columns (> 50 on GitLab.com as of 03/2020)
+ # Tables with large number of columns (> 50 on GitLab.com as of 01/2021)
WIDE_TABLES = %i[
- users
- projects
ci_builds
+ namespaces
+ projects
+ users
].freeze
# List of helpers that add new columns, either directly (ADD_COLUMN_METHODS)
# or through a create/alter table (TABLE_METHODS)
ADD_COLUMN_METHODS = %i(add_column add_column_with_default change_column_type_concurrently).freeze
- TABLE_METHODS = %i(create_table create_table_if_not_exists change_table).freeze
+ TABLE_METHODS = %i(create_table create_table_if_not_exists change_table create_table_with_constraints).freeze
def high_traffic_tables
@high_traffic_tables ||= rubocop_migrations_config.dig('Migration/UpdateLargeTable', 'HighTrafficTables')
diff --git a/rubocop/qa_helpers.rb b/rubocop/qa_helpers.rb
index f4adf7f4e9f..9d6396e412e 100644
--- a/rubocop/qa_helpers.rb
+++ b/rubocop/qa_helpers.rb
@@ -1,3 +1,5 @@
+# frozen_string_literal: true
+
module RuboCop
# Module containing helper methods for writing QA cops.
module QAHelpers
diff --git a/rubocop/rubocop-migrations.yml b/rubocop/rubocop-migrations.yml
index 41bd2a4ce7d..45f8270b34d 100644
--- a/rubocop/rubocop-migrations.yml
+++ b/rubocop/rubocop-migrations.yml
@@ -38,6 +38,7 @@ Migration/UpdateLargeTable:
- :users
- :user_preferences
- :user_details
+ - :vulnerability_occurrences
- :web_hook_logs
DeniedMethods:
- :change_column_type_concurrently
diff --git a/rubocop/rubocop-usage-data.yml b/rubocop/rubocop-usage-data.yml
index 0e40a5971ee..bbc4d590ddc 100644
--- a/rubocop/rubocop-usage-data.yml
+++ b/rubocop/rubocop-usage-data.yml
@@ -21,6 +21,8 @@ UsageData/LargeTable:
- :Gitaly::Server
- :Gitlab::UsageData
- :Gitlab::UsageDataCounters
+ - :Arel::Nodes::NamedFunction
+ - :Arel
- :License
- :Rails
- :Time