Skip to content

Commit

Permalink
[Fix rubocop#1078] Fix false negative for `Rails/LexicallyScopedActio…
Browse files Browse the repository at this point in the history
…nFilter`
  • Loading branch information
vlad-pisanov committed Aug 31, 2023
1 parent 8e43737 commit eb76249
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 8 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* [#1078](https://github.com/rubocop/rubocop-rails/issues/1078): Fix a false negative for `Rails/LexicallyScopedActionFilter` when no methods are defined. ([@vlad-pisanov][])
15 changes: 7 additions & 8 deletions lib/rubocop/cop/rails/lexically_scoped_action_filter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -122,24 +122,23 @@ def on_send(node)
parent = node.each_ancestor(:class, :module).first
return unless parent

# NOTE: a `:begin` node may not exist if the class/module consists of a single statement
block = parent.each_child_node(:begin).first
return unless block

defined_action_methods = defined_action_methods(block)

methods = array_values(methods_node).reject do |method|
defined_action_methods.include?(method)
end
unmatched_methods = array_values(methods_node) - defined_action_methods
return if unmatched_methods.empty?

message = message(methods, parent)
add_offense(node, message: message) unless methods.empty?
message = message(unmatched_methods, parent)
add_offense(node, message: message)
end

private

def defined_action_methods(block)
defined_methods = block.each_child_node(:def).map(&:method_name)
return [] unless block

defined_methods = block.each_child_node(:def).map(&:method_name)
defined_methods + aliased_action_methods(block, defined_methods)
end

Expand Down
9 changes: 9 additions & 0 deletions spec/rubocop/cop/rails/lexically_scoped_action_filter_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,15 @@ def index
RUBY
end

it 'registers an offense when no methods are defined' do
expect_offense <<~RUBY
class LoginController < ApplicationController
before_action :require_login, only: %i[index show]
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ `index`, `show` are not explicitly defined on the class.
end
RUBY
end

it 'register an offense when using action filter in module' do
expect_offense <<~RUBY
module FooMixin
Expand Down

0 comments on commit eb76249

Please sign in to comment.