summaryrefslogtreecommitdiff
path: root/rubocop
diff options
context:
space:
mode:
authorGitLab Bot <gitlab-bot@gitlab.com>2022-08-18 08:17:02 +0000
committerGitLab Bot <gitlab-bot@gitlab.com>2022-08-18 08:17:02 +0000
commitb39512ed755239198a9c294b6a45e65c05900235 (patch)
treed234a3efade1de67c46b9e5a38ce813627726aa7 /rubocop
parentd31474cf3b17ece37939d20082b07f6657cc79a9 (diff)
downloadgitlab-ce-b39512ed755239198a9c294b6a45e65c05900235.tar.gz
Add latest changes from gitlab-org/gitlab@15-3-stable-eev15.3.0-rc42
Diffstat (limited to 'rubocop')
-rw-r--r--rubocop/code_reuse_helpers.rb14
-rw-r--r--rubocop/cop/code_reuse/worker.rb4
-rw-r--r--rubocop/cop/gemspec/avoid_executing_git.rb62
-rw-r--r--rubocop/cop/gitlab/deprecate_track_redis_hll_event.rb33
-rw-r--r--rubocop/cop/gitlab/feature_available_usage.rb3
-rw-r--r--rubocop/cop/gitlab/mark_used_feature_flags.rb2
-rw-r--r--rubocop/cop/migration/add_limit_to_text_columns.rb2
-rw-r--r--rubocop/cop/scalability/bulk_perform_with_context.rb2
-rw-r--r--rubocop/cop_todo.rb49
-rw-r--r--rubocop/formatter/todo_formatter.rb48
10 files changed, 169 insertions, 50 deletions
diff --git a/rubocop/code_reuse_helpers.rb b/rubocop/code_reuse_helpers.rb
index 45cfa7ba78d..2769da2389c 100644
--- a/rubocop/code_reuse_helpers.rb
+++ b/rubocop/code_reuse_helpers.rb
@@ -76,10 +76,15 @@ module RuboCop
in_app_directory?(node, 'controllers')
end
+ # Returns true if the given node resides in app/graphql or ee/app/graphql.
+ def in_graphql?(node)
+ in_app_directory?(node, 'graphql')
+ end
+
# Returns true if the given node resides in app/graphql/types,
# ee/app/graphql/types, or ee/app/graphql/ee/types.
def in_graphql_types?(node)
- in_app_directory?(node, 'graphql/types') || in_app_directory?(node, 'graphql/ee/types')
+ in_graphql_directory?(node, 'types')
end
# Returns true if the given node resides in lib/api or ee/lib/api.
@@ -113,6 +118,13 @@ module RuboCop
)
end
+ # Returns true if the given node resides in app/graphql/{directory},
+ # ee/app/graphql/{directory}, or ee/app/graphql/ee/{directory}.
+ def in_graphql_directory?(node, directory)
+ in_app_directory?(node, "graphql/#{directory}") ||
+ in_app_directory?(node, "graphql/ee/#{directory}")
+ end
+
# Returns the receiver name of a send node.
#
# For the AST node `(send (const nil? :Foo) ...)` this would return
diff --git a/rubocop/cop/code_reuse/worker.rb b/rubocop/cop/code_reuse/worker.rb
index 4902920234f..3a1120ac2a1 100644
--- a/rubocop/cop/code_reuse/worker.rb
+++ b/rubocop/cop/code_reuse/worker.rb
@@ -10,7 +10,7 @@ module RuboCop
include CodeReuseHelpers
IN_CONTROLLER = 'Workers can not be used in a controller.'
- IN_API = 'Workers can not be used in a Grape API.'
+ IN_API = 'Workers can not be used in an API endpoint.'
IN_FINDER = 'Workers can not be used in a Finder.'
IN_PRESENTER = 'Workers can not be used in a Presenter.'
IN_SERIALIZER = 'Workers can not be used in a Serializer.'
@@ -32,7 +32,7 @@ module RuboCop
message =
if in_controller?(node)
IN_CONTROLLER
- elsif in_api?(node)
+ elsif in_api?(node) || in_graphql?(node)
IN_API
elsif in_finder?(node)
IN_FINDER
diff --git a/rubocop/cop/gemspec/avoid_executing_git.rb b/rubocop/cop/gemspec/avoid_executing_git.rb
new file mode 100644
index 00000000000..09789e8208c
--- /dev/null
+++ b/rubocop/cop/gemspec/avoid_executing_git.rb
@@ -0,0 +1,62 @@
+# frozen_string_literal: true
+
+module RuboCop
+ module Cop
+ module Gemspec
+ # Checks that `git` is not executed in a vendored gemspec file.
+ # In some installed containers, `git` is not available.
+ #
+ # @example
+ #
+ # # bad
+ # Gem::Specification.new do |spec|
+ # spec.test_files = `git ls-files -- test/*`.split("\n")
+ # end
+ #
+ # # good
+ # Gem::Specification.new do |spec|
+ # spec.name = 'your_cool_gem_name'
+ # spec.test_files += Dir.glob('test/**/*')
+ # end
+ #
+ class AvoidExecutingGit < Base
+ include RangeHelp
+
+ MSG = 'Do not execute `git` in gemspec.'
+
+ # @!method gem_specification(node)
+ def_node_matcher :gem_specification, <<~PATTERN
+ (block
+ (send
+ (const
+ (const {cbase nil?} :Gem) :Specification) :new)
+ ...)
+ PATTERN
+
+ def_node_matcher :send_node?, <<~PATTERN
+ send
+ PATTERN
+
+ def_node_search :executes_string, <<~PATTERN
+ $(xstr (str $_))
+ PATTERN
+
+ def on_block(block_node)
+ return unless gem_specification(block_node)
+
+ block_node.descendants.each do |node|
+ next unless send_node?(node)
+
+ str = executes_string(node)
+
+ str.each do |execute_node, val|
+ break unless val.start_with?('git ')
+
+ add_offense(execute_node, message: message)
+ end
+ end
+ end
+ end
+ end
+ end
+end
diff --git a/rubocop/cop/gitlab/deprecate_track_redis_hll_event.rb b/rubocop/cop/gitlab/deprecate_track_redis_hll_event.rb
new file mode 100644
index 00000000000..3e30f3aa4d0
--- /dev/null
+++ b/rubocop/cop/gitlab/deprecate_track_redis_hll_event.rb
@@ -0,0 +1,33 @@
+# frozen_string_literal: true
+
+require 'rack/utils'
+
+module RuboCop
+ module Cop
+ module Gitlab
+ # This cop prevents from using deprecated `track_redis_hll_event` method.
+ #
+ # @example
+ #
+ # # bad
+ # track_redis_hll_event :show, name: 'p_analytics_valuestream'
+ #
+ # # good
+ # track_event :show, name: 'g_analytics_valuestream', destinations: [:redis_hll]
+ class DeprecateTrackRedisHLLEvent < RuboCop::Cop::Cop
+ MSG = '`track_redis_hll_event` is deprecated. Use `track_event` helper instead. ' \
+ 'See https://docs.gitlab.com/ee/development/service_ping/implement.html#add-new-events'
+
+ def_node_matcher :track_redis_hll_event_used?, <<~PATTERN
+ (send _ :track_redis_hll_event ...)
+ PATTERN
+
+ def on_send(node)
+ return unless track_redis_hll_event_used?(node)
+
+ add_offense(node, location: :selector)
+ end
+ end
+ end
+ end
+end
diff --git a/rubocop/cop/gitlab/feature_available_usage.rb b/rubocop/cop/gitlab/feature_available_usage.rb
index b50bdd8ca43..f748b7d9111 100644
--- a/rubocop/cop/gitlab/feature_available_usage.rb
+++ b/rubocop/cop/gitlab/feature_available_usage.rb
@@ -23,6 +23,9 @@ module RuboCop
operations
security_and_compliance
container_registry
+ environments
+ feature_flags
+ releases
].freeze
EE_FEATURES = %i[requirements].freeze
ALL_FEATURES = (FEATURES + EE_FEATURES).freeze
diff --git a/rubocop/cop/gitlab/mark_used_feature_flags.rb b/rubocop/cop/gitlab/mark_used_feature_flags.rb
index 0bebd7901f3..63bccec31c0 100644
--- a/rubocop/cop/gitlab/mark_used_feature_flags.rb
+++ b/rubocop/cop/gitlab/mark_used_feature_flags.rb
@@ -145,7 +145,7 @@ module RuboCop
return unless node.children[opts_index]
node.children[opts_index].each_pair.find do |pair|
- pair.key.value == :feature_flag
+ pair.key.value == :_deprecated_feature_flag
end&.value
else
arg_index = rugged_method?(node) ? 3 : 2
diff --git a/rubocop/cop/migration/add_limit_to_text_columns.rb b/rubocop/cop/migration/add_limit_to_text_columns.rb
index b5780e87c19..a47fbe0bf16 100644
--- a/rubocop/cop/migration/add_limit_to_text_columns.rb
+++ b/rubocop/cop/migration/add_limit_to_text_columns.rb
@@ -104,7 +104,7 @@ module RuboCop
block_node = node.each_ancestor(:block).first
create_table_node = block_node
.children
- .find { |n| TABLE_METHODS.include?(n.children[1])}
+ .find { |n| TABLE_METHODS.include?(n.children[1]) }
if create_table_node
table_name = create_table_node.children[2].value
diff --git a/rubocop/cop/scalability/bulk_perform_with_context.rb b/rubocop/cop/scalability/bulk_perform_with_context.rb
index b96aa35bfee..bb944b2ad62 100644
--- a/rubocop/cop/scalability/bulk_perform_with_context.rb
+++ b/rubocop/cop/scalability/bulk_perform_with_context.rb
@@ -20,7 +20,7 @@ module RuboCop
being scheduled, please disable this cop with a comment explaing which
context will be applied.
- Read more about it https://docs.gitlab.com/ee/development/sidekiq_style_guide.html#worker-context
+ Read more about it https://docs.gitlab.com/ee/development/sidekiq/logging.html#worker-context
MSG
BACKGROUND_MIGRATION_WORKER_NAMES = %w[BackgroundMigrationWorker CiDatabaseWorker].freeze
diff --git a/rubocop/cop_todo.rb b/rubocop/cop_todo.rb
new file mode 100644
index 00000000000..42e2f9fbe13
--- /dev/null
+++ b/rubocop/cop_todo.rb
@@ -0,0 +1,49 @@
+# frozen_string_literal: true
+
+module RuboCop
+ class CopTodo
+ attr_accessor :previously_disabled
+
+ attr_reader :cop_name, :files, :offense_count
+
+ def initialize(cop_name)
+ @cop_name = cop_name
+ @files = Set.new
+ @offense_count = 0
+ @cop_class = self.class.find_cop_by_name(cop_name)
+ @previously_disabled = false
+ end
+
+ def record(file, offense_count)
+ @files << file
+ @offense_count += offense_count
+ end
+
+ def autocorrectable?
+ @cop_class&.support_autocorrect?
+ end
+
+ def to_yaml
+ yaml = []
+ yaml << '---'
+ yaml << '# Cop supports --auto-correct.' if autocorrectable?
+ yaml << "#{cop_name}:"
+
+ if previously_disabled
+ yaml << " # Offense count: #{offense_count}"
+ yaml << ' # Temporarily disabled due to too many offenses'
+ yaml << ' Enabled: false'
+ end
+
+ yaml << ' Exclude:'
+ yaml.concat files.sort.map { |file| " - '#{file}'" }
+ yaml << ''
+
+ yaml.join("\n")
+ end
+
+ def self.find_cop_by_name(cop_name)
+ RuboCop::Cop::Registry.global.find_by_cop_name(cop_name)
+ end
+ end
+end
diff --git a/rubocop/formatter/todo_formatter.rb b/rubocop/formatter/todo_formatter.rb
index 662cc1551ff..789d0418f96 100644
--- a/rubocop/formatter/todo_formatter.rb
+++ b/rubocop/formatter/todo_formatter.rb
@@ -5,6 +5,7 @@ require 'rubocop'
require 'yaml'
require_relative '../todo_dir'
+require_relative '../cop_todo'
module RuboCop
module Formatter
@@ -14,26 +15,6 @@ module RuboCop
# For example, this formatter stores offenses for `RSpec/VariableName`
# in `.rubocop_todo/rspec/variable_name.yml`.
class TodoFormatter < BaseFormatter
- class Todo
- attr_reader :cop_name, :files, :offense_count
-
- def initialize(cop_name)
- @cop_name = cop_name
- @files = Set.new
- @offense_count = 0
- @cop_class = RuboCop::Cop::Registry.global.find_by_cop_name(cop_name)
- end
-
- def record(file, offense_count)
- @files << file
- @offense_count += offense_count
- end
-
- def autocorrectable?
- @cop_class&.support_autocorrect?
- end
- end
-
DEFAULT_BASE_DIRECTORY = File.expand_path('../../.rubocop_todo', __dir__)
class << self
@@ -44,7 +25,7 @@ module RuboCop
def initialize(output, _options = {})
@directory = self.class.base_directory
- @todos = Hash.new { |hash, cop_name| hash[cop_name] = Todo.new(cop_name) }
+ @todos = Hash.new { |hash, cop_name| hash[cop_name] = CopTodo.new(cop_name) }
@todo_dir = TodoDir.new(directory)
@config_inspect_todo_dir = load_config_inspect_todo_dir
@config_old_todo_yml = load_config_old_todo_yml
@@ -65,8 +46,8 @@ module RuboCop
def finished(_inspected_files)
@todos.values.sort_by(&:cop_name).each do |todo|
- yaml = to_yaml(todo)
- path = @todo_dir.write(todo.cop_name, yaml)
+ todo.previously_disabled = previously_disabled?(todo)
+ path = @todo_dir.write(todo.cop_name, todo.to_yaml)
output.puts "Written to #{relative_path(path)}\n"
end
@@ -90,27 +71,6 @@ module RuboCop
path.delete_prefix("#{parent}/")
end
- def to_yaml(todo)
- yaml = []
- yaml << '---'
- yaml << '# Cop supports --auto-correct.' if todo.autocorrectable?
- yaml << "#{todo.cop_name}:"
-
- if previously_disabled?(todo)
- yaml << " # Offense count: #{todo.offense_count}"
- yaml << ' # Temporarily disabled due to too many offenses'
- yaml << ' Enabled: false'
- end
-
- yaml << ' Exclude:'
-
- files = todo.files.sort.map { |file| " - '#{file}'" }
- yaml.concat files
- yaml << ''
-
- yaml.join("\n")
- end
-
def check_multiple_configurations!
cop_names = @config_inspect_todo_dir.keys & @config_old_todo_yml.keys
return if cop_names.empty?