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: no-extra-boolean-cast invalid autofix for Boolean() without args #12076
Fix: no-extra-boolean-cast invalid autofix for Boolean() without args #12076
Conversation
This change makes sense to me. I know that this is the current behavior, but the fixer changing if (Boolean()) {
foo();
}
while (Boolean()) {
foo();
} to true;
true; feels like the autofixer overreaching to me. Now that we're fixing var foo = Boolean() ? bar() : baz(); to var foo = false ? bar() : baz(); , I wonder if we should be fixing the first example above to if (false) {
foo();
}
while (false) {
foo();
} as well? Additionally, seems like it should fix the following: if (!Boolean()) {
foo();
}
while (!Boolean()) {
foo();
} to if (true) {
foo();
}
while (true) {
foo();
} I'm curious what the rest of the team has to say about this - there might be some historical context around this that I'm not aware of. |
I think this is a bug from #7977. It was assumed that this rule warns on While these two don't change behavior (just delete a lot of unreachable code, which I guess is also not okay), the first example does: var foo = Boolean() ? bar() : baz(); to var foo = true; This is certainly a bug,
It's working exactly like that with this fix: {
code: "if (Boolean()) {}",
output: "if (false) {}",
errors: [{
messageId: "unexpectedCall",
type: "CallExpression"
}]
},
It was already working like this, and this shouldn't be changed by this fix. But to be sure, I'll add these test cases. |
Added 3 test cases and modified 1 to match the example, and everything works as you described :) |
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.
LGTM. Thanks for contributing!
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.
Curious to know what happens in this case: void ! /* Comment */ Boolean()
. I'm thinking sourceCode.getTokenBefore
should find the correct token since you didn't specify { comments: true }
, but I think we should make sure the comment is preserved. That might mean no autofix in that case (we have other rules that choose not to fix if it is difficult to preserve comments).
Otherwise, changes look good to me. Thanks for contributing!
Can't verify in the PR version at the moment, but I'm pretty sure that this comment will be removed, as it is within the node which is supposed to be replaced. It was already working like that (Demo link). I'll fix that in this PR, autofix shouldn't delete user's comments. I've already noticed that many rules are deleting comments (which should never happen I guess) and was thinking about an additional method in the |
That sounds like a great idea! |
Comments should be safe now in these and other autofix cases in this rule, please check is it ok. |
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.
LGTM. Thanks for adding all the extra tests!
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 call on handling comments
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.
LGTM, thanks for the good tests!!
What is the purpose of this pull request? (put an "X" next to item)
[X] Bug fix
Tell us about your environment
What parser (default, Babel-ESLint, etc.) are you using?
default
Please show your full configuration:
Configuration
What did you do? Please include the actual source code causing the issue.
Demo link
What did you expect to happen?
Not to change behavior and/or remove the code.
What actually happened? Please include the actual, raw output from ESLint.
What changes did you make? (Give an overview)
Fixed bugs:
!Boolean()
and the fix was replacingparent
withtrue
.Is there anything you'd like reviewers to focus on?