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 Style/FileDirnameLevel
cop
#10419
Add new Style/FileDirnameLevel
cop
#10419
Conversation
module RuboCop | ||
module Cop | ||
module Style | ||
# This cop checks for repeated `File.dirname`. |
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.
repeated -> nested
# # good | ||
# File.dirname(path, 2) | ||
# | ||
class FileDirnameLevel < Base |
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.
I wonder if NestedFileDirname
is not a better name, but I'm fine with the proposed name.
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.
Yeah, I was wondering whether to use that name. So, I've updated to NestedFileDirname
to be consistent with the doc.
|
||
RSpec.describe RuboCop::Cop::Style::FileDirnameLevel, :config do | ||
context 'Ruby >= 3.1', :ruby31 do | ||
it 'registers and corrects an offense when using `File.dirname(path)` repeated two times' do |
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.
nested two times
And so on for the other example descriptions.
## Summary This cop checks for nested `File.dirname`. It replaces nested `File.dirname` with the level argument introduced in Ruby 3.1. ```ruby # bad File.dirname(File.dirname(path)) # good File.dirname(path, 2) ``` See: https://bugs.ruby-lang.org/issues/12194 ## Other Information For `File.expand_path('../..', path)`, there is a partial conflict with `Style/ExpandPathArguments` cop. So this PR does not support it. https://docs.rubocop.org/rubocop/1.25/cops_style.html#styleexpandpatharguments
193ce3d
to
a975c1b
Compare
Thanks! |
Summary
This cop checks for nested
File.dirname
. It replaces repeats with the level argument introduced in Ruby 3.1.See: https://bugs.ruby-lang.org/issues/12194
Other Information
For
File.expand_path('../..', path)
, there is a partial conflict withStyle/ExpandPathArguments
cop. So this PR does not support it.https://docs.rubocop.org/rubocop/1.25/cops_style.html#styleexpandpatharguments
Before submitting the PR make sure the following are checked:
[Fix #issue-number]
(if the related issue exists).master
(if not - rebase it).bundle exec rake default
. It executes all tests and runs RuboCop on its own code.{change_type}_{change_description}.md
if the new code introduces user-observable changes. See changelog entry format for details.