forked from github/rubocop-github
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rails_view_render_shorthand.rb
38 lines (31 loc) · 1.16 KB
/
rails_view_render_shorthand.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
# frozen_string_literal: true
require "rubocop"
module RuboCop
module Cop
module GitHub
class RailsViewRenderShorthand < Base
MSG = "Prefer `render` partial shorthand"
def_node_matcher :render_with_options?, <<-PATTERN
(send nil? {:render :render_to_string} (hash $...))
PATTERN
def_node_matcher :partial_key?, <<-PATTERN
(pair (sym :partial) $(str _))
PATTERN
def_node_matcher :locals_key?, <<-PATTERN
(pair (sym :locals) $_)
PATTERN
def on_send(node)
if option_pairs = render_with_options?(node)
partial_key = option_pairs.map { |pair| partial_key?(pair) }.compact.first
locals_key = option_pairs.map { |pair| locals_key?(pair) }.compact.first
if option_pairs.length == 1 && partial_key
add_offense(node, message: "Use `render #{partial_key.source}` instead")
elsif option_pairs.length == 2 && partial_key && locals_key
add_offense(node, message: "Use `render #{partial_key.source}, #{locals_key.source}` instead")
end
end
end
end
end
end
end