Skip to content

Commit

Permalink
Add AllowedIdentifiers configuration option
Browse files Browse the repository at this point in the history
  • Loading branch information
viralpraxis committed Sep 24, 2024
1 parent afe9f44 commit 561304a
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 2 deletions.
1 change: 1 addition & 0 deletions config/default.yml
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,4 @@ ThreadSafety/RackMiddlewareInstanceVariable:
- 'lib/middleware/**/*.rb'
- 'app/middlewares/**/*.rb'
- 'lib/middlewares/**/*.rb'
AllowedIdentifiers: []
4 changes: 4 additions & 0 deletions docs/modules/ROOT/pages/cops_threadsafety.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -333,4 +333,8 @@ end
| Include
| `+app/middleware/**/*.rb+`, `+lib/middleware/**/*.rb+`, `+app/middlewares/**/*.rb+`, `+lib/middlewares/**/*.rb+`
| Array
| AllowedIdentifiers
| `[]`
| Array
|===
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ module ThreadSafety
# end
# end
class RackMiddlewareInstanceVariable < Base
include AllowedIdentifiers
include OperationWithThreadsafeResult

MSG = 'Avoid instance variables in Rack middleware.'
Expand All @@ -68,8 +69,10 @@ def on_class(node)

node.each_node(:def) do |method_definition_node|
method_definition_node.each_node(:ivasgn, :ivar) do |ivar_node|
assignable, = ivar_node.to_a
next if assignable == application_variable || safe_variables.include?(assignable)
variable, = ivar_node.to_a
if variable == application_variable || safe_variables.include?(variable) || allowed_identifier?(variable)
next
end

add_offense ivar_node
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -316,4 +316,27 @@ def call(env)
RUBY
end
end

context 'with non-empty `AllowedIdentifiers` config' do
let(:cop_config) do
{ 'AllowedIdentifiers' => ['options'] }
end

it 'registers an offense' do
expect_offense(<<~RUBY)
class TestMiddleware
def call(env)
@app.call(env)
end
def initialize(app)
@app = app
@some_var = 2
^^^^^^^^^^^^^ #{msg}
@options = 1
end
end
RUBY
end
end
end

0 comments on commit 561304a

Please sign in to comment.