forked from github/rubocop-github
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrails_controller_render_action_symbol.rb
43 lines (36 loc) · 1.15 KB
/
rails_controller_render_action_symbol.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
# frozen_string_literal: true
require "rubocop"
module RuboCop
module Cop
module GitHub
class RailsControllerRenderActionSymbol < Cop
MSG = "Prefer `render` with string instead of symbol"
def_node_matcher :render_sym?, <<-PATTERN
(send nil? {:render :render_to_string} $(sym _))
PATTERN
def_node_matcher :render_with_options?, <<-PATTERN
(send nil? {:render :render_to_string} (hash $...))
PATTERN
def_node_matcher :action_key?, <<-PATTERN
(pair (sym {:action :template}) $(sym _))
PATTERN
def on_send(node)
if sym_node = render_sym?(node)
add_offense(sym_node, location: :expression)
elsif option_pairs = render_with_options?(node)
option_pairs.each do |pair|
if sym_node = action_key?(pair)
add_offense(sym_node, location: :expression)
end
end
end
end
def autocorrect(node)
lambda do |corrector|
corrector.replace(node.source_range, "\"#{node.children[0]}\"")
end
end
end
end
end
end