forked from github/rubocop-github
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rails_controller_render_literal.rb
94 lines (79 loc) · 2.33 KB
/
rails_controller_render_literal.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
# frozen_string_literal: true
require "rubocop"
module RuboCop
module Cop
module GitHub
class RailsControllerRenderLiteral < Cop
MSG = "render must be used with a string literal"
def_node_matcher :literal?, <<-PATTERN
({str sym true false nil?} ...)
PATTERN
def_node_matcher :render?, <<-PATTERN
(send nil? :render ...)
PATTERN
def_node_matcher :render_literal?, <<-PATTERN
(send nil? :render ({str sym} $_) $...)
PATTERN
def_node_matcher :render_with_options?, <<-PATTERN
(send nil? :render (hash $...))
PATTERN
def_node_matcher :ignore_key?, <<-PATTERN
(pair (sym {
:body
:file
:html
:inline
:js
:json
:nothing
:plain
:text
:xml
}) $_)
PATTERN
def_node_matcher :template_key?, <<-PATTERN
(pair (sym {
:action
:partial
:template
}) $_)
PATTERN
def_node_matcher :layout_key?, <<-PATTERN
(pair (sym :layout) $_)
PATTERN
def_node_matcher :options_key?, <<-PATTERN
(pair (sym {
:content_type
:location
:status
:formats
}) ...)
PATTERN
def on_send(node)
return unless render?(node)
if render_literal?(node)
elsif option_pairs = render_with_options?(node)
option_pairs = option_pairs.reject { |pair| options_key?(pair) }
if option_pairs.any? { |pair| ignore_key?(pair) }
return
end
if template_node = option_pairs.map { |pair| template_key?(pair) }.compact.first
if !literal?(template_node)
add_offense(node, location: :expression)
end
else
add_offense(node, location: :expression)
end
if layout_node = option_pairs.map { |pair| layout_key?(pair) }.compact.first
if !literal?(layout_node)
add_offense(node, location: :expression)
end
end
else
add_offense(node, location: :expression)
end
end
end
end
end
end