-
-
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
Add new Lint/BooleanSymbol
cop
#4515
Conversation
bec72e4
to
0b25fab
Compare
# true | ||
# | ||
class BooleanSymbol < Cop | ||
MSG = 'Use `%<name>s` instead of `:%<name>s`.'.freeze |
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 think the cop description and the message should allude to the fact that this might be accidental, since there are actual cases where you'd want :true
or :false
, and just saying "use true
instead" is actually wrong. WDYT? 🙂
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.
Yes, you're right. I made message similar to Lint/AssignmentInCondition
.
Interesting case! 🙂 I think this can be enabled by default, but could actually be turned off in RuboCop's own |
0b25fab
to
551f1dc
Compare
@Drenmi That's right, I disabled it for |
# @example | ||
# | ||
# # bad | ||
# |
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 empty line seems redundant to me.
# @example | ||
# | ||
# # good | ||
# |
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.
Same here.
# true | ||
# | ||
class BooleanSymbol < Cop | ||
MSG = 'Boolean symbol - you probably meant to use `%s`.'.freeze |
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.
Symbol with a boolean value name (or something like this). "Boolean symbol" sounds a bit strange to me.
MSG = 'Boolean symbol - you probably meant to use `%s`.'.freeze | ||
|
||
def on_sym(node) | ||
match = node.source.match(/\A:(true|false)\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.
That seems like an overkill - you can just check if the symbol is :true
or :false
.
module RuboCop | ||
module Cop | ||
module Lint | ||
# This cop checks for boolean symbols. |
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'd expand this text a bit.
92e6f51
to
b62c96b
Compare
@bbatsov, fixed. |
My first thought when reading through the description was cool, should this also look for Is this going to be prone to a high volume of false positive? |
# @example | ||
# | ||
# # bad | ||
# :true |
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.
While we are at it, we might as well add in the bad and good example for false. 2 extra lines of documentation that would then cover 100% of the cases.
def on_sym(node) | ||
name = node.source[1..-1] | ||
|
||
return unless %w[true false].include?(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.
Any thoughts on using a node matcher?
def_node_matcher :boolean_symbol?, '(sym {:true :false})'
def on_sym(node)
return unless boolean_symbol?(node)
add_offense(node, :expression, format(MSG, node.source[1..-1]))
end
RUBY | ||
end | ||
|
||
it 'do not registers an offense when using regular symbol' 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.
Would it provide value to add a test for the boolean versions of true and false?
b62c96b
to
377682d
Compare
@rrosenblum Thanks, I've made changes according to your comments.
For example I checked > ack -hc "true"
6398
> ack -hc "'true'"
39
> ack -hc " :true"
4 And > ack -hc "true"
2573
> ack -hc "'true'"
11
> ack -hc " :true"
3 I think it should be treated like |
377682d
to
e2c64a0
Compare
@bbatsov, thanks! |
A few days ago I had this bug when someone accidentally used
:true
instead oftrue
. This leads to some problems since both:true
and:false
are "truthy" values and it's hard to notice this because they are quite normal symbols.I think in most cases you want to use just standard booleans so I suggest to mark it as an issue.
Before submitting the PR make sure the following are checked:
[Fix #issue-number]
(if the related issue exists).master
(if not - rebase it).rake spec
) are passing.rake internal_investigation
.and description in grammatically correct, complete sentences.
rake generate_cops_documentation
(required only when you've added a new cop or changed the configuration/documentation of an existing cop).