[Fix #4008] Add new Style/ExpandPathArguments cop#5552
Merged
bbatsov merged 1 commit intoFeb 10, 2018
Conversation
41b657c to
923e853
Compare
Style/ExpandPathArguments copStyle/ExpandPathArguments cop
923e853 to
c2a4562
Compare
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__) ```
c2a4562 to
836b087
Compare
Collaborator
|
Nice work! 👍 |
This file contains hidden or 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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Fixes #4008.
Summary
This cop checks for use of the
File.expand_patharguments.Likewise, it also checks for the
Pathname.newargument.Basically, replace
__FILE__with__dir_, but also warn about arguments that specify unnecessary current directory as follows.This cop has autocorrect for these.
This commit autocorrected some offenses that had occurred in RuboCop itself using this
Style/ExpandPathArgumentscop.Other Information
If other than
str_typeis used as an argument toexpand_path, it will not be checked because the path cannot be determined.For example, in the following cases.
Before submitting the PR make sure the following are checked:
[Fix #issue-number](if the related issue exists).master(if not - rebase it).and description in grammatically correct, complete sentences.
rake defaultorrake parallel. It executes all tests and RuboCop for itself, and generates the documentation.