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: Prevent false positives with no-constant-condition #15486
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -175,7 +175,17 @@ ruleTester.run("no-constant-condition", rule, { | |
"function* foo() {while (true) {function* foo() {yield;}yield;}}", | ||
"function* foo() { for (let x = yield; x < 10; x++) {yield;}yield;}", | ||
"function* foo() { for (let x = yield; ; x++) { yield; }}", | ||
"if (new Number(x) + 1 === 2) {}" | ||
"if (new Number(x) + 1 === 2) {}", | ||
|
||
// #15467 | ||
"if([a]==[b]) {}", | ||
"if (+[...a]) {}", | ||
"if (+[...[...a]]) {}", | ||
"if (`${[...a]}`) {}", | ||
"if (`${[a]}`) {}", | ||
"if (+[a]) {}", | ||
"if (0 - [a]) {}", | ||
"if (1 * [a]) {}" | ||
], | ||
invalid: [ | ||
{ code: "for(;true;);", errors: [{ messageId: "unexpected", type: "Literal" }] }, | ||
|
@@ -262,8 +272,6 @@ ruleTester.run("no-constant-condition", rule, { | |
|
||
// #5228 , typeof conditions | ||
{ code: "if(typeof x){}", errors: [{ messageId: "unexpected", type: "UnaryExpression" }] }, | ||
{ code: "if(`${typeof x}`){}", errors: [{ messageId: "unexpected", type: "TemplateLiteral" }] }, | ||
{ code: "if(`${''}${typeof x}`){}", errors: [{ messageId: "unexpected", type: "TemplateLiteral" }] }, | ||
{ code: "if(typeof 'abc' === 'string'){}", errors: [{ messageId: "unexpected", type: "BinaryExpression" }] }, | ||
{ code: "if(a = typeof b){}", errors: [{ messageId: "unexpected", type: "AssignmentExpression" }] }, | ||
{ code: "if(a, typeof b){}", errors: [{ messageId: "unexpected", type: "SequenceExpression" }] }, | ||
|
@@ -358,18 +366,6 @@ ruleTester.run("no-constant-condition", rule, { | |
code: "if(''+[]) {}", | ||
errors: [{ messageId: "unexpected", type: "BinaryExpression" }] | ||
}, | ||
{ | ||
code: "if([a]==[a]) {}", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is a true error that we won't catch any more, but it is equivalent to a false positive ( There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is [1]==[1] // false There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ah, yes. Sorry, not sure what I was thinking. That said, [1] == 1
true
[0] == 1
false So, you're absolutely right that There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Will
I'm asking because these look like realistic mistakes that will be no longer reported by There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. These would get caught by
These would not:
However, I suspect (hope!) that most people would be using some form of the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
I've just left a question about this on the |
||
errors: [{ messageId: "unexpected", type: "BinaryExpression" }] | ||
}, | ||
{ | ||
code: "if([a] - '') {}", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This was a false positive. If |
||
errors: [{ messageId: "unexpected", type: "BinaryExpression" }] | ||
}, | ||
{ | ||
code: "if(+[a]) {}", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This was a false positive. If |
||
errors: [{ messageId: "unexpected", type: "UnaryExpression" }] | ||
}, | ||
{ | ||
code: "if(+1) {}", | ||
errors: [{ messageId: "unexpected", type: "UnaryExpression" }] | ||
|
@@ -397,7 +393,9 @@ ruleTester.run("no-constant-condition", rule, { | |
// Boxed primitives are always truthy | ||
{ code: "if(new Boolean(foo)) {}", errors: [{ messageId: "unexpected" }] }, | ||
{ code: "if(new String(foo)) {}", errors: [{ messageId: "unexpected" }] }, | ||
{ code: "if(new Number(foo)) {}", errors: [{ messageId: "unexpected" }] } | ||
{ code: "if(new Number(foo)) {}", errors: [{ messageId: "unexpected" }] }, | ||
|
||
// Spreading a constant array | ||
{ code: "if(`${[...['a']]}`) {}", errors: [{ messageId: "unexpected" }] } | ||
] | ||
}); |
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.
These are true errors that we won't catch any more.