-
Notifications
You must be signed in to change notification settings - Fork 12.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Detect circularities when removing 'undefined' from defaulted params (#…
…37023) Fixes #37008 Note that referencing a variable in its initializer is a TDZ error; the OP report had OOB logic that prevented this in practice (?)
- Loading branch information
1 parent
f7d2beb
commit c4e9685
Showing
6 changed files
with
104 additions
and
6 deletions.
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
32 changes: 32 additions & 0 deletions
32
tests/baselines/reference/circularOptionalityRemoval.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,32 @@ | ||
tests/cases/compiler/circularOptionalityRemoval.ts(2,14): error TS2502: 'x' is referenced directly or indirectly in its own type annotation. | ||
tests/cases/compiler/circularOptionalityRemoval.ts(2,38): error TS2372: Parameter 'x' cannot be referenced in its initializer. | ||
tests/cases/compiler/circularOptionalityRemoval.ts(2,38): error TS2532: Object is possibly 'undefined'. | ||
tests/cases/compiler/circularOptionalityRemoval.ts(2,46): error TS2372: Parameter 'x' cannot be referenced in its initializer. | ||
tests/cases/compiler/circularOptionalityRemoval.ts(5,14): error TS1015: Parameter cannot have question mark and initializer. | ||
tests/cases/compiler/circularOptionalityRemoval.ts(5,14): error TS2502: 'x' is referenced directly or indirectly in its own type annotation. | ||
tests/cases/compiler/circularOptionalityRemoval.ts(5,27): error TS2304: Cannot find name 'someCondition'. | ||
tests/cases/compiler/circularOptionalityRemoval.ts(5,54): error TS2372: Parameter 'x' cannot be referenced in its initializer. | ||
|
||
|
||
==== tests/cases/compiler/circularOptionalityRemoval.ts (8 errors) ==== | ||
// Constructed repro | ||
function fn1(x: number | undefined = x > 0 ? x : 0) { } | ||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||
!!! error TS2502: 'x' is referenced directly or indirectly in its own type annotation. | ||
~ | ||
!!! error TS2372: Parameter 'x' cannot be referenced in its initializer. | ||
~ | ||
!!! error TS2532: Object is possibly 'undefined'. | ||
~ | ||
!!! error TS2372: Parameter 'x' cannot be referenced in its initializer. | ||
|
||
// Report from user | ||
function fn2(x?: string = someCondition ? 'value1' : x) { } | ||
~ | ||
!!! error TS1015: Parameter cannot have question mark and initializer. | ||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||
!!! error TS2502: 'x' is referenced directly or indirectly in its own type annotation. | ||
~~~~~~~~~~~~~ | ||
!!! error TS2304: Cannot find name 'someCondition'. | ||
~ | ||
!!! error TS2372: Parameter 'x' cannot be referenced in its initializer. |
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,16 @@ | ||
//// [circularOptionalityRemoval.ts] | ||
// Constructed repro | ||
function fn1(x: number | undefined = x > 0 ? x : 0) { } | ||
|
||
// Report from user | ||
function fn2(x?: string = someCondition ? 'value1' : x) { } | ||
|
||
//// [circularOptionalityRemoval.js] | ||
// Constructed repro | ||
function fn1(x) { | ||
if (x === void 0) { x = x > 0 ? x : 0; } | ||
} | ||
// Report from user | ||
function fn2(x) { | ||
if (x === void 0) { x = someCondition ? 'value1' : x; } | ||
} |
14 changes: 14 additions & 0 deletions
14
tests/baselines/reference/circularOptionalityRemoval.symbols
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,14 @@ | ||
=== tests/cases/compiler/circularOptionalityRemoval.ts === | ||
// Constructed repro | ||
function fn1(x: number | undefined = x > 0 ? x : 0) { } | ||
>fn1 : Symbol(fn1, Decl(circularOptionalityRemoval.ts, 0, 0)) | ||
>x : Symbol(x, Decl(circularOptionalityRemoval.ts, 1, 13)) | ||
>x : Symbol(x, Decl(circularOptionalityRemoval.ts, 1, 13)) | ||
>x : Symbol(x, Decl(circularOptionalityRemoval.ts, 1, 13)) | ||
|
||
// Report from user | ||
function fn2(x?: string = someCondition ? 'value1' : x) { } | ||
>fn2 : Symbol(fn2, Decl(circularOptionalityRemoval.ts, 1, 55)) | ||
>x : Symbol(x, Decl(circularOptionalityRemoval.ts, 4, 13)) | ||
>x : Symbol(x, Decl(circularOptionalityRemoval.ts, 4, 13)) | ||
|
21 changes: 21 additions & 0 deletions
21
tests/baselines/reference/circularOptionalityRemoval.types
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,21 @@ | ||
=== tests/cases/compiler/circularOptionalityRemoval.ts === | ||
// Constructed repro | ||
function fn1(x: number | undefined = x > 0 ? x : 0) { } | ||
>fn1 : (x?: number | undefined) => void | ||
>x : number | undefined | ||
>x > 0 ? x : 0 : number | undefined | ||
>x > 0 : boolean | ||
>x : number | undefined | ||
>0 : 0 | ||
>x : number | undefined | ||
>0 : 0 | ||
|
||
// Report from user | ||
function fn2(x?: string = someCondition ? 'value1' : x) { } | ||
>fn2 : (x?: string | undefined) => void | ||
>x : string | undefined | ||
>someCondition ? 'value1' : x : string | undefined | ||
>someCondition : any | ||
>'value1' : "value1" | ||
>x : string | 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
// @strictNullChecks: true | ||
|
||
// Constructed repro | ||
function fn1(x: number | undefined = x > 0 ? x : 0) { } | ||
|
||
// Report from user | ||
function fn2(x?: string = someCondition ? 'value1' : x) { } |