11# frozen_string_literal: true
22
33require "rubocop"
4+ require "rubocop/cop/github/render_literal_helpers"
45
56module RuboCop
67 module Cop
78 module GitHub
89 class RailsControllerRenderLiteral < Cop
9- MSG = "render must be used with a string literal or an instance of a Class"
10-
11- def_node_matcher :literal? , <<-PATTERN
12- ({str sym true false nil?} ...)
13- PATTERN
14-
15- def_node_matcher :render? , <<-PATTERN
16- (send nil? {:render :render_to_string} ...)
17- PATTERN
18-
19- def_node_matcher :render_literal? , <<-PATTERN
20- (send nil? {:render :render_to_string} ({str sym} $_) $...)
21- PATTERN
22-
23- def_node_matcher :render_const? , <<-PATTERN
24- (send nil? {:render :render_to_string} (const _ _) ...)
25- PATTERN
26-
27- def_node_matcher :render_inst? , <<-PATTERN
28- (send nil? {:render :render_to_string} (send _ :new ...) ...)
29- PATTERN
10+ include RenderLiteralHelpers
3011
31- def_node_matcher :render_with_options? , <<-PATTERN
32- (send nil? {:render :render_to_string} (hash $...))
33- PATTERN
12+ MSG = "render must be used with a string literal or an instance of a Class"
3413
3514 def_node_matcher :ignore_key? , <<-PATTERN
3615 (pair (sym {
@@ -68,10 +47,16 @@ class RailsControllerRenderLiteral < Cop
6847 }) ...)
6948 PATTERN
7049
50+ def_node_matcher :render_const? , <<-PATTERN
51+ (send nil? {:render :render_to_string} (const _ _) ...)
52+ PATTERN
53+
7154 def on_send ( node )
7255 return unless render? ( node )
7356
74- if render_literal? ( node ) || render_inst? ( node ) || render_const? ( node )
57+ return if render_view_component? ( node ) || render_const? ( node )
58+
59+ if render_literal? ( node )
7560 elsif option_pairs = render_with_options? ( node )
7661 option_pairs = option_pairs . reject { |pair | options_key? ( pair ) }
7762
@@ -82,18 +67,40 @@ def on_send(node)
8267 if template_node = option_pairs . map { |pair | template_key? ( pair ) } . compact . first
8368 if !literal? ( template_node )
8469 add_offense ( node , location : :expression )
70+ return
8571 end
8672 else
8773 add_offense ( node , location : :expression )
74+ return
8875 end
8976
9077 if layout_node = option_pairs . map { |pair | layout_key? ( pair ) } . compact . first
9178 if !literal? ( layout_node )
9279 add_offense ( node , location : :expression )
80+ return
9381 end
9482 end
9583 else
9684 add_offense ( node , location : :expression )
85+ return
86+ end
87+
88+ if render_literal? ( node )
89+ option_hash = node . arguments [ 1 ]
90+ if option_hash && !option_hash . hash_type?
91+ add_offense ( node , location : :expression )
92+ return
93+ end
94+ option_pairs = option_hash && option_hash . pairs
95+ else
96+ option_pairs = node . arguments [ 0 ] . pairs
97+ end
98+
99+ if option_pairs
100+ locals = option_pairs . map { |pair | locals_key? ( pair ) } . compact . first
101+ if locals && ( !locals . hash_type? || !hash_with_literal_keys? ( locals ) )
102+ add_offense ( node , location : :expression )
103+ end
97104 end
98105 end
99106 end
0 commit comments