Skip to content

[Fix #4008] Add new Style/ExpandPathArguments cop#5552

Merged
bbatsov merged 1 commit into
rubocop:masterfrom
koic:add_new_style_expand_path_arguments_cop
Feb 10, 2018
Merged

[Fix #4008] Add new Style/ExpandPathArguments cop#5552
bbatsov merged 1 commit into
rubocop:masterfrom
koic:add_new_style_expand_path_arguments_cop

Conversation

@koic
Copy link
Copy Markdown
Member

@koic koic commented Feb 7, 2018

Fixes #4008.

Summary

This cop checks for use of the File.expand_path arguments.

# bad
File.expand_path('..', __FILE__)

# good
File.expand_path(__dir__)

Likewise, it also checks for the Pathname.new argument.

# 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.

# 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.

File.expand_path(path, __FILE__)
File.expand_path("#{path_to_file}.png", __FILE__)

Before submitting the PR make sure the following are checked:

  • Wrote good commit messages.
  • Commit message starts with [Fix #issue-number] (if the related issue exists).
  • Feature branch is up-to-date with master (if not - rebase it).
  • Squashed related commits together.
  • Added tests.
  • Added an entry to the Changelog if the new code introduces user-observable changes. See changelog entry format.
  • The PR relates to only one subject with a clear title
    and description in grammatically correct, complete sentences.
  • Run rake default or rake parallel. It executes all tests and RuboCop for itself, and generates the documentation.

@koic koic force-pushed the add_new_style_expand_path_arguments_cop branch 2 times, most recently from 41b657c to 923e853 Compare February 7, 2018 15:35
@koic koic changed the title Add new Style/ExpandPathArguments cop [Fix #4008] Add new Style/ExpandPathArguments cop Feb 7, 2018
@koic koic force-pushed the add_new_style_expand_path_arguments_cop branch from 923e853 to c2a4562 Compare February 7, 2018 16:11
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__)
```
@koic koic force-pushed the add_new_style_expand_path_arguments_cop branch from c2a4562 to 836b087 Compare February 7, 2018 22:35
@koic koic closed this Feb 10, 2018
@koic koic deleted the add_new_style_expand_path_arguments_cop branch February 10, 2018 15:07
@koic koic restored the add_new_style_expand_path_arguments_cop branch February 10, 2018 15:08
@koic koic reopened this Feb 10, 2018
@bbatsov bbatsov merged commit b232303 into rubocop:master Feb 10, 2018
@bbatsov
Copy link
Copy Markdown
Collaborator

bbatsov commented Feb 10, 2018

Nice work! 👍

@koic koic deleted the add_new_style_expand_path_arguments_cop branch February 10, 2018 18:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants