-
Notifications
You must be signed in to change notification settings - Fork 13k
Disallow truthiness/nullishness checks on syntax that never varies on it #59217
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
Merged
RyanCavanaugh
merged 13 commits into
microsoft:main
from
RyanCavanaugh:noSuspectTruthyChecks
Jul 22, 2024
Merged
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
26f7cd3
Check ?? and truthiness checks for suspicious expression syntax
RyanCavanaugh 263a967
Lint/format
RyanCavanaugh 02353fc
Fix-ups
RyanCavanaugh f4a4251
Format
RyanCavanaugh ca1034f
Update error messages
RyanCavanaugh 701b54a
Merge branch 'main' of https://github.com/microsoft/TypeScript into n…
RyanCavanaugh 3388e4c
Format
RyanCavanaugh 6d0da7a
Add @internal
RyanCavanaugh 64d27ce
Handle not-undefined undefineds
RyanCavanaugh f988a05
Resolve identifiers to check for undefined
RyanCavanaugh b786cf2
Baseline update
RyanCavanaugh 5f59624
Add tests; just use skipOuterExpressions
RyanCavanaugh ba53157
🧹
RyanCavanaugh 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 hidden or 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 hidden or 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 hidden or 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 |
---|---|---|
|
@@ -923,6 +923,14 @@ export const enum RelationComparisonResult { | |
Overflow = ComplexityOverflow | StackDepthOverflow, | ||
} | ||
|
||
/** @internal */ | ||
export const enum PredicateSemantics { | ||
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 think |
||
None = 0, | ||
Always = 1 << 0, | ||
Never = 1 << 1, | ||
Sometimes = Always | Never, | ||
} | ||
|
||
/** @internal */ | ||
export type NodeId = number; | ||
|
||
|
This file contains hidden or 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
31 changes: 31 additions & 0 deletions
31
tests/baselines/reference/aliasUsageInOrExpression.errors.txt
This file contains hidden or 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,31 @@ | ||
aliasUsageInOrExpression_main.ts(10,40): error TS2873: This kind of expression is always falsy. | ||
aliasUsageInOrExpression_main.ts(11,40): error TS2873: This kind of expression is always falsy. | ||
|
||
|
||
==== aliasUsageInOrExpression_main.ts (2 errors) ==== | ||
import Backbone = require("./aliasUsageInOrExpression_backbone"); | ||
import moduleA = require("./aliasUsageInOrExpression_moduleA"); | ||
interface IHasVisualizationModel { | ||
VisualizationModel: typeof Backbone.Model; | ||
} | ||
var i: IHasVisualizationModel; | ||
var d1 = i || moduleA; | ||
var d2: IHasVisualizationModel = i || moduleA; | ||
var d2: IHasVisualizationModel = moduleA || i; | ||
var e: { x: IHasVisualizationModel } = <{ x: IHasVisualizationModel }>null || { x: moduleA }; | ||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||
!!! error TS2873: This kind of expression is always falsy. | ||
var f: { x: IHasVisualizationModel } = <{ x: IHasVisualizationModel }>null ? { x: moduleA } : null; | ||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||
!!! error TS2873: This kind of expression is always falsy. | ||
==== aliasUsageInOrExpression_backbone.ts (0 errors) ==== | ||
export class Model { | ||
public someData: string; | ||
} | ||
|
||
==== aliasUsageInOrExpression_moduleA.ts (0 errors) ==== | ||
import Backbone = require("./aliasUsageInOrExpression_backbone"); | ||
export class VisualizationModel extends Backbone.Model { | ||
// interesting stuff here | ||
} | ||
|
26 changes: 26 additions & 0 deletions
26
tests/baselines/reference/bestCommonTypeWithContextualTyping.errors.txt
This file contains hidden or 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,26 @@ | ||
bestCommonTypeWithContextualTyping.ts(19,31): error TS2873: This kind of expression is always falsy. | ||
|
||
|
||
==== bestCommonTypeWithContextualTyping.ts (1 errors) ==== | ||
interface Contextual { | ||
dummy; | ||
p?: number; | ||
} | ||
|
||
interface Ellement { | ||
dummy; | ||
p: any; | ||
} | ||
|
||
var e: Ellement; | ||
|
||
// All of these should pass. Neither type is a supertype of the other, but the RHS should | ||
// always use Ellement in these examples (not Contextual). Because Ellement is assignable | ||
// to Contextual, no errors. | ||
var arr: Contextual[] = [e]; // Ellement[] | ||
var obj: { [s: string]: Contextual } = { s: e }; // { s: Ellement; [s: string]: Ellement } | ||
|
||
var conditional: Contextual = null ? e : e; // Ellement | ||
~~~~ | ||
!!! error TS2873: This kind of expression is always falsy. | ||
var contextualOr: Contextual = e || e; // Ellement |
This file contains hidden or 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 hidden or 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 TS2872: This kind of expression is always truthy. | ||
|
||
|
||
==== 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 TS2872: This kind of expression is always truthy. | ||
} | ||
|
This file contains hidden or 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 hidden or 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 TS2873: This kind of expression is always falsy. | ||
|
||
|
||
==== computedPropertyNames46_ES5.ts (1 errors) ==== | ||
var o = { | ||
["" || 0]: 0 | ||
~~ | ||
!!! error TS2873: This kind of expression is always falsy. | ||
}; |
9 changes: 9 additions & 0 deletions
9
tests/baselines/reference/computedPropertyNames46_ES6.errors.txt
This file contains hidden or 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 TS2873: This kind of expression is always falsy. | ||
|
||
|
||
==== computedPropertyNames46_ES6.ts (1 errors) ==== | ||
var o = { | ||
["" || 0]: 0 | ||
~~ | ||
!!! error TS2873: This kind of expression is always falsy. | ||
}; |
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.
Uh oh!
There was an error while loading. Please reload this page.