Rails/LexicallyScopedActionFilter gives false positive when before action used with conditional statement
No offenses.
app/controllers/api/test.rb:4:3: C: Rails/LexicallyScopedActionFilter: update, cancel are not explicitly defined on the controller.
before_action(:authenticate, only: %i[update cancel]) unless foo
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Use this test class in the /controllers directory
# frozen_string_literal: true
class Test < ActionController
before_action(:authenticate, only: %i[update cancel]) unless foo
def update; end
def cancel; end
end
$ rubocop -V
0.52.1 (using Parser 2.4.0.2, running on ruby 2.5.0 x86_64-linux)
Thanks for the bug report!
The cause is here. https://github.com/bbatsov/rubocop/blob/87f453d20f789fb4cb233a661dbed8794502022a/lib/rubocop/cop/rails/lexically_scoped_action_filter.rb#L70
The code expects node.parent is a class node, but the parent is a if node in this case. So we should replace parent with each_ancestor or something.
What about filters inserted via mixins? Will they be addressed too when this issue is resolved?
In those cases, the parent is a module instead of a class:
module FooMixin
extend ActiveSupport::Concern
included do
before_action proc { authenticate },
only: :foo
end
def foo; end
end
class BarController < ActionController
include FooMixin
end
Most helpful comment
What about filters inserted via mixins? Will they be addressed too when this issue is resolved?
In those cases, the parent is a module instead of a class: