forked from rubocop/rubocop-rails
/
action_controller_flash_before_render.rb
98 lines (79 loc) · 2.63 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
# 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? :ApplicationController)
(const (const nil? :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_assigment_node = find_ancestor(flash_node, type: :send)
context = flash_assigment_node.parent
context.each_child_node.any? do |node|
render?(node)
end
end
def inherit_action_controller_base?(node)
class_node = find_ancestor(node, type: :class)
return 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 find_ancestor(node, type:)
node.each_ancestor(type).first
end
end
end
end
end