forked from github/rubocop-github
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rails_view_render_paths_exist.rb
59 lines (49 loc) · 1.62 KB
/
rails_view_render_paths_exist.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
# frozen_string_literal: true
require "rubocop"
module RuboCop
module Cop
module GitHub
class RailsViewRenderPathsExist < Cop
def_node_matcher :render?, <<-PATTERN
(send nil? :render $...)
PATTERN
def_node_matcher :render_str?, <<-PATTERN
(send nil? :render $(str $_) ...)
PATTERN
def_node_matcher :render_options?, <<-PATTERN
(send nil? :render (hash $...))
PATTERN
def_node_matcher :partial_key?, <<-PATTERN
(pair (sym :partial) $(str $_))
PATTERN
def on_send(node)
return unless cop_config["ViewPath"]
if args = render_str?(node)
node, path = args
unless resolve_partial(path.to_s)
add_offense(node, location: :expression, message: "Partial template could not be found")
end
elsif pairs = render_options?(node)
if pair = pairs.detect { |p| partial_key?(p) }
node, path = partial_key?(pair)
unless resolve_partial(path.to_s)
add_offense(node, location: :expression, message: "Partial template could not be found")
end
end
end
end
def resolve_partial(path)
parts = path.split(File::SEPARATOR)
parts << "_#{parts.pop}"
path = parts.join(File::SEPARATOR)
cop_config["ViewPath"].each do |view_path|
if m = Dir[File.join(config.path_relative_to_config(view_path), path) + "*"].first
return m
end
end
nil
end
end
end
end
end