Skip to content

Commit

Permalink
Merge pull request #844 from koic/fix_a_false_positive_for_rails_acti…
Browse files Browse the repository at this point in the history
…on_controller_flash_before_render

[Fix #843] Fix a false positive for `Rails/ActionControllerFlashBeforeRender`
  • Loading branch information
koic authored Oct 27, 2022
2 parents fe0ae4d + 1cb5789 commit 0d430fe
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* [#843](https://github.com/rubocop/rubocop-rails/issues/843): Fix a false positive for `Rails/ActionControllerFlashBeforeRender` when using `flash` in multiline `if` branch before `redirect_to`. ([@koic][])
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,12 @@ def on_send(flash_node)
def followed_by_render?(flash_node)
flash_assigment_node = find_ancestor(flash_node, type: :send)
context = flash_assigment_node
context = context.parent if context.parent.if_type?
if (if_node = context.each_ancestor(:if).first)
context = if_node
elsif context.right_siblings.empty?
return true
end
context = context.right_siblings
return true if context.empty?

context.compact.any? do |node|
render?(node)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,50 @@ def create
end
RUBY
end

it 'registers an offense when using `flash` in multiline `if` branch before `render_to`' do
expect_offense(<<~RUBY)
class HomeController < #{parent_class}
def create
if condition
do_something
flash[:alert] = "msg"
^^^^^ Use `flash.now` before `render`.
end
render :index
end
end
RUBY

expect_correction(<<~RUBY)
class HomeController < #{parent_class}
def create
if condition
do_something
flash.now[:alert] = "msg"
end
render :index
end
end
RUBY
end

it 'does not register an offense when using `flash` in multiline `if` branch before `redirect_to`' do
expect_no_offenses(<<~RUBY)
class HomeController < #{parent_class}
def create
if condition
do_something
flash[:alert] = "msg"
end
redirect_to :index
end
end
RUBY
end
end
end

Expand Down

0 comments on commit 0d430fe

Please sign in to comment.