-
Notifications
You must be signed in to change notification settings - Fork 12.3k
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
Disallow truthiness checks on certain literal kinds #59003
Closed
Closed
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
returns.js(20,12): error TS2869: Truthiness test of this kind of expression appears unintentional | ||
|
||
|
||
==== returns.js (1 errors) ==== | ||
// @ts-check | ||
/** | ||
* @returns {string} This comment is not currently exposed | ||
*/ | ||
function f() { | ||
return "hello"; | ||
} | ||
|
||
/** | ||
* @returns {string=} This comment is not currently exposed | ||
*/ | ||
function f1() { | ||
return "hello world"; | ||
} | ||
|
||
/** | ||
* @returns {string|number} This comment is not currently exposed | ||
*/ | ||
function f2() { | ||
return 5 || "hello"; | ||
~ | ||
!!! error TS2869: Truthiness test of this kind of expression appears unintentional | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
9 changes: 9 additions & 0 deletions
9
tests/baselines/reference/computedPropertyNames46_ES5.errors.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
computedPropertyNames46_ES5.ts(2,6): error TS2869: Truthiness test of this kind of expression appears unintentional | ||
|
||
|
||
==== computedPropertyNames46_ES5.ts (1 errors) ==== | ||
var o = { | ||
["" || 0]: 0 | ||
~~ | ||
!!! error TS2869: Truthiness test of this kind of expression appears unintentional | ||
}; |
9 changes: 9 additions & 0 deletions
9
tests/baselines/reference/computedPropertyNames46_ES6.errors.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
computedPropertyNames46_ES6.ts(2,6): error TS2869: Truthiness test of this kind of expression appears unintentional | ||
|
||
|
||
==== computedPropertyNames46_ES6.ts (1 errors) ==== | ||
var o = { | ||
["" || 0]: 0 | ||
~~ | ||
!!! error TS2869: Truthiness test of this kind of expression appears unintentional | ||
}; |
23 changes: 23 additions & 0 deletions
23
tests/baselines/reference/computedPropertyNames48_ES5.errors.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
computedPropertyNames48_ES5.ts(16,6): error TS2869: Truthiness test of this kind of expression appears unintentional | ||
|
||
|
||
==== computedPropertyNames48_ES5.ts (1 errors) ==== | ||
declare function extractIndexer<T>(p: { [n: number]: T }): T; | ||
|
||
enum E { x } | ||
|
||
var a: any; | ||
|
||
extractIndexer({ | ||
[a]: "" | ||
}); // Should return string | ||
|
||
extractIndexer({ | ||
[E.x]: "" | ||
}); // Should return string | ||
|
||
extractIndexer({ | ||
["" || 0]: "" | ||
~~ | ||
!!! error TS2869: Truthiness test of this kind of expression appears unintentional | ||
}); // Should return any (widened form of undefined) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
23 changes: 23 additions & 0 deletions
23
tests/baselines/reference/computedPropertyNames48_ES6.errors.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
computedPropertyNames48_ES6.ts(16,6): error TS2869: Truthiness test of this kind of expression appears unintentional | ||
|
||
|
||
==== computedPropertyNames48_ES6.ts (1 errors) ==== | ||
declare function extractIndexer<T>(p: { [n: number]: T }): T; | ||
|
||
enum E { x } | ||
|
||
var a: any; | ||
|
||
extractIndexer({ | ||
[a]: "" | ||
}); // Should return string | ||
|
||
extractIndexer({ | ||
[E.x]: "" | ||
}); // Should return string | ||
|
||
extractIndexer({ | ||
["" || 0]: "" | ||
~~ | ||
!!! error TS2869: Truthiness test of this kind of expression appears unintentional | ||
}); // Should return any (widened form of undefined) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
84 changes: 84 additions & 0 deletions
84
tests/baselines/reference/conditionalOperatorConditionIsNumberType.errors.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
conditionalOperatorConditionIsNumberType.ts(27,1): error TS2869: Truthiness test of this kind of expression appears unintentional | ||
conditionalOperatorConditionIsNumberType.ts(29,1): error TS2869: Truthiness test of this kind of expression appears unintentional | ||
conditionalOperatorConditionIsNumberType.ts(30,1): error TS2869: Truthiness test of this kind of expression appears unintentional | ||
conditionalOperatorConditionIsNumberType.ts(53,23): error TS2869: Truthiness test of this kind of expression appears unintentional | ||
conditionalOperatorConditionIsNumberType.ts(55,23): error TS2869: Truthiness test of this kind of expression appears unintentional | ||
conditionalOperatorConditionIsNumberType.ts(56,32): error TS2869: Truthiness test of this kind of expression appears unintentional | ||
|
||
|
||
==== conditionalOperatorConditionIsNumberType.ts (6 errors) ==== | ||
//Cond ? Expr1 : Expr2, Cond is of number type, Expr1 and Expr2 have the same type | ||
var condNumber: number; | ||
|
||
var exprAny1: any; | ||
var exprBoolean1: boolean; | ||
var exprNumber1: number; | ||
var exprString1: string; | ||
var exprIsObject1: Object; | ||
|
||
var exprAny2: any; | ||
var exprBoolean2: boolean; | ||
var exprNumber2: number; | ||
var exprString2: string; | ||
var exprIsObject2: Object; | ||
|
||
//Cond is a number type variable | ||
condNumber ? exprAny1 : exprAny2; | ||
condNumber ? exprBoolean1 : exprBoolean2; | ||
condNumber ? exprNumber1 : exprNumber2; | ||
condNumber ? exprString1 : exprString2; | ||
condNumber ? exprIsObject1 : exprIsObject2; | ||
condNumber ? exprString1 : exprBoolean1; // Union | ||
|
||
//Cond is a number type literal | ||
1 ? exprAny1 : exprAny2; | ||
0 ? exprBoolean1 : exprBoolean2; | ||
0.123456789 ? exprNumber1 : exprNumber2; | ||
~~~~~~~~~~~ | ||
!!! error TS2869: Truthiness test of this kind of expression appears unintentional | ||
- 10000000000000 ? exprString1 : exprString2; | ||
1000000000000 ? exprIsObject1 : exprIsObject2; | ||
~~~~~~~~~~~~~ | ||
!!! error TS2869: Truthiness test of this kind of expression appears unintentional | ||
10000 ? exprString1 : exprBoolean1; // Union | ||
~~~~~ | ||
!!! error TS2869: Truthiness test of this kind of expression appears unintentional | ||
|
||
//Cond is a number type expression | ||
function foo() { return 1 }; | ||
var array = [1, 2, 3]; | ||
|
||
1 * 0 ? exprAny1 : exprAny2; | ||
1 + 1 ? exprBoolean1 : exprBoolean2; | ||
"string".length ? exprNumber1 : exprNumber2; | ||
foo() ? exprString1 : exprString2; | ||
foo() / array[1] ? exprIsObject1 : exprIsObject2; | ||
foo() ? exprString1 : exprBoolean1; // Union | ||
|
||
//Results shoud be same as Expr1 and Expr2 | ||
var resultIsAny1 = condNumber ? exprAny1 : exprAny2; | ||
var resultIsBoolean1 = condNumber ? exprBoolean1 : exprBoolean2; | ||
var resultIsNumber1 = condNumber ? exprNumber1 : exprNumber2; | ||
var resultIsString1 = condNumber ? exprString1 : exprString2; | ||
var resultIsObject1 = condNumber ? exprIsObject1 : exprIsObject2; | ||
var resultIsStringOrBoolean1 = condNumber ? exprString1 : exprBoolean1; // Union | ||
|
||
var resultIsAny2 = 1 ? exprAny1 : exprAny2; | ||
var resultIsBoolean2 = 0 ? exprBoolean1 : exprBoolean2; | ||
var resultIsNumber2 = 0.123456789 ? exprNumber1 : exprNumber2; | ||
~~~~~~~~~~~ | ||
!!! error TS2869: Truthiness test of this kind of expression appears unintentional | ||
var resultIsString2 = - 10000000000000 ? exprString1 : exprString2; | ||
var resultIsObject2 = 1000000000000 ? exprIsObject1 : exprIsObject2; | ||
~~~~~~~~~~~~~ | ||
!!! error TS2869: Truthiness test of this kind of expression appears unintentional | ||
var resultIsStringOrBoolean2 = 10000 ? exprString1 : exprBoolean1; // Union | ||
~~~~~ | ||
!!! error TS2869: Truthiness test of this kind of expression appears unintentional | ||
|
||
var resultIsAny3 = 1 * 0 ? exprAny1 : exprAny2; | ||
var resultIsBoolean3 = 1 + 1 ? exprBoolean1 : exprBoolean2; | ||
var resultIsNumber3 = "string".length ? exprNumber1 : exprNumber2; | ||
var resultIsString3 = foo() ? exprString1 : exprString2; | ||
var resultIsObject3 = foo() / array[1] ? exprIsObject1 : exprIsObject2; | ||
var resultIsStringOrBoolean3 = foo() / array[1] ? exprString1 : exprBoolean1; // Union |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Aware this is still a draft, but function expressions will almost certainly be a mistyped IIFE, so it might be helpful to separate out a specific error message for these – maybe some sort of "Did you mean to call..." message, à la 2774?