Skip to content

Commit 0f598db

Browse files
authored
Fixes #29524, a merged UMD-global (#30403)
This kind of merged symbol causes crashes in two places because it's marked BlockScoped, which makes us assume that it must be something that is inside a SourceFile. However, block-scoped checks don't make sense for this kind of symbol, so I exclude them by looking at the kind of the valueDeclaration, as @mprobst suggested in the original bug.
1 parent 563593b commit 0f598db

6 files changed

+118
-1
lines changed

src/compiler/checker.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16936,6 +16936,7 @@ namespace ts {
1693616936
function checkNestedBlockScopedBinding(node: Identifier, symbol: Symbol): void {
1693716937
if (languageVersion >= ScriptTarget.ES2015 ||
1693816938
(symbol.flags & (SymbolFlags.BlockScopedVariable | SymbolFlags.Class)) === 0 ||
16939+
isSourceFile(symbol.valueDeclaration) ||
1693916940
symbol.valueDeclaration.parent.kind === SyntaxKind.CatchClause) {
1694016941
return;
1694116942
}
@@ -29594,7 +29595,7 @@ namespace ts {
2959429595
}
2959529596

2959629597
function isSymbolOfDeclarationWithCollidingName(symbol: Symbol): boolean {
29597-
if (symbol.flags & SymbolFlags.BlockScoped) {
29598+
if (symbol.flags & SymbolFlags.BlockScoped && !isSourceFile(symbol.valueDeclaration)) {
2959829599
const links = getSymbolLinks(symbol);
2959929600
if (links.isDeclarationWithCollidingName === undefined) {
2960029601
const container = getEnclosingBlockScopeContainer(symbol.valueDeclaration);
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
tests/cases/compiler/global.d.ts(6,16): error TS2403: Subsequent variable declarations must have the same type. Variable 'THREE' must be of type 'typeof import("tests/cases/compiler/global")', but here has type 'typeof import("tests/cases/compiler/three")'.
2+
3+
4+
==== tests/cases/compiler/three.d.ts (0 errors) ====
5+
export namespace THREE {
6+
export class Vector2 {}
7+
}
8+
9+
==== tests/cases/compiler/global.d.ts (1 errors) ====
10+
import * as _three from './three';
11+
12+
export as namespace THREE;
13+
14+
declare global {
15+
export const THREE: typeof _three;
16+
~~~~~
17+
!!! error TS2403: Subsequent variable declarations must have the same type. Variable 'THREE' must be of type 'typeof import("tests/cases/compiler/global")', but here has type 'typeof import("tests/cases/compiler/three")'.
18+
}
19+
20+
==== tests/cases/compiler/test.ts (0 errors) ====
21+
const m = THREE
22+
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
//// [tests/cases/compiler/checkMergedGlobalUMDSymbol.ts] ////
2+
3+
//// [three.d.ts]
4+
export namespace THREE {
5+
export class Vector2 {}
6+
}
7+
8+
//// [global.d.ts]
9+
import * as _three from './three';
10+
11+
export as namespace THREE;
12+
13+
declare global {
14+
export const THREE: typeof _three;
15+
}
16+
17+
//// [test.ts]
18+
const m = THREE
19+
20+
21+
//// [test.js]
22+
var m = THREE;
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
=== tests/cases/compiler/three.d.ts ===
2+
export namespace THREE {
3+
>THREE : Symbol(THREE, Decl(three.d.ts, 0, 0))
4+
5+
export class Vector2 {}
6+
>Vector2 : Symbol(Vector2, Decl(three.d.ts, 0, 24))
7+
}
8+
9+
=== tests/cases/compiler/global.d.ts ===
10+
import * as _three from './three';
11+
>_three : Symbol(_three, Decl(global.d.ts, 0, 6))
12+
13+
export as namespace THREE;
14+
>THREE : Symbol(THREE, Decl(global.d.ts, 0, 34))
15+
16+
declare global {
17+
>global : Symbol(global, Decl(global.d.ts, 2, 26))
18+
19+
export const THREE: typeof _three;
20+
>THREE : Symbol("tests/cases/compiler/global", Decl(global.d.ts, 0, 0), Decl(global.d.ts, 5, 14))
21+
>_three : Symbol(_three, Decl(global.d.ts, 0, 6))
22+
}
23+
24+
=== tests/cases/compiler/test.ts ===
25+
const m = THREE
26+
>m : Symbol(m, Decl(test.ts, 0, 5))
27+
>THREE : Symbol("tests/cases/compiler/global", Decl(global.d.ts, 0, 0), Decl(global.d.ts, 5, 14))
28+
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
=== tests/cases/compiler/three.d.ts ===
2+
export namespace THREE {
3+
>THREE : typeof THREE
4+
5+
export class Vector2 {}
6+
>Vector2 : Vector2
7+
}
8+
9+
=== tests/cases/compiler/global.d.ts ===
10+
import * as _three from './three';
11+
>_three : typeof _three
12+
13+
export as namespace THREE;
14+
>THREE : typeof import("tests/cases/compiler/global")
15+
16+
declare global {
17+
>global : typeof global
18+
19+
export const THREE: typeof _three;
20+
>THREE : typeof import("tests/cases/compiler/global")
21+
>_three : typeof _three
22+
}
23+
24+
=== tests/cases/compiler/test.ts ===
25+
const m = THREE
26+
>m : typeof import("tests/cases/compiler/global")
27+
>THREE : typeof import("tests/cases/compiler/global")
28+
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// @Filename: three.d.ts
2+
export namespace THREE {
3+
export class Vector2 {}
4+
}
5+
6+
// @Filename: global.d.ts
7+
import * as _three from './three';
8+
9+
export as namespace THREE;
10+
11+
declare global {
12+
export const THREE: typeof _three;
13+
}
14+
15+
// @Filename: test.ts
16+
const m = THREE

0 commit comments

Comments
 (0)