forked from rubocop/rubocop-rails
-
Notifications
You must be signed in to change notification settings - Fork 0
/
action_controller_flash_before_render.rb
114 lines (92 loc) · 3.26 KB
/
action_controller_flash_before_render.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# frozen_string_literal: true
module RuboCop
module Cop
module Rails
# Using `flash` assignment before `render` in Rails controllers 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
# class HomeController < ApplicationController
# def create
# flash[:alert] = "msg"
# render :index
# end
# end
#
# # good
# class HomeController < ApplicationController
# def create
# flash.now[:alert] = "msg"
# render :index
# end
# end
#
class ActionControllerFlashBeforeRender < Base
extend 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
def_node_search :action_controller?, <<~PATTERN
{
(const {nil? cbase} :ApplicationController)
(const (const {nil? cbase} :ActionController) :Base)
}
PATTERN
RESTRICT_ON_SEND = [:flash].freeze
def on_send(flash_node)
return unless flash_assignment?(flash_node)
return unless followed_by_render?(flash_node)
return unless instance_method_or_block?(flash_node)
return unless inherit_action_controller_base?(flash_node)
add_offense(flash_node) do |corrector|
corrector.replace(flash_node, 'flash.now')
end
end
private
def followed_by_render?(flash_node)
flash_assignment_node = find_ancestor(flash_node, type: :send)
context = flash_assignment_node
if (node = context.each_ancestor(:if, :rescue).first)
return false if use_redirect_to?(context)
context = node.rescue_type? ? node.parent : node
end
siblings = context.right_siblings
return true if siblings.empty?
siblings.compact.any? do |render_candidate|
render?(render_candidate)
end
end
def inherit_action_controller_base?(node)
class_node = find_ancestor(node, type: :class)
return false unless class_node
action_controller?(class_node)
end
def instance_method_or_block?(node)
def_node = find_ancestor(node, type: :def)
block_node = find_ancestor(node, type: :block)
def_node || block_node
end
def use_redirect_to?(context)
context.right_siblings.compact.any? do |sibling|
# Unwrap `return redirect_to :index`
sibling = sibling.children.first if sibling.return_type? && sibling.children.one?
sibling.send_type? && sibling.method?(:redirect_to)
end
end
def find_ancestor(node, type:)
node.each_ancestor(type).first
end
end
end
end
end