-
-
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
[Fix #3666] Adds Uncommunicative MethodArg & BlockParam Naming cops #5211
Conversation
931ab1a
to
918bdfe
Compare
Out of curiosity - how many offences does the RuboCop codebase produce? |
And 2 understandable cli_spec failures. However, doing this has highlighted a bug in this PR. I have to account for default args (I guess only operate on the lhs when the arg assigns). So this is WIP until I fix it. E.g.
The vast majority of those 101 offenses are legitimate based on standards these new cops present. Only a small handful are default argument issues. Would you like me to fix the offenses and default these to enabled? |
That would be my preference, yes. |
|
||
def min_length | ||
cop_config['MinParamNameLength'] || | ||
cop_config['MinArgNameLength'] |
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.
This method is confusing. We will be able to set MinParamNameLength
to Naming/UncommunicativeMethodArgName:
. I think it is a bug. Naming/UncommunicativeMethodArgName
should only use MinArgNameLength
, do not use MinParamNameLength
.
For example:
# in the mixin
def min_length
cop_config[MIN_LENGTH_KEY]
end
# in the cops
class UncommunicativeMethodArgName
MIN_LENGTH_KEY = 'MinArgNameLength'
end
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.
Great point! Thanks!
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 wasn't able to do exactly this. Since the mixin is a Module
, it didn't have access to constants from the Class it extends. But this issue is resolved.
end | ||
|
||
def uppercase?(name) | ||
name =~ /[A-Z]/ |
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.
Probably it should be [[:upper:]]
.
See #5017
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.
Excellent! Thank you!
def on_def(node) | ||
return unless node.arguments? | ||
check(node, node.arguments) | ||
end |
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.
This cop should check defs
node also (For def self.foo; end
).
config/default.yml
Outdated
@@ -685,6 +685,15 @@ Naming/PredicateName: | |||
Exclude: | |||
- 'spec/**/*' | |||
|
|||
Naming/UncommunicativeBlockParamName: | |||
# Parameter names may be equal to or greater than this value | |||
MinParamNameLength: 1 |
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.
Btw, with such a default will this ever flag anything? :-)
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.
It will only flag BlockParams that end in numbers or contain capital letters. It will never flag length. But, should users want to enforce a 2 or 3 char min length, they have the ability to.
If we raise it, I think we'd have to add more config to whitelist individual letter exceptions. As we discussed some on the issue, single letter block params are more common. Perhaps we can hash it out in a new issue after this becomes deliverable?
70dedf0
to
4c249c3
Compare
@@ -70,7 +70,7 @@ def no_surrounding_space?(arg, equals) | |||
!arg.space_after? && !equals.space_after? | |||
end | |||
|
|||
def message(_) | |||
def message(_msg) |
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.
The argument to #message
is not a message. It is the offending node. 🙂
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.
Thanks! I'll change it here and everywhere else this PR corrects a #message
.
@@ -10,7 +10,7 @@ def checkable_layout?(_node) | |||
true | |||
end | |||
|
|||
def deltas_for_first_pair(*) | |||
def deltas_for_first_pair(*_args) |
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.
This isn't any more communicative than just *
. 🙂
I think some of the fixes made here highlight the limitations of the method. The sets "short" and "uncommunicative" are not perfectly overlapping. For example, Not sure how to approach this though. Perhaps a whitelist might be a good start? For blocks, I know there have been many discussions in the Reek repo, because people tend to use single-letter arguments for single-line blocks. |
I agree that these Cops have there limitations, particularly when it comes to length. To clarify:
I'm going to give this another full pass and see if I can improve some of these names more. About half of our offenders are either That leaves adding a whitelist. I will go ahead and add this configurable option in my next pass. Based on the corrections you see in this PR, what exactly would you like to see on Opinions & suggestions are very welcome here. |
I guess this would become apparent just by analysing the offences produced by the cop with a whitelist present. :-) Sorry for dropping the ball on the cop for a bit, but my focus was getting 0.52.1 out. I'm going to give this another full pass and see if I can improve some of these names more. About half of our offenders are either t1/t2 or n1/n2. These were difficult to rename because they are often named via a each_cons(2) and thus truly seem like a first and second. I've renamed some of these token_one and token_two, and this seems horrible. I'm contemplating naming all these left_token/right_token and left_node/right_node. WDYT?
|
4c249c3
to
3f11431
Compare
nil_or_empty?(node) do |variable1, variable2| | ||
return unless variable1 == variable2 | ||
nil_or_empty?(node) do |var1, var2| | ||
return unless var1 == var2 |
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 really didn't want to do this. But. For the Rails blank?
and present?
cops, "variable" or "var" are the best descriptions of what these values contain. (I don't find variable anymore descriptive than var.)
.rubocop.yml
Outdated
- 'token1' | ||
- 'token2' | ||
- 'var1' | ||
- 'var2' |
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 find this list informative. Seems like these cops could also use a config option to turn off NamesEndingInNumbers
offenses.
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 was just about to suggest this. :-)
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'll add this and disable it by default as well.
3f11431
to
76aad6c
Compare
dc533c5
to
cf21688
Compare
👍 |
Adds new
Naming/UncommunicativeMethodArgName
&Naming/UncommunicativeBlockParamName
cops. This enhancement was discussed in detail on issue #3666. One detail left off the discussion was that there is now aNaming
namespace, and it makes sense to add these there.These cops are disabled by default and should be easily extendable. I'd be happy to add more configuration, but it may be best to flush out what exactly comes next in an issue.
[Fix #issue-number]
(if the related issue exists).master
(if not - rebase it).and description in grammatically correct, complete sentences.
rake default
orrake parallel
. It executes all tests and RuboCop for itself, and generates the documentation.