Skip to content

Commit

Permalink
Merge pull request #59 from viralpraxis/rename-instance-variable-in-c…
Browse files Browse the repository at this point in the history
…lass-method

Rename `ThreadSafety::InstanceVariableInClassMethod` cop to `ThreadSafety::ClassInstanceVariable`
  • Loading branch information
viralpraxis authored Oct 1, 2024
2 parents f404a29 + 77c65ac commit 696e9c0
Show file tree
Hide file tree
Showing 8 changed files with 59 additions and 52 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

## master (unreleased)

* [#59](https://github.com/rubocop/rubocop-thread_safety/pull/59): Rename `ThreadSafety::InstanceVariableInClassMethod` cop to `ThreadSafety::ClassInstanceVariable` to better reflect its purpose. ([@viralpraxis](https://github.com/viralpraxis))
* [#55](https://github.com/rubocop/rubocop-thread_safety/pull/55): Enhance `ThreadSafety::InstanceVariableInClassMethod` cop to detect offenses within `class_eval/exec` blocks. ([@viralpraxis](https://github.com/viralpraxis))
* [#54](https://github.com/rubocop/rubocop-thread_safety/pull/54): Drop support for RuboCop older than 1.48. ([@viralpraxis](https://github.com/viralpraxis))
* [#52](https://github.com/rubocop/rubocop-thread_safety/pull/52): Add new `RackMiddlewareInstanceVariable` cop to detect instance variables in Rack middleware. ([@viralpraxis](https://github.com/viralpraxis))
Expand Down
2 changes: 2 additions & 0 deletions config/obsoletion.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
renamed:
ThreadSafety/InstanceVariableInClassMethod: ThreadSafety/ClassInstanceVariable
2 changes: 1 addition & 1 deletion docs/modules/ROOT/pages/cops.adoc
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
=== Department xref:cops_threadsafety.adoc[ThreadSafety]

* xref:cops_threadsafety.adoc#threadsafetyclassandmoduleattributes[ThreadSafety/ClassAndModuleAttributes]
* xref:cops_threadsafety.adoc#threadsafetyinstancevariableinclassmethod[ThreadSafety/InstanceVariableInClassMethod]
* xref:cops_threadsafety.adoc#threadsafetyclassinstancevariable[ThreadSafety/ClassInstanceVariable]
* xref:cops_threadsafety.adoc#threadsafetymutableclassinstancevariable[ThreadSafety/MutableClassInstanceVariable]
* xref:cops_threadsafety.adoc#threadsafetynewthread[ThreadSafety/NewThread]
* xref:cops_threadsafety.adoc#threadsafetydirchdir[ThreadSafety/DirChdir]
Expand Down
54 changes: 27 additions & 27 deletions docs/modules/ROOT/pages/cops_threadsafety.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -42,32 +42,7 @@ end
| Boolean
|===
== ThreadSafety/DirChdir
|===
| Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed
| Enabled
| Yes
| No
| -
| -
|===
Avoid using `Dir.chdir` due to its process-wide effect.
=== Examples
[source,ruby]
----
# bad
Dir.chdir("/var/run")
# bad
FileUtils.chdir("/var/run")
----
== ThreadSafety/InstanceVariableInClassMethod
== ThreadSafety/ClassInstanceVariable
|===
| Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed
Expand All @@ -79,7 +54,7 @@ FileUtils.chdir("/var/run")
| -
|===
Avoid instance variables in class methods.
Avoid class instance variables.
=== Examples
Expand Down Expand Up @@ -142,6 +117,31 @@ module Example
end
----
== ThreadSafety/DirChdir
|===
| Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed
| Enabled
| Yes
| No
| -
| -
|===
Avoid using `Dir.chdir` due to its process-wide effect.
=== Examples
[source,ruby]
----
# bad
Dir.chdir("/var/run")
# bad
FileUtils.chdir("/var/run")
----
== ThreadSafety/MutableClassInstanceVariable
|===
Expand Down
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
2 changes: 2 additions & 0 deletions lib/rubocop/thread_safety.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,7 @@ module ThreadSafety
CONFIG = YAML.safe_load(CONFIG_DEFAULT.read).freeze

private_constant(:CONFIG_DEFAULT, :PROJECT_ROOT)

::RuboCop::ConfigObsoletion.files << PROJECT_ROOT.join('config', 'obsoletion.yml')
end
end
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 696e9c0

Please sign in to comment.