Skip to content
代码片段 群组 项目
提交 5384c9b2 编辑于 作者: Peter Leitzen's avatar Peter Leitzen 提交者: Marc Shaw
浏览文件

Update existing cops

The major changes here are:

1. Inherit from `RuboCop::Cop::Base`, not `RuboCop::Cop::Cop`.
2. New auto-correction style - `extend RuboCop::Cop::AutoCorrector`,
   pass a block to `add_offense` rather than defining an `autocorrect`
   method.
3. `add_offense` no longer takes a `location` keyword argument.
4. Specs should not set `subject(:cop)` as that will be done
   automatically, and will use the config correctly.
上级 2084713b
No related branches found
No related tags found
无相关合并请求
显示
40 个添加44 个删除
......@@ -163,7 +163,7 @@ def disallow_send_to(node, suffix, message)
each_send_node(node) do |send_node|
next unless send_receiver_name_ends_with?(send_node, suffix)
add_offense(send_node, location: :expression, message: message)
add_offense(send_node, message: message)
end
end
......
......@@ -6,7 +6,7 @@ module Cop
# in preparation to upgrade to Rails 6.1
#
# See https://gitlab.com/gitlab-org/gitlab/-/issues/225874
class ActiveModelErrorsDirectManipulation < RuboCop::Cop::Cop
class ActiveModelErrorsDirectManipulation < RuboCop::Cop::Base
MSG = 'Avoid manipulating errors hash directly. For more details check https://gitlab.com/gitlab-org/gitlab/-/issues/225874'
MANIPULATIVE_METHODS = ":<< :append :clear :collect! :compact! :concat :delete :delete_at :delete_if :drop :drop_while :fill :filter! :keep_if :flatten! :insert :map! :pop :prepend :push :reject! :replace :reverse! :rotate! :select! :shift :shuffle! :slice! :sort! :sort_by! :uniq! :unshift"
......@@ -50,10 +50,10 @@ class ActiveModelErrorsDirectManipulation < RuboCop::Cop::Cop
PATTERN
def on_send(node)
add_offense(node, location: :expression) if active_model_errors_root_assignment?(node)
add_offense(node, location: :expression) if active_model_errors_root_manipulation?(node)
add_offense(node, location: :expression) if active_model_errors_manipulation?(node)
add_offense(node, location: :expression) if active_model_errors_assignment?(node)
add_offense(node) if active_model_errors_root_assignment?(node)
add_offense(node) if active_model_errors_root_manipulation?(node)
add_offense(node) if active_model_errors_manipulation?(node)
add_offense(node) if active_model_errors_assignment?(node)
end
end
end
......
......@@ -3,7 +3,7 @@
module RuboCop
module Cop
# Cop that blacklists the use of `reload`.
class ActiveRecordAssociationReload < RuboCop::Cop::Cop
class ActiveRecordAssociationReload < RuboCop::Cop::Base
MSG = 'Use reset instead of reload. ' \
'For more details check the https://gitlab.com/gitlab-org/gitlab-foss/issues/60218.'
......@@ -14,7 +14,7 @@ class ActiveRecordAssociationReload < RuboCop::Cop::Cop
def on_send(node)
return unless reload?(node)
add_offense(node, location: :selector)
add_offense(node.loc.selector)
end
end
end
......
......@@ -3,7 +3,9 @@
module RuboCop
module Cop
module API
class Base < RuboCop::Cop::Cop
class Base < RuboCop::Cop::Base
extend RuboCop::Cop::AutoCorrector
# This cop checks that APIs subclass API::Base.
#
# @example
......@@ -38,13 +40,9 @@ class Base < RuboCop::Cop::Cop
def on_class(node)
grape_api_definition(node) do
add_offense(node.children[1])
end
end
def autocorrect(node)
lambda do |corrector|
corrector.replace(node, '::API::Base')
add_offense(node.children[1]) do |corrector|
corrector.replace(node.children[1], '::API::Base')
end
end
end
end
......
......@@ -3,7 +3,7 @@
module RuboCop
module Cop
module API
class GrapeArrayMissingCoerce < RuboCop::Cop::Cop
class GrapeArrayMissingCoerce < RuboCop::Cop::Base
# This cop checks that Grape API parameters using an Array type
# implement a coerce_with method:
#
......
......@@ -9,7 +9,7 @@ module Cop
# problems, even when a developer eager loaded all necessary associations.
#
# See https://gitlab.com/gitlab-org/gitlab/-/issues/23182 for more information.
class AvoidBecomes < RuboCop::Cop::Cop
class AvoidBecomes < RuboCop::Cop::Base
MSG = 'Avoid the use of becomes(SomeConstant), as this creates a ' \
'new object and throws away any eager loaded associations. ' \
'When creating URLs in views, just use the path helpers directly. ' \
......@@ -23,7 +23,7 @@ class AvoidBecomes < RuboCop::Cop::Cop
PATTERN
def on_send(node)
add_offense(node, location: :expression) if becomes?(node)
add_offense(node) if becomes?(node)
end
end
end
......
......@@ -20,7 +20,7 @@ module Cop
# do_an_heavy_calculation
# end
#
class AvoidBreakFromStrongMemoize < RuboCop::Cop::Cop
class AvoidBreakFromStrongMemoize < RuboCop::Cop::Base
MSG = 'Do not use break inside strong_memoize, use next instead.'
def on_block(node)
......
......@@ -3,7 +3,7 @@
module RuboCop
module Cop
# Cop that blacklists keyword arguments usage in Sidekiq workers
class AvoidKeywordArgumentsInSidekiqWorkers < RuboCop::Cop::Cop
class AvoidKeywordArgumentsInSidekiqWorkers < RuboCop::Cop::Base
MSG = "Do not use keyword arguments in Sidekiq workers. " \
"For details, check https://github.com/mperham/sidekiq/issues/2372"
OBSERVED_METHOD = :perform
......@@ -13,7 +13,7 @@ def on_def(node)
node.arguments.each do |argument|
if argument.type == :kwarg || argument.type == :kwoptarg
add_offense(node, location: :expression)
add_offense(node)
end
end
end
......
......@@ -20,7 +20,7 @@ module Cop
# do_something_else
# end
#
class AvoidReturnFromBlocks < RuboCop::Cop::Cop
class AvoidReturnFromBlocks < RuboCop::Cop::Base
MSG = 'Do not return from a block, use next or break instead.'
DEF_METHODS = %i[define_method lambda].freeze
WHITELISTED_METHODS = %i[each each_filename times loop].freeze
......
......@@ -13,7 +13,9 @@ module Cop
# root to: redirect('-/autocomplete/users')
#
class AvoidRouteRedirectLeadingSlash < RuboCop::Cop::Cop
class AvoidRouteRedirectLeadingSlash < RuboCop::Cop::Base
extend RuboCop::Cop::AutoCorrector
MSG = 'Do not use a leading "/" in route redirects'
def_node_matcher :leading_slash_in_redirect?, <<~PATTERN
......@@ -24,7 +26,9 @@ def on_send(node)
return unless in_routes?(node)
return unless leading_slash_in_redirect?(node)
add_offense(node)
add_offense(node) do |corrector|
corrector.replace(node.loc.expression, remove_leading_slash(node))
end
end
def has_leading_slash?(str)
......@@ -38,12 +42,6 @@ def in_routes?(node)
dirname.end_with?('config/routes') || filename.end_with?('routes.rb')
end
def autocorrect(node)
lambda do |corrector|
corrector.replace(node.loc.expression, remove_leading_slash(node))
end
end
def remove_leading_slash(node)
node.source.sub('/', '')
end
......
......@@ -19,7 +19,7 @@ module Cop
# # ...
# end
#
class BanCatchThrow < RuboCop::Cop::Cop
class BanCatchThrow < RuboCop::Cop::Base
MSG = "Do not use catch or throw unless a gem's API demands it."
def on_send(node)
......@@ -27,7 +27,7 @@ def on_send(node)
return unless receiver.nil? && %i[catch throw].include?(method_name)
add_offense(node, location: :expression)
add_offense(node)
end
end
end
......
......@@ -6,7 +6,7 @@ module RuboCop
module Cop
module CodeReuse
# Cop that enforces various code reuse rules for Finders.
class Finder < RuboCop::Cop::Cop
class Finder < RuboCop::Cop::Base
include CodeReuseHelpers
IN_FINDER = 'Finders can not be used inside a Finder.'
......
......@@ -6,7 +6,7 @@ module RuboCop
module Cop
module CodeReuse
# Cop that enforces various code reuse rules for Presenter classes.
class Presenter < RuboCop::Cop::Cop
class Presenter < RuboCop::Cop::Base
include CodeReuseHelpers
IN_SERVICE = 'Presenters can not be used in a Service class.'
......
......@@ -6,7 +6,7 @@ module RuboCop
module Cop
module CodeReuse
# Cop that enforces various code reuse rules for Serializer classes.
class Serializer < RuboCop::Cop::Cop
class Serializer < RuboCop::Cop::Base
include CodeReuseHelpers
IN_SERVICE = 'Serializers can not be used in a Service class.'
......
......@@ -6,7 +6,7 @@ module RuboCop
module Cop
module CodeReuse
# Cop that enforces various code reuse rules for Service classes.
class ServiceClass < RuboCop::Cop::Cop
class ServiceClass < RuboCop::Cop::Base
include CodeReuseHelpers
IN_FINDER = 'Service classes can not be used in a Finder.'
......
......@@ -6,7 +6,7 @@ module RuboCop
module Cop
module CodeReuse
# Cop that enforces various code reuse rules for workers.
class Worker < RuboCop::Cop::Cop
class Worker < RuboCop::Cop::Base
include CodeReuseHelpers
IN_CONTROLLER = 'Workers can not be used in a controller.'
......
......@@ -4,7 +4,7 @@ module RuboCop
module Cop
module Database
# Cop that checks if 'disable_referential_integrity' method is called.
class DisableReferentialIntegrity < RuboCop::Cop::Cop
class DisableReferentialIntegrity < RuboCop::Cop::Base
MSG = <<~TEXT
Do not use `disable_referential_integrity`, disable triggers in a safe
transaction instead. Follow the format:
......
......@@ -3,7 +3,7 @@
module RuboCop
module Cop
module Database
class EstablishConnection < RuboCop::Cop::Cop
class EstablishConnection < RuboCop::Cop::Base
MSG = "Don't establish new database connections, as this slows down " \
'tests and may result in new connections using an incorrect configuration'
......@@ -12,7 +12,7 @@ class EstablishConnection < RuboCop::Cop::Cop
PATTERN
def on_send(node)
add_offense(node, location: :expression) if establish_connection?(node)
add_offense(node) if establish_connection?(node)
end
end
end
......
......@@ -10,7 +10,7 @@ module Database
# # good
# ApplicationRecord.connection
#
class MultipleDatabases < RuboCop::Cop::Cop
class MultipleDatabases < RuboCop::Cop::Base
AR_BASE_MESSAGE = <<~EOF
Do not use methods from ActiveRecord::Base, use the ApplicationRecord class instead
For fixing offenses related to the ActiveRecord::Base.transaction method, see our guidelines:
......@@ -32,7 +32,7 @@ def on_send(node)
active_record_base_method = node.children[1]
return if method_is_allowed?(active_record_base_method)
add_offense(node, location: :expression, message: AR_BASE_MESSAGE)
add_offense(node, message: AR_BASE_MESSAGE)
end
private
......
......@@ -20,7 +20,7 @@ module Database
# # good
#
# run_cheap_queries_with_each_batch
class RescueQueryCanceled < RuboCop::Cop::Cop
class RescueQueryCanceled < RuboCop::Cop::Base
MSG = <<~EOF
Avoid rescuing the `ActiveRecord::QueryCanceled` class.
......
0% 加载中 .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册