Skip to content

Commit cffc425

Browse files
authored
fix53287 mergeSymbol checks if the resolved target can merge with the source (#58326)
1 parent abc37af commit cffc425

File tree

36 files changed

+740
-182
lines changed

36 files changed

+740
-182
lines changed

src/compiler/checker.ts

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2621,7 +2621,16 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
26212621
if (resolvedTarget === unknownSymbol) {
26222622
return source;
26232623
}
2624-
target = cloneSymbol(resolvedTarget);
2624+
if (
2625+
!(resolvedTarget.flags & getExcludedSymbolFlags(source.flags)) ||
2626+
(source.flags | resolvedTarget.flags) & SymbolFlags.Assignment
2627+
) {
2628+
target = cloneSymbol(resolvedTarget);
2629+
}
2630+
else {
2631+
reportMergeSymbolError(target, source);
2632+
return source;
2633+
}
26252634
}
26262635
// Javascript static-property-assignment declarations always merge, even though they are also values
26272636
if (source.flags & SymbolFlags.ValueModule && target.flags & SymbolFlags.ValueModule && target.constEnumOnlyModule && !source.constEnumOnlyModule) {
@@ -2657,7 +2666,12 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
26572666
);
26582667
}
26592668
}
2660-
else { // error
2669+
else {
2670+
reportMergeSymbolError(target, source);
2671+
}
2672+
return target;
2673+
2674+
function reportMergeSymbolError(target: Symbol, source: Symbol) {
26612675
const isEitherEnum = !!(target.flags & SymbolFlags.Enum || source.flags & SymbolFlags.Enum);
26622676
const isEitherBlockScoped = !!(target.flags & SymbolFlags.BlockScopedVariable || source.flags & SymbolFlags.BlockScopedVariable);
26632677
const message = isEitherEnum ? Diagnostics.Enum_declarations_can_only_merge_with_namespace_or_other_enum_declarations
@@ -2684,7 +2698,6 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
26842698
if (!isTargetPlainJs) addDuplicateDeclarationErrorsForSymbols(target, message, symbolName, source);
26852699
}
26862700
}
2687-
return target;
26882701

26892702
function addDuplicateLocations(locs: Declaration[], symbol: Symbol): void {
26902703
if (symbol.declarations) {

tests/baselines/reference/checkMergedGlobalUMDSymbol.errors.txt

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,25 @@
1-
global.d.ts(6,16): error TS2403: Subsequent variable declarations must have the same type. Variable 'THREE' must be of type 'typeof import("global")', but here has type 'typeof import("three")'.
1+
global.d.ts(3,21): error TS2451: Cannot redeclare block-scoped variable 'THREE'.
2+
global.d.ts(6,16): error TS2451: Cannot redeclare block-scoped variable 'THREE'.
23

34

45
==== three.d.ts (0 errors) ====
56
export namespace THREE {
67
export class Vector2 {}
78
}
89

9-
==== global.d.ts (1 errors) ====
10+
==== global.d.ts (2 errors) ====
1011
import * as _three from './three';
1112

1213
export as namespace THREE;
14+
~~~~~
15+
!!! error TS2451: Cannot redeclare block-scoped variable 'THREE'.
16+
!!! related TS6203 global.d.ts:6:16: 'THREE' was also declared here.
1317

1418
declare global {
1519
export const THREE: typeof _three;
1620
~~~~~
17-
!!! error TS2403: Subsequent variable declarations must have the same type. Variable 'THREE' must be of type 'typeof import("global")', but here has type 'typeof import("three")'.
18-
!!! related TS6203 global.d.ts:1:1: 'THREE' was also declared here.
21+
!!! error TS2451: Cannot redeclare block-scoped variable 'THREE'.
22+
!!! related TS6203 global.d.ts:3:21: 'THREE' was also declared here.
1923
}
2024

2125
==== test.ts (0 errors) ====

tests/baselines/reference/checkMergedGlobalUMDSymbol.symbols

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,12 @@ declare global {
1919
>global : Symbol(global, Decl(global.d.ts, 2, 26))
2020

2121
export const THREE: typeof _three;
22-
>THREE : Symbol(THREE, Decl(global.d.ts, 0, 0), Decl(global.d.ts, 5, 14))
22+
>THREE : Symbol(THREE, Decl(global.d.ts, 5, 14))
2323
>_three : Symbol(_three, Decl(global.d.ts, 0, 6))
2424
}
2525

2626
=== test.ts ===
2727
const m = THREE
2828
>m : Symbol(m, Decl(test.ts, 0, 5))
29-
>THREE : Symbol(THREE, Decl(global.d.ts, 0, 0), Decl(global.d.ts, 5, 14))
29+
>THREE : Symbol(THREE, Decl(global.d.ts, 5, 14))
3030

tests/baselines/reference/checkMergedGlobalUMDSymbol.types

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,16 +24,16 @@ declare global {
2424
> : ^^^^^^^^^^^^^
2525

2626
export const THREE: typeof _three;
27-
>THREE : typeof import("global")
28-
> : ^^^^^^^^^^^^^^^^^^^^^^^
27+
>THREE : typeof _three
28+
> : ^^^^^^^^^^^^^
2929
>_three : typeof _three
3030
> : ^^^^^^^^^^^^^
3131
}
3232

3333
=== test.ts ===
3434
const m = THREE
35-
>m : typeof import("global")
36-
> : ^^^^^^^^^^^^^^^^^^^^^^^
37-
>THREE : typeof import("global")
38-
> : ^^^^^^^^^^^^^^^^^^^^^^^
35+
>m : typeof import("three")
36+
> : ^^^^^^^^^^^^^^^^^^^^^^
37+
>THREE : typeof import("three")
38+
> : ^^^^^^^^^^^^^^^^^^^^^^
3939

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/node_modules/@fullcalendar/core/index.d.ts(4,10): error TS2300: Duplicate identifier 'VNode'.
2+
/node_modules/@fullcalendar/react/index.d.ts(4,19): error TS2300: Duplicate identifier 'VNode'.
3+
4+
5+
==== /node_modules/@fullcalendar/react/index.d.ts (1 errors) ====
6+
import * as react from 'react';
7+
declare global {
8+
namespace FullCalendarVDom {
9+
export import VNode = react.ReactNode;
10+
~~~~~
11+
!!! error TS2300: Duplicate identifier 'VNode'.
12+
!!! related TS6203 /node_modules/@fullcalendar/core/index.d.ts:4:10: 'VNode' was also declared here.
13+
}
14+
}
15+
16+
export default class FullCalendar {
17+
}
18+
19+
==== /node_modules/@fullcalendar/core/index.d.ts (1 errors) ====
20+
import * as preact from 'preact';
21+
declare global {
22+
namespace FullCalendarVDom {
23+
type VNode = preact.VNode<any>;
24+
~~~~~
25+
!!! error TS2300: Duplicate identifier 'VNode'.
26+
!!! related TS6203 /node_modules/@fullcalendar/react/index.d.ts:4:19: 'VNode' was also declared here.
27+
}
28+
}
29+
30+
export type EventInput = any;
31+
32+
==== /node_modules/@types/react/index.d.ts (0 errors) ====
33+
export = React;
34+
export as namespace React;
35+
declare namespace React {
36+
type ReactNode = any;
37+
function useMemo<T>(factory: () => T, deps: undefined): T;
38+
}
39+
40+
==== /node_modules/preact/index.d.ts (0 errors) ====
41+
export as namespace preact;
42+
export interface VNode<P = {}> {}
43+
44+
==== /index.tsx (0 errors) ====
45+
import FullCalendar from "@fullcalendar/react";
46+
import { EventInput } from "@fullcalendar/core";
47+

tests/baselines/reference/checkerInitializationCrash.symbols

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ declare global {
1313
export import VNode = react.ReactNode;
1414
>VNode : Symbol(FullCalendarVDom.VNode, Decl(index.d.ts, 2, 30))
1515
>react : Symbol(react, Decl(index.d.ts, 0, 6))
16-
>ReactNode : Symbol(react.ReactNode, Decl(index.d.ts, 2, 25), Decl(index.d.ts, 2, 30))
16+
>ReactNode : Symbol(react.ReactNode, Decl(index.d.ts, 2, 25))
1717
}
1818
}
1919

@@ -32,7 +32,7 @@ declare global {
3232
>FullCalendarVDom : Symbol(FullCalendarVDom, Decl(index.d.ts, 1, 16), Decl(index.d.ts, 1, 16))
3333

3434
type VNode = preact.VNode<any>;
35-
>VNode : Symbol(React.ReactNode, Decl(index.d.ts, 2, 25), Decl(index.d.ts, 2, 30))
35+
>VNode : Symbol(VNode, Decl(index.d.ts, 2, 30))
3636
>preact : Symbol(preact, Decl(index.d.ts, 0, 6))
3737
>VNode : Symbol(preact.VNode, Decl(index.d.ts, 0, 27))
3838
}
@@ -52,7 +52,7 @@ declare namespace React {
5252
>React : Symbol(React, Decl(index.d.ts, 1, 26))
5353

5454
type ReactNode = any;
55-
>ReactNode : Symbol(ReactNode, Decl(index.d.ts, 2, 25), Decl(index.d.ts, 2, 30))
55+
>ReactNode : Symbol(ReactNode, Decl(index.d.ts, 2, 25))
5656

5757
function useMemo<T>(factory: () => T, deps: undefined): T;
5858
>useMemo : Symbol(useMemo, Decl(index.d.ts, 3, 25))

tests/baselines/reference/checkerInitializationCrash.types

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,14 +39,16 @@ declare global {
3939

4040
namespace FullCalendarVDom {
4141
type VNode = preact.VNode<any>;
42-
>VNode : any
42+
>VNode : VNode
43+
> : ^^^^^
4344
>preact : any
4445
> : ^^^
4546
}
4647
}
4748

4849
export type EventInput = any;
4950
>EventInput : any
51+
> : ^^^
5052

5153
=== /node_modules/@types/react/index.d.ts ===
5254
export = React;
@@ -63,6 +65,7 @@ declare namespace React {
6365

6466
type ReactNode = any;
6567
>ReactNode : any
68+
> : ^^^
6669

6770
function useMemo<T>(factory: () => T, deps: undefined): T;
6871
>useMemo : <T>(factory: () => T, deps: undefined) => T
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
bar.d.ts(2,21): error TS2451: Cannot redeclare block-scoped variable 'foo'.
2+
bar.d.ts(6,11): error TS2451: Cannot redeclare block-scoped variable 'foo'.
3+
bar.d.ts(6,11): error TS2502: 'foo' is referenced directly or indirectly in its own type annotation.
4+
5+
6+
==== bar.d.ts (3 errors) ====
7+
import * as foo from './foo'
8+
export as namespace foo
9+
~~~
10+
!!! error TS2451: Cannot redeclare block-scoped variable 'foo'.
11+
!!! related TS6203 bar.d.ts:6:11: 'foo' was also declared here.
12+
export = foo;
13+
14+
declare global {
15+
const foo: typeof foo;
16+
~~~
17+
!!! error TS2451: Cannot redeclare block-scoped variable 'foo'.
18+
!!! related TS6203 bar.d.ts:2:21: 'foo' was also declared here.
19+
~~~
20+
!!! error TS2502: 'foo' is referenced directly or indirectly in its own type annotation.
21+
}
22+
23+
==== foo.d.ts (0 errors) ====
24+
interface Root {
25+
/**
26+
* A .default property for ES6 default import compatibility
27+
*/
28+
default: Root;
29+
}
30+
31+
declare const root: Root;
32+
export = root;
33+

tests/baselines/reference/crashDeclareGlobalTypeofExport.symbols

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ declare global {
1414
>global : Symbol(global, Decl(bar.d.ts, 2, 13))
1515

1616
const foo: typeof foo;
17-
>foo : Symbol(foo, Decl(foo.d.ts, 7, 13), Decl(bar.d.ts, 5, 9))
18-
>foo : Symbol(foo, Decl(foo.d.ts, 7, 13), Decl(bar.d.ts, 5, 9))
17+
>foo : Symbol(foo, Decl(bar.d.ts, 5, 9))
18+
>foo : Symbol(foo, Decl(bar.d.ts, 5, 9))
1919
}
2020

2121
=== foo.d.ts ===

tests/baselines/reference/crashDeclareGlobalTypeofExport.types

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,10 @@ declare global {
1818
> : ^^^^^^^^^^^^^
1919

2020
const foo: typeof foo;
21-
>foo : Root
22-
> : ^^^^
23-
>foo : Root
24-
> : ^^^^
21+
>foo : any
22+
> : ^^^
23+
>foo : any
24+
> : ^^^
2525
}
2626

2727
=== foo.d.ts ===
Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
input.ts(1,1): error TS2309: An export assignment cannot be used in a module with other exported elements.
2-
input.ts(6,14): error TS2323: Cannot redeclare exported variable 'Sub'.
2+
input.ts(6,14): error TS2300: Duplicate identifier 'Sub'.
3+
input.ts(12,14): error TS2300: Duplicate identifier 'Sub'.
34

45

5-
==== input.ts (2 errors) ====
6+
==== input.ts (3 errors) ====
67
export = exports;
78
~~~~~~~~~~~~~~~~~
89
!!! error TS2309: An export assignment cannot be used in a module with other exported elements.
@@ -12,11 +13,15 @@ input.ts(6,14): error TS2323: Cannot redeclare exported variable 'Sub'.
1213
}
1314
export class Sub {
1415
~~~
15-
!!! error TS2323: Cannot redeclare exported variable 'Sub'.
16+
!!! error TS2300: Duplicate identifier 'Sub'.
17+
!!! related TS6203 input.ts:12:14: 'Sub' was also declared here.
1618
instance!: {
1719
t: number;
1820
};
1921
}
2022
declare namespace exports {
2123
export { Sub };
24+
~~~
25+
!!! error TS2300: Duplicate identifier 'Sub'.
26+
!!! related TS6203 input.ts:6:14: 'Sub' was also declared here.
2227
}

tests/baselines/reference/declarationFileNoCrashOnExtraExportModifier.symbols

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ declare class exports {
1414
>t : Symbol(exports.t, Decl(input.ts, 2, 27))
1515
}
1616
export class Sub {
17-
>Sub : Symbol(Sub, Decl(input.ts, 4, 1), Decl(input.ts, 4, 1))
17+
>Sub : Symbol(Sub, Decl(input.ts, 4, 1))
1818

1919
instance!: {
2020
>instance : Symbol(Sub.instance, Decl(input.ts, 5, 18))

tests/baselines/reference/declarationFileNoCrashOnExtraExportModifier.types

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,6 @@ declare namespace exports {
3636
> : ^^^^^^^^^^^^^^
3737

3838
export { Sub };
39-
>Sub : typeof import("input").Sub
40-
> : ^^^^^^^^^^^^^^^^^^^^^^^^^^
39+
>Sub : typeof Sub
40+
> : ^^^^^^^^^^
4141
}

0 commit comments

Comments
 (0)