-
-
Notifications
You must be signed in to change notification settings - Fork 3.1k
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
Recommend File.expand_path(__dir__)
over File.expand_path('..', __FILE__)
#4008
Comments
I'm fine with the idea. I think I planned to implement this myself years ago, but I never got to doing so. |
@backus |
@dabroz good point I'll adjust that |
Fixes rubocop#4008. ## Summary This cop checks for use of the `File.expand_path` arguments. ```ruby # bad File.expand_path('..', __FILE__) # good File.expand_path(__dir__) ``` Likewise, it also checks for the `Pathname.new` argument. ```ruby # bad Pathname.new(__FILE__).parent.expand_path # good Pathname.new(__dir__).expand_path ``` Basically, replace `__FILE__` with `__dir_`, but also warn about arguments that specify unnecessary current directory as follows. ```ruby # bad File.expand_path('.', __FILE__) # good File.expand_path(__FILE__) ``` This cop has autocorrect for these. This commit autocorrected some offenses that had occurred in RuboCop itself using this `Style/ExpandPathArguments` cop. ## Other Information If other than `str_type` is used as an argument to `expand_path`, it will not be checked because the path cannot be determined. For example, in the following cases. ```ruby File.expand_path(path, __FILE__) File.expand_path("#{path_to_file}.png", __FILE__) ```
Fixes #4008. ## Summary This cop checks for use of the `File.expand_path` arguments. ```ruby # bad File.expand_path('..', __FILE__) # good File.expand_path(__dir__) ``` Likewise, it also checks for the `Pathname.new` argument. ```ruby # bad Pathname.new(__FILE__).parent.expand_path # good Pathname.new(__dir__).expand_path ``` Basically, replace `__FILE__` with `__dir_`, but also warn about arguments that specify unnecessary current directory as follows. ```ruby # bad File.expand_path('.', __FILE__) # good File.expand_path(__FILE__) ``` This cop has autocorrect for these. This commit autocorrected some offenses that had occurred in RuboCop itself using this `Style/ExpandPathArguments` cop. ## Other Information If other than `str_type` is used as an argument to `expand_path`, it will not be checked because the path cannot be determined. For example, in the following cases. ```ruby File.expand_path(path, __FILE__) File.expand_path("#{path_to_file}.png", __FILE__) ```
The message is This is with Ruby 2.3.1 and RuboCop 0.52.1. |
Since Ruby 2.0 we've had
__dir__
as well as__FILE__
. Still, I think a very standard practice is writeeven though now you can do
Other corrections we could make like this:
The text was updated successfully, but these errors were encountered: