diff options
Diffstat (limited to 'rubocop/cop/gitlab')
22 files changed, 136 insertions, 154 deletions
diff --git a/rubocop/cop/gitlab/avoid_feature_category_not_owned.rb b/rubocop/cop/gitlab/avoid_feature_category_not_owned.rb index fb790f44a96..05d7824f92e 100644 --- a/rubocop/cop/gitlab/avoid_feature_category_not_owned.rb +++ b/rubocop/cop/gitlab/avoid_feature_category_not_owned.rb @@ -5,7 +5,7 @@ require_relative '../../code_reuse_helpers' module RuboCop module Cop module Gitlab - class AvoidFeatureCategoryNotOwned < RuboCop::Cop::Cop + class AvoidFeatureCategoryNotOwned < RuboCop::Cop::Base include ::RuboCop::CodeReuseHelpers MSG = 'Avoid adding new endpoints with `feature_category :not_owned`. See https://docs.gitlab.com/ee/development/feature_categorization' @@ -25,7 +25,7 @@ module RuboCop return unless file_needs_feature_category?(node) return unless setting_not_owned?(node) - add_offense(node, location: :expression) + add_offense(node) end private diff --git a/rubocop/cop/gitlab/avoid_feature_get.rb b/rubocop/cop/gitlab/avoid_feature_get.rb index 1bce89268d9..68aaff8aeff 100644 --- a/rubocop/cop/gitlab/avoid_feature_get.rb +++ b/rubocop/cop/gitlab/avoid_feature_get.rb @@ -3,23 +3,35 @@ module RuboCop module Cop module Gitlab - # Cop that blacklists the use of `Feature.get`. - class AvoidFeatureGet < RuboCop::Cop::Cop + # Bans the use of `Feature.get`. + # + # @example + # + # # bad + # + # Feature.get(:x).enable + # Feature.get(:x).enable_percentage_of_time(100) + # Feature.get(:x).remove + # + # # good + # + # stub_feature_flags(x: true) + # Feature.enable(:x) + # Feature.enable_percentage_of_time(:x, 100) + # Feature.remove(:x) + # + class AvoidFeatureGet < RuboCop::Cop::Base MSG = 'Use `stub_feature_flags` method instead of `Feature.get`. ' \ 'See doc/development/feature_flags/index.md#feature-flags-in-tests for more information.' def_node_matcher :feature_get?, <<~PATTERN - (send (const nil? :Feature) :get ...) - PATTERN - - def_node_matcher :global_feature_get?, <<~PATTERN - (send (const (cbase) :Feature) :get ...) + (send (const {nil? cbase} :Feature) :get ...) PATTERN def on_send(node) - return unless feature_get?(node) || global_feature_get?(node) + return unless feature_get?(node) - add_offense(node, location: :selector) + add_offense(node.loc.selector) end end end diff --git a/rubocop/cop/gitlab/avoid_uploaded_file_from_params.rb b/rubocop/cop/gitlab/avoid_uploaded_file_from_params.rb index 0795f96732d..cb3382c1b75 100644 --- a/rubocop/cop/gitlab/avoid_uploaded_file_from_params.rb +++ b/rubocop/cop/gitlab/avoid_uploaded_file_from_params.rb @@ -33,7 +33,7 @@ module RuboCop # uploaded_file = declared_params[:file] # end # end - class AvoidUploadedFileFromParams < RuboCop::Cop::Cop + class AvoidUploadedFileFromParams < RuboCop::Cop::Base MSG = 'Use the `UploadedFile` set by `multipart.rb` instead of calling `UploadedFile.from_params` directly. See https://docs.gitlab.com/ee/development/uploads/working_with_uploads.html' def_node_matcher :calling_uploaded_file_from_params?, <<~PATTERN @@ -43,7 +43,7 @@ module RuboCop def on_send(node) return unless calling_uploaded_file_from_params?(node) - add_offense(node, location: :expression) + add_offense(node) end end end diff --git a/rubocop/cop/gitlab/bulk_insert.rb b/rubocop/cop/gitlab/bulk_insert.rb index baaefc2533c..f88dcde2dc2 100644 --- a/rubocop/cop/gitlab/bulk_insert.rb +++ b/rubocop/cop/gitlab/bulk_insert.rb @@ -5,7 +5,7 @@ module RuboCop module Gitlab # Cop that disallows the use of `legacy_bulk_insert`, in favour of using # the `BulkInsertSafe` module. - class BulkInsert < RuboCop::Cop::Cop + class BulkInsert < RuboCop::Cop::Base MSG = 'Use the `BulkInsertSafe` concern, instead of using `LegacyBulkInsert.bulk_insert`. See https://docs.gitlab.com/ee/development/insert_into_tables_in_batches.html' def_node_matcher :raw_union?, <<~PATTERN @@ -15,7 +15,7 @@ module RuboCop def on_send(node) return unless raw_union?(node) - add_offense(node, location: :expression) + add_offense(node) end end end diff --git a/rubocop/cop/gitlab/change_timezone.rb b/rubocop/cop/gitlab/change_timezone.rb index c30a057d51c..a5d9393b6c3 100644 --- a/rubocop/cop/gitlab/change_timezone.rb +++ b/rubocop/cop/gitlab/change_timezone.rb @@ -3,7 +3,7 @@ module RuboCop module Cop module Gitlab - class ChangeTimezone < RuboCop::Cop::Cop + class ChangeTimezone < RuboCop::Cop::Base MSG = "Do not change timezone in the runtime (application or rspec), " \ "it could result in silently modifying other behavior." diff --git a/rubocop/cop/gitlab/const_get_inherit_false.rb b/rubocop/cop/gitlab/const_get_inherit_false.rb index 3d3bbc4c8d3..e44097a8d9e 100644 --- a/rubocop/cop/gitlab/const_get_inherit_false.rb +++ b/rubocop/cop/gitlab/const_get_inherit_false.rb @@ -6,7 +6,9 @@ module RuboCop # Cop that encourages usage of inherit=false for 2nd argument when using const_get. # # See https://gitlab.com/gitlab-org/gitlab/issues/27678 - class ConstGetInheritFalse < RuboCop::Cop::Cop + class ConstGetInheritFalse < RuboCop::Cop::Base + extend RuboCop::Cop::AutoCorrector + MSG = 'Use inherit=false when using const_get.' def_node_matcher :const_get?, <<~PATTERN @@ -17,11 +19,7 @@ module RuboCop return unless const_get?(node) return if second_argument(node)&.false_type? - add_offense(node, location: :selector) - end - - def autocorrect(node) - lambda do |corrector| + add_offense(node.loc.selector) do |corrector| if arg = second_argument(node) corrector.replace(arg.source_range, 'false') else diff --git a/rubocop/cop/gitlab/delegate_predicate_methods.rb b/rubocop/cop/gitlab/delegate_predicate_methods.rb index 43b5184faab..9e6a2823719 100644 --- a/rubocop/cop/gitlab/delegate_predicate_methods.rb +++ b/rubocop/cop/gitlab/delegate_predicate_methods.rb @@ -21,7 +21,7 @@ module RuboCop # def is_foo? # !!bar&.is_foo? # end - class DelegatePredicateMethods < RuboCop::Cop::Cop + class DelegatePredicateMethods < RuboCop::Cop::Base MSG = "Using `delegate` with `allow_nil` on the following predicate methods is discouraged: %s." RESTRICT_ON_SEND = %i[delegate].freeze def_node_matcher :predicate_allow_nil_option, <<~PATTERN diff --git a/rubocop/cop/gitlab/deprecate_track_redis_hll_event.rb b/rubocop/cop/gitlab/deprecate_track_redis_hll_event.rb index 3e30f3aa4d0..58411357eeb 100644 --- a/rubocop/cop/gitlab/deprecate_track_redis_hll_event.rb +++ b/rubocop/cop/gitlab/deprecate_track_redis_hll_event.rb @@ -14,7 +14,7 @@ module RuboCop # # # good # track_event :show, name: 'g_analytics_valuestream', destinations: [:redis_hll] - class DeprecateTrackRedisHLLEvent < RuboCop::Cop::Cop + class DeprecateTrackRedisHLLEvent < RuboCop::Cop::Base 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' @@ -25,7 +25,7 @@ module RuboCop def on_send(node) return unless track_redis_hll_event_used?(node) - add_offense(node, location: :selector) + add_offense(node.loc.selector) end end end diff --git a/rubocop/cop/gitlab/event_store_subscriber.rb b/rubocop/cop/gitlab/event_store_subscriber.rb index 0b2dd94bc42..7e4cc3e66cc 100644 --- a/rubocop/cop/gitlab/event_store_subscriber.rb +++ b/rubocop/cop/gitlab/event_store_subscriber.rb @@ -30,7 +30,7 @@ module RuboCop # end # end # - class EventStoreSubscriber < RuboCop::Cop::Cop + class EventStoreSubscriber < RuboCop::Cop::Base SUBSCRIBER_MODULE_NAME = 'Gitlab::EventStore::Subscriber' FORBID_PERFORM_OVERRIDE = "Do not override `perform` in a `#{SUBSCRIBER_MODULE_NAME}`." REQUIRE_HANDLE_EVENT = "A `#{SUBSCRIBER_MODULE_NAME}` must implement `#handle_event(event)`." diff --git a/rubocop/cop/gitlab/except.rb b/rubocop/cop/gitlab/except.rb index 24da6962457..d20cf47b473 100644 --- a/rubocop/cop/gitlab/except.rb +++ b/rubocop/cop/gitlab/except.rb @@ -5,7 +5,7 @@ module RuboCop module Gitlab # Cop that disallows the use of `Gitlab::SQL::Except`, in favour of using # the `FromExcept` module. - class Except < RuboCop::Cop::Cop + class Except < RuboCop::Cop::Base MSG = 'Use the `FromExcept` concern, instead of using `Gitlab::SQL::Except` directly' def_node_matcher :raw_except?, <<~PATTERN @@ -15,7 +15,7 @@ module RuboCop def on_send(node) return unless raw_except?(node) - add_offense(node, location: :expression) + add_offense(node) end end end diff --git a/rubocop/cop/gitlab/feature_available_usage.rb b/rubocop/cop/gitlab/feature_available_usage.rb index f748b7d9111..4dba4baf1e7 100644 --- a/rubocop/cop/gitlab/feature_available_usage.rb +++ b/rubocop/cop/gitlab/feature_available_usage.rb @@ -4,7 +4,7 @@ module RuboCop module Cop module Gitlab # Cop that checks for correct calling of #feature_available? - class FeatureAvailableUsage < RuboCop::Cop::Cop + class FeatureAvailableUsage < RuboCop::Cop::Base OBSERVED_METHOD = :feature_available? LICENSED_FEATURE_LITERAL_ARG_MSG = '`feature_available?` should not be called for features that can be licensed (`%s` given), use `licensed_feature_available?(feature)` instead.' LICENSED_FEATURE_DYNAMIC_ARG_MSG = "`feature_available?` should not be called for features that can be licensed (`%s` isn't a literal so we cannot say if it's legit or not), using `licensed_feature_available?(feature)` may be more appropriate." @@ -21,6 +21,7 @@ module RuboCop metrics_dashboard analytics operations + monitor security_and_compliance container_registry environments @@ -38,9 +39,9 @@ module RuboCop return if ALL_FEATURES.include?(feature_name(node)) && args_count(node) == 2 if !ALL_FEATURES.include?(feature_name(node)) - add_offense(node, location: :expression, message: licensed_feature_message(node)) + add_offense(node, message: licensed_feature_message(node)) elsif args_count(node) < 2 - add_offense(node, location: :expression, message: NOT_ENOUGH_ARGS_MSG) + add_offense(node, message: NOT_ENOUGH_ARGS_MSG) end end diff --git a/rubocop/cop/gitlab/finder_with_find_by.rb b/rubocop/cop/gitlab/finder_with_find_by.rb index 8fa9fe4a2f9..ac454398f9c 100644 --- a/rubocop/cop/gitlab/finder_with_find_by.rb +++ b/rubocop/cop/gitlab/finder_with_find_by.rb @@ -3,8 +3,10 @@ module RuboCop module Cop module Gitlab - class FinderWithFindBy < RuboCop::Cop::Cop - FIND_PATTERN = /\Afind(_by\!?)?\z/.freeze + class FinderWithFindBy < RuboCop::Cop::Base + extend RuboCop::Cop::AutoCorrector + + FIND_PATTERN = /\Afind(_by!?)?\z/.freeze ALLOWED_MODULES = ['FinderMethods'].freeze def message(used_method) @@ -18,13 +20,9 @@ module RuboCop end def on_send(node) - if find_on_execute?(node) && !allowed_module?(node) - add_offense(node, location: :selector, message: message(node.method_name)) - end - end + return unless find_on_execute?(node) && !allowed_module?(node) - def autocorrect(node) - lambda do |corrector| + add_offense(node.loc.selector, message: message(node.method_name)) do |corrector| upto_including_execute = node.descendants.first.source_range before_execute = node.descendants[1].source_range range_to_remove = node.source_range diff --git a/rubocop/cop/gitlab/httparty.rb b/rubocop/cop/gitlab/httparty.rb index 20f0c381e11..f57c605ce91 100644 --- a/rubocop/cop/gitlab/httparty.rb +++ b/rubocop/cop/gitlab/httparty.rb @@ -3,7 +3,9 @@ module RuboCop module Cop module Gitlab - class HTTParty < RuboCop::Cop::Cop + class HTTParty < RuboCop::Cop::Base + extend RuboCop::Cop::AutoCorrector + MSG_SEND = <<~EOL Avoid calling `HTTParty` directly. Instead, use the Gitlab::HTTP wrapper. To allow request to localhost or the private network set @@ -25,31 +27,18 @@ module RuboCop PATTERN def on_send(node) - add_offense(node, location: :expression, message: MSG_SEND) if httparty_node?(node) - add_offense(node, location: :expression, message: MSG_INCLUDE) if includes_httparty?(node) - end - - def autocorrect(node) - if includes_httparty?(node) - autocorrect_includes_httparty(node) - else - autocorrect_httparty_node(node) - end - end - - def autocorrect_includes_httparty(node) - lambda do |corrector| - corrector.remove(node.source_range) - end - end - - def autocorrect_httparty_node(node) - _, method_name, *arg_nodes = *node - - replacement = "Gitlab::HTTP.#{method_name}(#{arg_nodes.map(&:source).join(', ')})" - - lambda do |corrector| - corrector.replace(node.source_range, replacement) + if httparty_node?(node) + add_offense(node, message: MSG_SEND) do |corrector| + _, method_name, *arg_nodes = *node + + replacement = "Gitlab::HTTP.#{method_name}(#{arg_nodes.map(&:source).join(', ')})" + + corrector.replace(node.source_range, replacement) + end + elsif includes_httparty?(node) + add_offense(node, message: MSG_INCLUDE) do |corrector| + corrector.remove(node.source_range) + end end end end diff --git a/rubocop/cop/gitlab/intersect.rb b/rubocop/cop/gitlab/intersect.rb index 4b61073b804..e608b25cbe1 100644 --- a/rubocop/cop/gitlab/intersect.rb +++ b/rubocop/cop/gitlab/intersect.rb @@ -5,7 +5,7 @@ module RuboCop module Gitlab # Cop that disallows the use of `Gitlab::SQL::Intersect`, in favour of using # the `FromIntersect` module. - class Intersect < RuboCop::Cop::Cop + class Intersect < RuboCop::Cop::Base MSG = 'Use the `FromIntersect` concern, instead of using `Gitlab::SQL::Intersect` directly' def_node_matcher :raw_intersect?, <<~PATTERN @@ -15,7 +15,7 @@ module RuboCop def on_send(node) return unless raw_intersect?(node) - add_offense(node, location: :expression) + add_offense(node) end end end diff --git a/rubocop/cop/gitlab/json.rb b/rubocop/cop/gitlab/json.rb index d2ba0012ca0..56846e3c276 100644 --- a/rubocop/cop/gitlab/json.rb +++ b/rubocop/cop/gitlab/json.rb @@ -3,7 +3,9 @@ module RuboCop module Cop module Gitlab - class Json < RuboCop::Cop::Cop + class Json < RuboCop::Cop::Base + extend RuboCop::Cop::AutoCorrector + MSG = <<~EOL Avoid calling `JSON` directly. Instead, use the `Gitlab::Json` wrapper. This allows us to alter the JSON parser being used. @@ -14,19 +16,13 @@ module RuboCop PATTERN def on_send(node) - add_offense(node) if json_node?(node) - end - - def autocorrect(node) - autocorrect_json_node(node) - end + return unless json_node?(node) - def autocorrect_json_node(node) - _, method_name, *arg_nodes = *node + add_offense(node) do |corrector| + _, method_name, *arg_nodes = *node - replacement = "Gitlab::Json.#{method_name}(#{arg_nodes.map(&:source).join(', ')})" + replacement = "Gitlab::Json.#{method_name}(#{arg_nodes.map(&:source).join(', ')})" - lambda do |corrector| corrector.replace(node.source_range, replacement) end end diff --git a/rubocop/cop/gitlab/keys_first_and_values_first.rb b/rubocop/cop/gitlab/keys_first_and_values_first.rb index e9bf266cdd7..0a1d525f07f 100644 --- a/rubocop/cop/gitlab/keys_first_and_values_first.rb +++ b/rubocop/cop/gitlab/keys_first_and_values_first.rb @@ -3,53 +3,54 @@ module RuboCop module Cop module Gitlab - class KeysFirstAndValuesFirst < RuboCop::Cop::Cop - FIRST_PATTERN = /\Afirst\z/.freeze - - def message(used_method) - <<~MSG - Don't use `.keys.first` and `.values.first`. - Instead use `.each_key.first` and `.each_value.first` (or `.first.first` and `first.second`) - - This will reduce memory usage and execution time. - MSG - end + # Detects the use of `.keys.first` or `.values.first` and suggests a + # change to `.each_key.first` or `.each_value.first`. This reduces + # memory usage and execution time. + # + # @example + # + # # bad + # + # hash.keys.first + # hash.values.first + # + # # good + # + # hash.each_key.first + # hash.each_value.first + # + class KeysFirstAndValuesFirst < RuboCop::Cop::Base + extend RuboCop::Cop::AutoCorrector + + MSG = 'Prefer `.%{autocorrect_method}.first` over `.%{current_method}.first`. ' \ + 'This reduces memory usage and execution time.' + + AUTOCORRECT_METHOD = { + keys: 'each_key', + values: 'each_value' + }.freeze + + def_node_matcher :keys_or_values_first, <<~PATTERN + (send (send ({send const hash lvar} ...) ${:keys :values}) :first) + PATTERN def on_send(node) - if find_on_keys_or_values?(node) - add_offense(node, location: :selector, message: message(node.method_name)) - end - end + current_method = keys_or_values_first(node) + return unless current_method + + autocorrect_method = AUTOCORRECT_METHOD.fetch(current_method) + msg = format(MSG, autocorrect_method: autocorrect_method, current_method: current_method) - def autocorrect(node) - lambda do |corrector| - replace_with = if node.descendants.first.method_name == :values - '.each_value' - elsif node.descendants.first.method_name == :keys - '.each_key' - else - throw("Expect '.values.first' or '.keys.first', but get #{node.descendants.first.method_name}.first") # rubocop:disable Cop/BanCatchThrow - end - - upto_including_keys_or_values = node.descendants.first.source_range - before_keys_or_values = node.descendants[1].source_range - range_to_replace = node.source_range - .with(begin_pos: before_keys_or_values.end_pos, - end_pos: upto_including_keys_or_values.end_pos) - corrector.replace(range_to_replace, replace_with) + add_offense(node.loc.selector, message: msg) do |corrector| + replacement = "#{autocorrect_expression(node)}.#{autocorrect_method}.first" + corrector.replace(node, replacement) end end - def find_on_keys_or_values?(node) - chained_on_node = node.descendants.first - node.method_name.to_s =~ FIRST_PATTERN && - chained_on_node.is_a?(RuboCop::AST::SendNode) && - [:keys, :values].include?(chained_on_node.method_name) && - node.descendants[1] - end + private - def method_name_for_node(node) - children[1].to_s + def autocorrect_expression(node) + node.receiver.receiver.source end end end diff --git a/rubocop/cop/gitlab/mark_used_feature_flags.rb b/rubocop/cop/gitlab/mark_used_feature_flags.rb index 63bccec31c0..8d8c84e78f5 100644 --- a/rubocop/cop/gitlab/mark_used_feature_flags.rb +++ b/rubocop/cop/gitlab/mark_used_feature_flags.rb @@ -9,7 +9,7 @@ module RuboCop # # The files set in `tmp/feature_flags/*.used` can then be used for verification purpose. # - class MarkUsedFeatureFlags < RuboCop::Cop::Cop + class MarkUsedFeatureFlags < RuboCop::Cop::Base include RuboCop::CodeReuseHelpers FEATURE_METHODS = %i[enabled? disabled?].freeze @@ -26,9 +26,6 @@ module RuboCop data_consistency deduplicate ].freeze - GRAPHQL_METHODS = %i[ - field - ].freeze SELF_METHODS = %i[ push_frontend_feature_flag push_force_frontend_feature_flag @@ -36,11 +33,12 @@ module RuboCop limit_feature_flag_for_override= ].freeze + EXPERIMENT_METHODS + RUGGED_METHODS + WORKER_METHODS - RESTRICT_ON_SEND = FEATURE_METHODS + EXPERIMENTATION_METHODS + GRAPHQL_METHODS + SELF_METHODS + RESTRICT_ON_SEND = FEATURE_METHODS + EXPERIMENTATION_METHODS + SELF_METHODS USAGE_DATA_COUNTERS_EVENTS_YAML_GLOBS = [ File.expand_path("../../../config/metrics/aggregates/*.yml", __dir__), - File.expand_path("../../../lib/gitlab/usage_data_counters/known_events/*.yml", __dir__) + File.expand_path("../../../lib/gitlab/usage_data_counters/known_events/*.yml", __dir__), + File.expand_path("../../../ee/lib/ee/gitlab/usage_data_counters/known_events/*.yml", __dir__) ].freeze class << self @@ -86,12 +84,12 @@ module RuboCop # Additionally, mark experiment-related feature flag as used as well matching_feature_flags = defined_feature_flags.select { |flag| flag == "#{flag_value}_experiment_percentage" } matching_feature_flags.each do |matching_feature_flag| - puts_if_ci(node, "The '#{matching_feature_flag}' feature flag tracks the #{flag_value} experiment, which is still in use, so we'll mark it as used.") + puts_if_debug(node, "The '#{matching_feature_flag}' feature flag tracks the #{flag_value} experiment, which is still in use, so we'll mark it as used.") save_used_feature_flag(matching_feature_flag) end end elsif flag_arg_is_send_type?(flag_arg) - puts_if_ci(node, "Feature flag is dynamic: '#{flag_value}.") + puts_if_debug(node, "Feature flag is dynamic: '#{flag_value}.") elsif flag_arg_is_dstr_or_dsym?(flag_arg) str_prefix = flag_arg.children[0] rest_children = flag_arg.children[1..] @@ -99,21 +97,23 @@ module RuboCop if rest_children.none? { |child| child.str_type? } matching_feature_flags = defined_feature_flags.select { |flag| flag.start_with?(str_prefix.value) } matching_feature_flags.each do |matching_feature_flag| - puts_if_ci(node, "The '#{matching_feature_flag}' feature flag starts with '#{str_prefix.value}', so we'll optimistically mark it as used.") + puts_if_debug(node, "The '#{matching_feature_flag}' feature flag starts with '#{str_prefix.value}', so we'll optimistically mark it as used.") save_used_feature_flag(matching_feature_flag) end else - puts_if_ci(node, "Interpolated feature flag name has multiple static string parts, we won't track it.") + puts_if_debug(node, "Interpolated feature flag name has multiple static string parts, we won't track it.") end else - puts_if_ci(node, "Feature flag has an unknown type: #{flag_arg.type}.") + puts_if_debug(node, "Feature flag has an unknown type: #{flag_arg.type}.") end end private - def puts_if_ci(node, text) - puts "#{text} (call: `#{node.source}`, source: #{node.location.expression.source_buffer.name})" if ENV['CI'] + def puts_if_debug(node, text) + return unless RuboCop::ConfigLoader.debug + + warn "#{text} (call: `#{node.source}`, source: #{node.location.expression.source_buffer.name})" end def save_used_feature_flag(feature_flag_name) @@ -138,15 +138,6 @@ module RuboCop node.children[3].each_pair.find do |pair| pair.key.value == :feature_flag end&.value - elsif graphql_method?(node) - return unless node.children.size > 3 - - opts_index = node.children[3].hash_type? ? 3 : 4 - return unless node.children[opts_index] - - node.children[opts_index].each_pair.find do |pair| - pair.key.value == :_deprecated_feature_flag - end&.value else arg_index = rugged_method?(node) ? 3 : 2 @@ -178,7 +169,7 @@ module RuboCop end def caller_is_feature?(node) - class_caller(node) == "Feature" + %w[Feature YamlProcessor::FeatureFlags].include?(class_caller(node)) end def caller_is_feature_gitaly?(node) @@ -209,16 +200,12 @@ module RuboCop WORKER_METHODS.include?(method_name(node)) end - def graphql_method?(node) - GRAPHQL_METHODS.include?(method_name(node)) && in_graphql_types?(node) - end - def self_method?(node) SELF_METHODS.include?(method_name(node)) && class_caller(node).empty? end def trackable_flag?(node) - feature_method?(node) || experimentation_method?(node) || graphql_method?(node) || self_method?(node) + feature_method?(node) || experimentation_method?(node) || self_method?(node) end # Marking all event's feature flags as used as Gitlab::UsageDataCounters::HLLRedisCounter.track_event{,context} diff --git a/rubocop/cop/gitlab/module_with_instance_variables.rb b/rubocop/cop/gitlab/module_with_instance_variables.rb index 40cdc0d3a57..e43ede61b7e 100644 --- a/rubocop/cop/gitlab/module_with_instance_variables.rb +++ b/rubocop/cop/gitlab/module_with_instance_variables.rb @@ -3,7 +3,7 @@ module RuboCop module Cop module Gitlab - class ModuleWithInstanceVariables < RuboCop::Cop::Cop + class ModuleWithInstanceVariables < RuboCop::Cop::Base MSG = <<~EOL Do not use instance variables in a module. Please read this for the rationale behind it: @@ -32,12 +32,12 @@ module RuboCop if only_ivar_or_assignment?(definition) # We don't allow if any other ivar is used definition.each_descendant(:ivar) do |offense| - add_offense(offense, location: :expression) + add_offense(offense) end # We allow initialize method and single ivar elsif !initialize_method?(definition) && !single_ivar?(definition) definition.each_descendant(:ivar, :ivasgn) do |offense| - add_offense(offense, location: :expression) + add_offense(offense) end end end diff --git a/rubocop/cop/gitlab/policy_rule_boolean.rb b/rubocop/cop/gitlab/policy_rule_boolean.rb index ca69eebab6e..ddf100fc8a1 100644 --- a/rubocop/cop/gitlab/policy_rule_boolean.rb +++ b/rubocop/cop/gitlab/policy_rule_boolean.rb @@ -22,7 +22,7 @@ module RuboCop # # good # rule { conducts_electricity & can?(:magnetize) }.enable :motor # rule { ~conducts_electricity & batteries }.enable :motor - class PolicyRuleBoolean < RuboCop::Cop::Cop + class PolicyRuleBoolean < RuboCop::Cop::Base def_node_search :has_and_operator?, <<~PATTERN (and ...) PATTERN diff --git a/rubocop/cop/gitlab/predicate_memoization.rb b/rubocop/cop/gitlab/predicate_memoization.rb index 4c851f90238..3fbd004655f 100644 --- a/rubocop/cop/gitlab/predicate_memoization.rb +++ b/rubocop/cop/gitlab/predicate_memoization.rb @@ -3,7 +3,7 @@ module RuboCop module Cop module Gitlab - class PredicateMemoization < RuboCop::Cop::Cop + class PredicateMemoization < RuboCop::Cop::Base MSG = <<~EOL Avoid using `@value ||= query` inside predicate methods in order to properly memoize `false` or `nil` values. diff --git a/rubocop/cop/gitlab/rails_logger.rb b/rubocop/cop/gitlab/rails_logger.rb index 5a1695ce56e..ae05491ca7d 100644 --- a/rubocop/cop/gitlab/rails_logger.rb +++ b/rubocop/cop/gitlab/rails_logger.rb @@ -38,7 +38,7 @@ module RuboCop def on_send(node) return unless rails_logger_log?(node) - add_offense(node, location: :expression) + add_offense(node) end end end diff --git a/rubocop/cop/gitlab/union.rb b/rubocop/cop/gitlab/union.rb index c44c847657b..36ec6bb9b7f 100644 --- a/rubocop/cop/gitlab/union.rb +++ b/rubocop/cop/gitlab/union.rb @@ -5,7 +5,7 @@ module RuboCop module Gitlab # Cop that disallows the use of `Gitlab::SQL::Union`, in favour of using # the `FromUnion` module. - class Union < RuboCop::Cop::Cop + class Union < RuboCop::Cop::Base MSG = 'Use the `FromUnion` concern, instead of using `Gitlab::SQL::Union` directly' def_node_matcher :raw_union?, <<~PATTERN @@ -15,7 +15,7 @@ module RuboCop def on_send(node) return unless raw_union?(node) - add_offense(node, location: :expression) + add_offense(node) end end end |