New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add new Rails/RootPathnameMethods
cop
#587
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
* [#587](https://github.com/rubocop/rubocop-rails/pull/587): Add new `Rails/RootPathnameMethods` cop. ([@leoarnold][]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
developpment | ||
filetest |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
# frozen_string_literal: true | ||
|
||
RSpec.describe RuboCop::Cop::Rails::RootPathnameMethods, :config do | ||
{ | ||
Dir: described_class::DIR_METHODS, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's my personal preference, but I don't feel too confident when some collections from the code are used in tests directly. It may sound far-fetched, but what if someone accidentally removes a line from that array? Specs won't fail. |
||
File: described_class::FILE_METHODS, | ||
FileTest: described_class::FILE_TEST_METHODS, | ||
FileUtils: described_class::FILE_UTILS_METHODS, | ||
IO: described_class::FILE_METHODS | ||
}.each do |receiver, methods| | ||
methods.each do |method| | ||
it "registers an offense when using `#{receiver}.#{method}(Rails.public_path)` (if arity exists)" do | ||
expect_offense(<<~RUBY, receiver: receiver, method: method) | ||
%{receiver}.%{method}(Rails.public_path) | ||
^{receiver}^^{method}^^^^^^^^^^^^^^^^^^^ `Rails.public_path` is a `Pathname` so you can just append `#%{method}`. | ||
RUBY | ||
|
||
expect_correction(<<~RUBY) | ||
Rails.public_path.#{method} | ||
RUBY | ||
end | ||
|
||
it "registers an offense when using `::#{receiver}.#{method}(::Rails.root.join(...))` (if arity exists)" do | ||
expect_offense(<<~RUBY, receiver: receiver, method: method) | ||
::%{receiver}.%{method}(::Rails.root.join('db', 'schema.rb')) | ||
^^^{receiver}^^{method}^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ `::Rails.root` is a `Pathname` so you can just append `#%{method}`. | ||
RUBY | ||
|
||
expect_correction(<<~RUBY) | ||
::Rails.root.join('db', 'schema.rb').#{method} | ||
RUBY | ||
end | ||
|
||
it "registers an offense when using `::#{receiver}.#{method}(::Rails.root.join(...), ...)` (if arity exists)" do | ||
expect_offense(<<~RUBY, receiver: receiver, method: method) | ||
::%{receiver}.%{method}(::Rails.root.join('db', 'schema.rb'), 20, 5) | ||
^^^{receiver}^^{method}^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ `::Rails.root` is a `Pathname` so you can just append `#%{method}`. | ||
RUBY | ||
|
||
expect_correction(<<~RUBY) | ||
::Rails.root.join('db', 'schema.rb').#{method}(20, 5) | ||
RUBY | ||
end | ||
end | ||
end | ||
|
||
# This is handled by `Rails/RootJoinChain` | ||
it 'does not register an offense when using `File.read(Rails.root.join(...).join(...))`' do | ||
expect_no_offenses(<<~RUBY) | ||
File.read(Rails.root.join('db').join('schema.rb')) | ||
RUBY | ||
end | ||
|
||
# This is handled by `Style/FileRead` | ||
it 'does not register an offense when using `File.open(Rails.root.join(...)).read`' do | ||
expect_no_offenses(<<~RUBY) | ||
File.open(Rails.root.join('db', 'schema.rb')).read | ||
RUBY | ||
end | ||
|
||
# This is handled by `Style/FileRead` | ||
it 'does not register an offense when using `File.open(Rails.root.join(...)).binread`' do | ||
expect_no_offenses(<<~RUBY) | ||
File.open(Rails.root.join('db', 'schema.rb')).binread | ||
RUBY | ||
end | ||
|
||
# This is handled by `Style/FileWrite` | ||
it 'does not register an offense when using `File.open(Rails.root.join(...)).write(content)`' do | ||
expect_no_offenses(<<~RUBY) | ||
File.open(Rails.root.join('db', 'schema.rb')).write(content) | ||
RUBY | ||
end | ||
|
||
# This is handled by `Style/FileWrite` | ||
it 'does not register an offense when using `File.open(Rails.root.join(...)).binwrite(content)`' do | ||
expect_no_offenses(<<~RUBY) | ||
File.open(Rails.root.join('db', 'schema.rb')).binwrite(content) | ||
RUBY | ||
end | ||
|
||
it 'registers an offense when using `File.open(Rails.root.join(...), ...)` inside an iterator' do | ||
expect_offense(<<~RUBY) | ||
files.map { |file| File.open(Rails.root.join('db', file), 'wb') } | ||
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ `Rails.root` is a `Pathname` so you can just append `#open`. | ||
RUBY | ||
|
||
expect_correction(<<~RUBY) | ||
files.map { |file| Rails.root.join('db', file).open('wb') } | ||
RUBY | ||
end | ||
end |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
File.methods - File.superclass.methods
also returns e.g.:absolute_path
which is not on those lists.Which makes me think does it make sense to fill in those constants with those calls?
On the other hand, if the Ruby version with which
rubocop
is run is different from the target Ruby version, there might be differences.However, does it matter much?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
... but
Pathname
does not have a#absolute_path
method. Not sure I can follow here.I generated this list using
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Understood.
So basically
(File.public_methods(false) & Pathname.instance_methods(false)).sort
.Does it make sense to set the constant to this statement?
What are the consequences? New methods being added on
File
and toPathname
in future Ruby versions change the behaviour of the cop?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Well, just because there is
File.foo
andPathname#foo
, it is not guaranteed thatFile.foo(p, ...)
andp.foo(...)
is the same, so I'd rather maintain a list of safe methods.Also, there may be cases where the Ruby that runs Rubocop is not the Ruby which runs production code (e.g. you develop a gem on Ruby 2, but someone uses it on JRuby 9). Are those standard libraries necessarily equivalent?