forked from rubocop/rubocop-rails
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfile_path.rb
125 lines (103 loc) · 3.95 KB
/
file_path.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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# frozen_string_literal: true
module RuboCop
module Cop
module Rails
# Identifies usages of file path joining process to use `Rails.root.join` clause.
# It is used to add uniformity when joining paths.
#
# @example EnforcedStyle: slashes (default)
# # bad
# Rails.root.join('app', 'models', 'goober')
#
# # good
# Rails.root.join('app/models/goober')
#
# # bad
# File.join(Rails.root, 'app/models/goober')
# "#{Rails.root}/app/models/goober"
#
# # good
# Rails.root.join('app/models/goober').to_s
#
# @example EnforcedStyle: arguments
# # bad
# Rails.root.join('app/models/goober')
#
# # good
# Rails.root.join('app', 'models', 'goober')
#
# # bad
# File.join(Rails.root, 'app/models/goober')
# "#{Rails.root}/app/models/goober"
#
# # good
# Rails.root.join('app', 'models', 'goober').to_s
#
class FilePath < Base
include ConfigurableEnforcedStyle
include RangeHelp
MSG_SLASHES = 'Prefer `Rails.root.join(\'path/to\')%<to_s>s`.'
MSG_ARGUMENTS = 'Prefer `Rails.root.join(\'path\', \'to\')%<to_s>s`.'
RESTRICT_ON_SEND = %i[join].freeze
def_node_matcher :file_join_nodes?, <<~PATTERN
(send (const {nil? cbase} :File) :join ...)
PATTERN
def_node_search :rails_root_nodes?, <<~PATTERN
(send (const {nil? cbase} :Rails) :root)
PATTERN
def_node_matcher :rails_root_join_nodes?, <<~PATTERN
(send #rails_root_nodes? :join ...)
PATTERN
def on_dstr(node)
return unless rails_root_nodes?(node)
return unless node.children.last.str_type?
last_child_source = node.children.last.source
return unless last_child_source.start_with?('.') || last_child_source.include?(File::SEPARATOR)
return if last_child_source.start_with?(':')
register_offense(node, require_to_s: true)
end
def on_send(node)
check_for_file_join_with_rails_root(node)
check_for_rails_root_join_with_slash_separated_path(node)
check_for_rails_root_join_with_string_arguments(node)
end
private
def check_for_file_join_with_rails_root(node)
return unless file_join_nodes?(node)
return unless node.arguments.any? { |e| rails_root_nodes?(e) }
register_offense(node, require_to_s: true)
end
def check_for_rails_root_join_with_string_arguments(node)
return unless style == :slashes
return unless rails_root_nodes?(node)
return unless rails_root_join_nodes?(node)
return unless node.arguments.size > 1
return unless node.arguments.all?(&:str_type?)
register_offense(node, require_to_s: false)
end
def check_for_rails_root_join_with_slash_separated_path(node)
return unless style == :arguments
return unless rails_root_nodes?(node)
return unless rails_root_join_nodes?(node)
return unless node.arguments.any? { |arg| string_with_slash?(arg) }
register_offense(node, require_to_s: false)
end
def string_with_slash?(node)
node.str_type? && node.source.include?('/')
end
def register_offense(node, require_to_s:)
line_range = node.loc.column...node.loc.last_column
source_range = source_range(processed_source.buffer, node.first_line, line_range)
require_to_s = false if node.dstr_type?
message = build_message(require_to_s)
add_offense(source_range, message: message)
end
def build_message(require_to_s)
message_template = style == :arguments ? MSG_ARGUMENTS : MSG_SLASHES
to_s = require_to_s ? '.to_s' : ''
format(message_template, to_s: to_s)
end
end
end
end
end