Skip to content

Commit

Permalink
Use Cop::Base API for ThreadSafety department
Browse files Browse the repository at this point in the history
Follow rubocop/rubocop#7868.

The legacy `Cop::Cop` API is soft deprecated and this PR use new `Cop::Base` API instead.

> maintain any RuboCop extensions, as the legacy API will be removed in RuboCop 2.0.

https://metaredux.com/posts/2020/10/21/rubocop-1-0.html

This new API requires RuboCop 0.87 or higher.
https://github.com/rubocop/rubocop/blob/master/CHANGELOG.md#0870-2020-07-06

That dependent version update will be resolved in rubocop#7, so there are no updates in this PR.
  • Loading branch information
koic committed May 18, 2022
1 parent 79d2dcf commit 8cfb15d
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 21 deletions.
Expand Up @@ -12,7 +12,7 @@ module ThreadSafety
# class User
# cattr_accessor :current_user
# end
class ClassAndModuleAttributes < Cop
class ClassAndModuleAttributes < Base
MSG = 'Avoid mutating class and module attributes.'
RESTRICT_ON_SEND = %i[
mattr_writer mattr_accessor cattr_writer cattr_accessor
Expand Down
Expand Up @@ -53,7 +53,7 @@ module ThreadSafety
#
# module_function :test
# end
class InstanceVariableInClassMethod < Cop
class InstanceVariableInClassMethod < Base
MSG = 'Avoid instance variables in class methods.'
RESTRICT_ON_SEND = %i[
instance_variable_set
Expand All @@ -73,7 +73,7 @@ def on_ivar(node)
return if method_definition?(node)
return if synchronized?(node)

add_offense(node, location: :name, message: MSG)
add_offense(node.loc.name, message: MSG)
end
alias on_ivasgn on_ivar

Expand Down
37 changes: 20 additions & 17 deletions lib/rubocop/cop/thread_safety/mutable_class_instance_variable.rb
Expand Up @@ -72,7 +72,8 @@ module ThreadSafety
# end
# end.freeze
# end
class MutableClassInstanceVariable < Cop
class MutableClassInstanceVariable < Base
extend AutoCorrector
include FrozenStringLiteral
include ConfigurableEnforcedStyle

Expand Down Expand Up @@ -108,23 +109,21 @@ def on_masgn(node)
end
end

def autocorrect(node)
def autocorrect(corrector, node)
expr = node.source_range

lambda do |corrector|
splat_value = splat_value(node)
if splat_value
correct_splat_expansion(corrector, expr, splat_value)
elsif node.array_type? && !node.bracketed?
corrector.insert_before(expr, '[')
corrector.insert_after(expr, ']')
elsif requires_parentheses?(node)
corrector.insert_before(expr, '(')
corrector.insert_after(expr, ')')
end

corrector.insert_after(expr, '.freeze')
splat_value = splat_value(node)
if splat_value
correct_splat_expansion(corrector, expr, splat_value)
elsif node.array_type? && !node.bracketed?
corrector.insert_before(expr, '[')
corrector.insert_after(expr, ']')
elsif requires_parentheses?(node)
corrector.insert_before(expr, '(')
corrector.insert_after(expr, ')')
end

corrector.insert_after(expr, '.freeze')
end

private
Expand Down Expand Up @@ -152,15 +151,19 @@ def strict_check(value)
return if operation_produces_threadsafe_object?(value)
return if frozen_string_literal?(value)

add_offense(value)
add_offense(value) do |corrector|
autocorrect(corrector, value)
end
end

def check(value)
return unless mutable_literal?(value) ||
range_enclosed_in_parentheses?(value)
return if frozen_string_literal?(value)

add_offense(value)
add_offense(value) do |corrector|
autocorrect(corrector, value)
end
end

def in_class?(node)
Expand Down
2 changes: 1 addition & 1 deletion lib/rubocop/cop/thread_safety/new_thread.rb
Expand Up @@ -10,7 +10,7 @@ module ThreadSafety
# @example
# # bad
# Thread.new { do_work }
class NewThread < Cop
class NewThread < Base
MSG = 'Avoid starting new threads.'
RESTRICT_ON_SEND = %i[new].freeze

Expand Down

0 comments on commit 8cfb15d

Please sign in to comment.