Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add
Rails/ActionControllerFlashBeforeRender
cop
- Loading branch information
1 parent
bceaa02
commit bd1d63d
Showing
5 changed files
with
90 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
* [#759](https://github.com/rubocop/rubocop-rails/pull/759): Add new `Rails/ActionControllerFlashBeforeRender` cop. ([@americodls][]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
48 changes: 48 additions & 0 deletions
48
lib/rubocop/cop/rails/action_controller_flash_before_render.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
# frozen_string_literal: true | ||
|
||
module RuboCop | ||
module Cop | ||
module Rails | ||
# Using `flash` assignment before `render` will persist the message for too long. | ||
# Check https://guides.rubyonrails.org/action_controller_overview.html#flash-now | ||
# | ||
# @example | ||
# # bad | ||
# flash[:alert] = "Warning!" | ||
# render :index | ||
# | ||
# # good | ||
# flash.now[:alert] = "Warning!" | ||
# render :index | ||
# | ||
class ActionControllerFlashBeforeRender < Base | ||
extend AutoCorrector | ||
|
||
MSG = 'Use `flash.now` before `render`.' | ||
|
||
def_node_search :flash_before_render?, <<~PATTERN | ||
^(send (send nil? :flash) :[]= ...) | ||
PATTERN | ||
|
||
def_node_search :render?, <<~PATTERN | ||
(send nil? :render ...) | ||
PATTERN | ||
|
||
RESTRICT_ON_SEND = [:flash].freeze | ||
|
||
def on_send(node) | ||
expression = flash_before_render?(node) | ||
|
||
return unless expression | ||
|
||
context = node.parent.parent | ||
return unless context.descendants.any? { render?(_1) } | ||
|
||
add_offense(node) do |corrector| | ||
corrector.replace(node, 'flash.now') | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
30 changes: 30 additions & 0 deletions
30
spec/rubocop/cop/rails/action_controller_flash_before_render_spec.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
# frozen_string_literal: true | ||
|
||
RSpec.describe RuboCop::Cop::Rails::ActionControllerFlashBeforeRender, :config do | ||
it 'registers an offense when using `flash` before `render`' do | ||
expect_offense(<<~RUBY) | ||
flash[:alert] = "msg" | ||
^^^^^ Use `flash.now` before `render`. | ||
render :index | ||
RUBY | ||
|
||
expect_correction(<<~RUBY) | ||
flash.now[:alert] = "msg" | ||
render :index | ||
RUBY | ||
end | ||
|
||
it 'does not register an offense when using `flash.now` before `render`' do | ||
expect_no_offenses(<<~RUBY) | ||
flash.now[:alert] = "msg" | ||
render :index | ||
RUBY | ||
end | ||
|
||
it 'does not register an offense when using `flash` before `redirect_to`' do | ||
expect_no_offenses(<<~RUBY) | ||
flash[:alert] = "msg" | ||
redirect_to "https://www.rubygems.org" | ||
RUBY | ||
end | ||
end |