fixes #48630 array binding pattern with only OmittedExpressions does not check RHS of for-of loop (#49008)

* fix RHS of for..of loop not evaluated when LHS is array binding element with OmittedExpression

* expand widened type check

* add more test cases

* update code with suggestions

* Make test target es2015

Co-authored-by: Andrew Branch <andrew@wheream.io>
This commit is contained in:
bentongxyz
2022-06-01 02:06:19 +08:00
committed by GitHub
parent 7baaf7be1c
commit 3939b38aa1
8 changed files with 109 additions and 1 deletions

View File

@@ -37933,7 +37933,7 @@ namespace ts {
// For a binding pattern, validate the initializer and exit
if (isBindingPattern(node.name)) {
const needCheckInitializer = node.initializer && node.parent.parent.kind !== SyntaxKind.ForInStatement;
const needCheckWidenedType = node.name.elements.length === 0;
const needCheckWidenedType = !some(node.name.elements, not(isOmittedExpression));
if (needCheckInitializer || needCheckWidenedType) {
// Don't validate for-in initializer as it is already an error
const widenedType = getWidenedTypeForVariableLikeDeclaration(node);

9
test.js Normal file
View File

@@ -0,0 +1,9 @@
"use strict";
exports.__esModule = true;
exports.main = void 0;
function main() {
for (var _i = 0, doesNotExist_1 = doesNotExist; _i < doesNotExist_1.length; _i++) {
var _a = doesNotExist_1[_i];
}
}
exports.main = main;

4
test.ts Normal file
View File

@@ -0,0 +1,4 @@
export function main() {
for (const [,] of doesNotExist) {
}
}

View File

@@ -0,0 +1,26 @@
tests/cases/compiler/omittedExpressionForOfLoop.ts(1,19): error TS2304: Cannot find name 'doesNotExist'.
tests/cases/compiler/omittedExpressionForOfLoop.ts(4,19): error TS2532: Object is possibly 'undefined'.
tests/cases/compiler/omittedExpressionForOfLoop.ts(7,12): error TS2488: Type 'never' must have a '[Symbol.iterator]()' method that returns an iterator.
tests/cases/compiler/omittedExpressionForOfLoop.ts(10,12): error TS2488: Type 'never' must have a '[Symbol.iterator]()' method that returns an iterator.
==== tests/cases/compiler/omittedExpressionForOfLoop.ts (4 errors) ====
for (const [,] of doesNotExist) {
~~~~~~~~~~~~
!!! error TS2304: Cannot find name 'doesNotExist'.
}
for (const [,] of undefined) {
~~~~~~~~~
!!! error TS2532: Object is possibly 'undefined'.
}
for (const [,] of []) {
~~~
!!! error TS2488: Type 'never' must have a '[Symbol.iterator]()' method that returns an iterator.
}
for (const [] of []) {
~~
!!! error TS2488: Type 'never' must have a '[Symbol.iterator]()' method that returns an iterator.
}

View File

@@ -0,0 +1,23 @@
//// [omittedExpressionForOfLoop.ts]
for (const [,] of doesNotExist) {
}
for (const [,] of undefined) {
}
for (const [,] of []) {
}
for (const [] of []) {
}
//// [omittedExpressionForOfLoop.js]
"use strict";
for (const [,] of doesNotExist) {
}
for (const [,] of undefined) {
}
for (const [,] of []) {
}
for (const [] of []) {
}

View File

@@ -0,0 +1,13 @@
=== tests/cases/compiler/omittedExpressionForOfLoop.ts ===
for (const [,] of doesNotExist) {
}
for (const [,] of undefined) {
>undefined : Symbol(undefined)
}
for (const [,] of []) {
}
for (const [] of []) {
}

View File

@@ -0,0 +1,19 @@
=== tests/cases/compiler/omittedExpressionForOfLoop.ts ===
for (const [,] of doesNotExist) {
> : undefined
>doesNotExist : any
}
for (const [,] of undefined) {
> : undefined
>undefined : undefined
}
for (const [,] of []) {
> : undefined
>[] : never[]
}
for (const [] of []) {
>[] : never[]
}

View File

@@ -0,0 +1,14 @@
// @strict: true
// @target: es2015
for (const [,] of doesNotExist) {
}
for (const [,] of undefined) {
}
for (const [,] of []) {
}
for (const [] of []) {
}