Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
eb05be4
commit 65388f3
Showing
6 changed files
with
55 additions
and
55 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
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
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
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
This file was deleted.
Oops, something went wrong.
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 | ||
|
||
# Using `flash` assignment before `render` will persist the message for too long. | ||
# Check https://guides.rubyonrails.org/action_controller_overview.html#flash-now | ||
# | ||
# @safety | ||
# This cop's autocorrection is unsafe because it replaces `flash` by `flash.now`. | ||
# Even though it is usually a mistake, it might be used intentionally. | ||
# | ||
# @example | ||
# # bad | ||
# flash[:alert] = "Warning!" | ||
# render :index | ||
# | ||
# # good | ||
# flash.now[:alert] = "Warning!" | ||
# render :index | ||
# | ||
class RuboCop::Cop::Rails::ActionControllerFlashBeforeRender < RuboCop::Cop::Cop | ||
extend RuboCop::Cop::AutoCorrector | ||
|
||
MSG = "Use `flash.now` before `render`." | ||
|
||
def_node_search :flash_assignment?, <<~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_assignment?(node) | ||
return unless expression | ||
|
||
context = node.parent.parent | ||
return unless context | ||
|
||
siblings = context.descendants | ||
return unless siblings.any? { |sibling| render?(sibling) } | ||
|
||
add_offense(node) do |corrector| | ||
corrector.replace(node, "flash.now") | ||
end | ||
end | ||
end |