Skip to content

Commit

Permalink
Rename ThreadSafety::InstanceVariableInClassMethod to `ThreadSafety…
Browse files Browse the repository at this point in the history
…::ClassInstanceVariable`
  • Loading branch information
viralpraxis committed Sep 29, 2024
1 parent 1cb008a commit 15fcef5
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 24 deletions.
2 changes: 1 addition & 1 deletion lib/rubocop-thread_safety.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

require 'rubocop/cop/mixin/operation_with_threadsafe_result'

require 'rubocop/cop/thread_safety/instance_variable_in_class_method'
require 'rubocop/cop/thread_safety/class_instance_variable'
require 'rubocop/cop/thread_safety/class_and_module_attributes'
require 'rubocop/cop/thread_safety/mutable_class_instance_variable'
require 'rubocop/cop/thread_safety/new_thread'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
module RuboCop
module Cop
module ThreadSafety
# Avoid instance variables in class methods.
# Avoid class instance variables.
#
# @example
# # bad
Expand Down Expand Up @@ -61,8 +61,8 @@ module ThreadSafety
#
# module_function :test
# end
class InstanceVariableInClassMethod < Base
MSG = 'Avoid instance variables in class methods.'
class ClassInstanceVariable < Base
MSG = 'Avoid class instance variables.'
RESTRICT_ON_SEND = %i[
instance_variable_set
instance_variable_get
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
# frozen_string_literal: true

RSpec.describe RuboCop::Cop::ThreadSafety::InstanceVariableInClassMethod, :config do
RSpec.describe RuboCop::Cop::ThreadSafety::ClassInstanceVariable, :config do
let(:msg) { 'Avoid class instance variables.' }

it 'registers an offense for assigning to an ivar in a class method' do
expect_offense(<<~RUBY)
class Test
def self.some_method(params)
@params = params
^^^^^^^ Avoid instance variables in class methods.
^^^^^^^ #{msg}
end
end
RUBY
Expand Down Expand Up @@ -112,7 +114,7 @@ class Test
Class.new do
def self.area
@area ||= some_computation
^^^^^ Avoid instance variables in class methods.
^^^^^ #{msg}
end
end
end
Expand All @@ -125,7 +127,7 @@ def self.area
class Test
def self.some_method
do_work(@params)
^^^^^^^ Avoid instance variables in class methods.
^^^^^^^ #{msg}
end
end
RUBY
Expand All @@ -136,7 +138,7 @@ def self.some_method
module ClassMethods
def some_method(params)
@params = params
^^^^^^^ Avoid instance variables in class methods.
^^^^^^^ #{msg}
end
end
RUBY
Expand All @@ -148,7 +150,7 @@ module Test
class_methods do
def some_method(params)
@params = params
^^^^^^^ Avoid instance variables in class methods.
^^^^^^^ #{msg}
end
end
end
Expand All @@ -161,7 +163,7 @@ class Test
class << self
def some_method(params)
@params = params
^^^^^^^ Avoid instance variables in class methods.
^^^^^^^ #{msg}
end
end
end
Expand All @@ -173,7 +175,7 @@ def some_method(params)
class Test
define_singleton_method(:some_method) do |params|
@params = params
^^^^^^^ Avoid instance variables in class methods.
^^^^^^^ #{msg}
end
end
RUBY
Expand All @@ -184,7 +186,7 @@ class Test
class Test
def self.some_method
do_work(instance_variable_get(:@params))
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Avoid instance variables in class methods.
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ #{msg}
end
end
RUBY
Expand All @@ -196,7 +198,7 @@ class Test
class << self
def some_method(name, params)
instance_variable_set(:"@\#{name}", params)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Avoid instance variables in class methods.
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ #{msg}
end
end
end
Expand All @@ -208,7 +210,7 @@ def some_method(name, params)
module ClassMethods
def some_method(params)
instance_variable_set(:@params, params)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Avoid instance variables in class methods.
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ #{msg}
end
end
RUBY
Expand All @@ -220,7 +222,7 @@ module Test
class_methods do
def some_method(params)
instance_variable_set(:@params, params)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Avoid instance variables in class methods.
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ #{msg}
end
end
end
Expand All @@ -232,7 +234,7 @@ def some_method(params)
class Test
define_singleton_method(:some_method) do |params|
instance_variable_set(:@params, params)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Avoid instance variables in class methods.
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ #{msg}
end
end
RUBY
Expand All @@ -245,7 +247,7 @@ module Test
def some_method(params)
instance_variable_set(:@params, params)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Avoid instance variables in class methods.
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ #{msg}
end
end
RUBY
Expand All @@ -256,7 +258,7 @@ def some_method(params)
module Test
def some_method(params)
instance_variable_set(:@params, params)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Avoid instance variables in class methods.
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ #{msg}
end
module_function :some_method
Expand All @@ -271,7 +273,7 @@ module Test
def some_method(params)
@params = params
^^^^^^^ Avoid instance variables in class methods.
^^^^^^^ #{msg}
end
end
RUBY
Expand All @@ -282,7 +284,7 @@ def some_method(params)
module Test
def some_method(params)
@params = params
^^^^^^^ Avoid instance variables in class methods.
^^^^^^^ #{msg}
end
module_function :some_method
Expand All @@ -295,7 +297,7 @@ def some_method(params)
def separate_with(separator)
Example.class_eval do
@separator = separator
^^^^^^^^^^ Avoid instance variables in class methods.
^^^^^^^^^^ #{msg}
end
end
RUBY
Expand All @@ -304,7 +306,7 @@ def separate_with(separator)
def separate_with(separator)
::Example.class_eval do
@separator = separator
^^^^^^^^^^ Avoid instance variables in class methods.
^^^^^^^^^^ #{msg}
end
end
RUBY
Expand All @@ -315,7 +317,7 @@ def separate_with(separator)
def separate_with(separator)
Example.class_exec do
@separator = separator
^^^^^^^^^^ Avoid instance variables in class methods.
^^^^^^^^^^ #{msg}
end
end
RUBY
Expand Down

0 comments on commit 15fcef5

Please sign in to comment.