Skip to content

Commit 9044589

Browse files
authored
Fix duplicate errors in js special assignments (microsoft#24508)
* Fix duplicate errors in js special assignments * Simplify checkExpressionCached call to checkExpression * Accept baselines after merge * Use Map for deferredNodes and improve NoDeferredCheck comment I added an assert when a duplicate was added, but it caused 18 failures in our test suite. * Remove NoDeferredCheck
1 parent 38dab74 commit 9044589

File tree

5 files changed

+96
-5
lines changed

5 files changed

+96
-5
lines changed

src/compiler/checker.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -465,7 +465,7 @@ namespace ts {
465465
let deferredGlobalImportMetaType: ObjectType;
466466
let deferredGlobalExtractSymbol: Symbol;
467467

468-
let deferredNodes: Node[] | undefined;
468+
let deferredNodes: Map<Node> | undefined;
469469
const allPotentiallyUnusedIdentifiers = createMap<PotentiallyUnusedIdentifier[]>(); // key is file name
470470

471471
let flowLoopStart = 0;
@@ -26070,12 +26070,13 @@ namespace ts {
2607026070
// Delaying the type check of the body ensures foo has been assigned a type.
2607126071
function checkNodeDeferred(node: Node) {
2607226072
if (deferredNodes) {
26073-
deferredNodes.push(node);
26073+
const id = "" + getNodeId(node);
26074+
deferredNodes.set(id, node);
2607426075
}
2607526076
}
2607626077

2607726078
function checkDeferredNodes() {
26078-
for (const node of deferredNodes!) {
26079+
deferredNodes!.forEach(node => {
2607926080
switch (node.kind) {
2608026081
case SyntaxKind.FunctionExpression:
2608126082
case SyntaxKind.ArrowFunction:
@@ -26091,7 +26092,7 @@ namespace ts {
2609126092
checkClassExpressionDeferred(<ClassExpression>node);
2609226093
break;
2609326094
}
26094-
}
26095+
});
2609526096
}
2609626097

2609726098
function checkSourceFile(node: SourceFile) {
@@ -26133,7 +26134,7 @@ namespace ts {
2613326134
clear(potentialThisCollisions);
2613426135
clear(potentialNewTargetCollisions);
2613526136

26136-
deferredNodes = [];
26137+
deferredNodes = createMap<Node>();
2613726138
forEach(node.statements, checkSourceElement);
2613826139

2613926140
checkDeferredNodes();
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
tests/cases/conformance/salsa/bug24252.js(8,9): error TS2322: Type 'string[]' is not assignable to type 'number[]'.
2+
Type 'string' is not assignable to type 'number'.
3+
4+
5+
==== tests/cases/conformance/salsa/bug24252.js (1 errors) ====
6+
var A = {};
7+
A.B = class {
8+
m() {
9+
/** @type {string[]} */
10+
var x = [];
11+
/** @type {number[]} */
12+
var y;
13+
y = x;
14+
~
15+
!!! error TS2322: Type 'string[]' is not assignable to type 'number[]'.
16+
!!! error TS2322: Type 'string' is not assignable to type 'number'.
17+
}
18+
};
19+
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
=== tests/cases/conformance/salsa/bug24252.js ===
2+
var A = {};
3+
>A : Symbol(A, Decl(bug24252.js, 0, 3), Decl(bug24252.js, 0, 11))
4+
5+
A.B = class {
6+
>A.B : Symbol(A.B, Decl(bug24252.js, 0, 11))
7+
>A : Symbol(A, Decl(bug24252.js, 0, 3), Decl(bug24252.js, 0, 11))
8+
>B : Symbol(A.B, Decl(bug24252.js, 0, 11))
9+
10+
m() {
11+
>m : Symbol(B.m, Decl(bug24252.js, 1, 13))
12+
13+
/** @type {string[]} */
14+
var x = [];
15+
>x : Symbol(x, Decl(bug24252.js, 4, 11))
16+
17+
/** @type {number[]} */
18+
var y;
19+
>y : Symbol(y, Decl(bug24252.js, 6, 11))
20+
21+
y = x;
22+
>y : Symbol(y, Decl(bug24252.js, 6, 11))
23+
>x : Symbol(x, Decl(bug24252.js, 4, 11))
24+
}
25+
};
26+
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
=== tests/cases/conformance/salsa/bug24252.js ===
2+
var A = {};
3+
>A : typeof A
4+
>{} : { [x: string]: any; }
5+
6+
A.B = class {
7+
>A.B = class { m() { /** @type {string[]} */ var x = []; /** @type {number[]} */ var y; y = x; }} : typeof B
8+
>A.B : typeof B
9+
>A : typeof A
10+
>B : typeof B
11+
>class { m() { /** @type {string[]} */ var x = []; /** @type {number[]} */ var y; y = x; }} : typeof B
12+
13+
m() {
14+
>m : () => void
15+
16+
/** @type {string[]} */
17+
var x = [];
18+
>x : string[]
19+
>[] : undefined[]
20+
21+
/** @type {number[]} */
22+
var y;
23+
>y : number[]
24+
25+
y = x;
26+
>y = x : string[]
27+
>y : number[]
28+
>x : string[]
29+
}
30+
};
31+
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// @noEmit: true
2+
// @checkJs: true
3+
// @allowJs: true
4+
// @Filename: bug24252.js
5+
var A = {};
6+
A.B = class {
7+
m() {
8+
/** @type {string[]} */
9+
var x = [];
10+
/** @type {number[]} */
11+
var y;
12+
y = x;
13+
}
14+
};

0 commit comments

Comments
 (0)