/
root_pathname_methods.rb
194 lines (179 loc) · 4.87 KB
/
root_pathname_methods.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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
# frozen_string_literal: true
module RuboCop
module Cop
module Rails
# Use `Rails.root` IO methods instead of passing it to `File`.
#
# `Rails.root` is an instance of `Pathname`
# so we can apply many IO methods directly.
#
# This cop works best when used together with
# `Style/FileRead`, `Style/FileWrite` and `Rails/RootJoinChain`.
#
# @example
# # bad
# File.open(Rails.root.join('db', 'schema.rb'))
# File.open(Rails.root.join('db', 'schema.rb'), 'w')
# File.read(Rails.root.join('db', 'schema.rb'))
# File.binread(Rails.root.join('db', 'schema.rb'))
# File.write(Rails.root.join('db', 'schema.rb'), content)
# File.binwrite(Rails.root.join('db', 'schema.rb'), content)
#
# # good
# Rails.root.join('db', 'schema.rb').open
# Rails.root.join('db', 'schema.rb').open('w')
# Rails.root.join('db', 'schema.rb').read
# Rails.root.join('db', 'schema.rb').binread
# Rails.root.join('db', 'schema.rb').write(content)
# Rails.root.join('db', 'schema.rb').binwrite(content)
#
class RootPathnameMethods < Base
extend AutoCorrector
MSG = '`%<rails_root>s` is a `Pathname` so you can just append `#%<method>s`.'
DIR_METHODS = %i[
children
delete
each_child
empty?
entries
exist?
glob
mkdir
open
rmdir
unlink
].to_set.freeze
FILE_METHODS = %i[
atime
basename
binread
binwrite
birthtime
blockdev?
chardev?
chmod
chown
ctime
delete
directory?
dirname
empty?
executable?
executable_real?
exist?
expand_path
extname
file?
fnmatch
fnmatch?
ftype
grpowned?
join
lchmod
lchown
lstat
mtime
open
owned?
pipe?
read
readable?
readable_real?
readlines
readlink
realdirpath
realpath
rename
setgid?
setuid?
size
size?
socket?
split
stat
sticky?
symlink?
sysopen
truncate
unlink
utime
world_readable?
world_writable?
writable?
writable_real?
write
zero?
].to_set.freeze
FILE_TEST_METHODS = %i[
blockdev?
chardev?
directory?
empty?
executable?
executable_real?
exist?
file?
grpowned?
owned?
pipe?
readable?
readable_real?
setgid?
setuid?
size
size?
socket?
sticky?
symlink?
world_readable?
world_writable?
writable?
writable_real?
zero?
].to_set.freeze
FILE_UTILS_METHODS = %i[
chmod
chown
mkdir
mkpath
rmdir
rmtree
].to_set.freeze
RESTRICT_ON_SEND = (DIR_METHODS + FILE_METHODS + FILE_TEST_METHODS + FILE_UTILS_METHODS).to_set.freeze
def_node_matcher :pathname_method, <<~PATTERN
{
(send (const {nil? cbase} :Dir) $DIR_METHODS $_ $...)
(send (const {nil? cbase} {:IO :File}) $FILE_METHODS $_ $...)
(send (const {nil? cbase} :FileTest) $FILE_TEST_METHODS $_ $...)
(send (const {nil? cbase} :FileUtils) $FILE_UTILS_METHODS $_ $...)
}
PATTERN
def_node_matcher :rails_root_pathname?, <<~PATTERN
{
$#rails_root?
(send $#rails_root? :join ...)
}
PATTERN
# @!method rails_root?(node)
def_node_matcher :rails_root?, <<~PATTERN
(send (const {nil? cbase} :Rails) {:root :public_path})
PATTERN
def on_send(node)
evidence(node) do |method, path, args, rails_root|
add_offense(node, message: format(MSG, method: method, rails_root: rails_root.source)) do |corrector|
replacement = "#{path.source}.#{method}"
replacement += "(#{args.map(&:source).join(', ')})" unless args.empty?
corrector.replace(node, replacement)
end
end
end
private
def evidence(node)
return if node.method?(:open) && node.parent&.send_type?
return unless (method, path, args = pathname_method(node)) && (rails_root = rails_root_pathname?(path))
yield(method, path, args, rails_root)
end
end
end
end
end