Skip to content

Commit ff68b7a

Browse files
committed
deduplicate emitted namespaces
1 parent 5f6eb6e commit ff68b7a

31 files changed

+316
-322
lines changed

internal/transformers/declarations/transform.go

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1826,17 +1826,12 @@ func (tx *DeclarationTransformer) visitExpressionStatement(node *ast.ExpressionS
18261826

18271827
func (tx *DeclarationTransformer) transformExpandoAssignment(node *ast.BinaryExpression) *ast.Node {
18281828
left := node.Left
1829-
right := node.Right
18301829

18311830
symbol := node.Symbol
18321831
if symbol == nil || symbol.Flags&ast.SymbolFlagsAssignment == 0 {
18331832
return nil
18341833
}
18351834

1836-
if ast.IsExpandoInitializer(right) {
1837-
return nil
1838-
}
1839-
18401835
ns := ast.GetLeftmostAccessExpression(left)
18411836
if ns == nil || ns.Kind != ast.KindIdentifier {
18421837
return nil
@@ -1847,6 +1842,13 @@ func (tx *DeclarationTransformer) transformExpandoAssignment(node *ast.BinaryExp
18471842
return nil
18481843
}
18491844

1845+
if ast.IsVariableDeclaration(declaration) {
1846+
id := ast.GetNodeId(tx.EmitContext().MostOriginal(declaration.Parent.Parent))
1847+
if tx.lateStatementReplacementMap[id] != nil {
1848+
return nil
1849+
}
1850+
}
1851+
18501852
host := declaration.Symbol()
18511853
if host == nil {
18521854
return nil

testdata/baselines/reference/submodule/compiler/declarationEmitLateBoundAssignments2.js

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -136,14 +136,14 @@ export declare function decl7(): void;
136136
export declare function decl8(): void;
137137
export declare function decl9(): void;
138138
export declare function decl10(): void;
139-
export declare function arrow(): void;
140-
export declare namespace arrow {
141-
const B: string;
142-
}
143-
export declare function arrow2(): void;
144-
export declare namespace arrow2 {
145-
const C: number;
146-
}
139+
export declare const arrow: {
140+
(): void;
141+
B: string;
142+
};
143+
export declare const arrow2: {
144+
(): void;
145+
C: number;
146+
};
147147
export declare const arrow3: {
148148
(): void;
149149
77: number;

testdata/baselines/reference/submodule/compiler/declarationEmitLateBoundAssignments2.js.diff

Lines changed: 2 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -28,22 +28,6 @@
2828
-export declare namespace decl9 { }
2929
export declare function decl10(): void;
3030
-export declare namespace decl10 { }
31-
-export declare const arrow: {
32-
- (): void;
33-
- B: string;
34-
-};
35-
-export declare const arrow2: {
36-
- (): void;
37-
- C: number;
38-
-};
39-
+export declare function arrow(): void;
40-
+export declare namespace arrow {
41-
+ const B: string;
42-
+}
43-
+export declare function arrow2(): void;
44-
+export declare namespace arrow2 {
45-
+ const C: number;
46-
+}
47-
export declare const arrow3: {
31+
export declare const arrow: {
4832
(): void;
49-
77: number;
33+
B: string;

testdata/baselines/reference/submodule/compiler/expandoFunctionExpressionsWithDynamicNames.js

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,11 @@ exports.expr2[s] = 0;
2727

2828

2929
//// [expandoFunctionExpressionsWithDynamicNames.d.ts]
30-
export declare function expr(): void;
31-
export declare namespace expr {
32-
const X: number;
33-
}
34-
export declare function expr2(): void;
35-
export declare namespace expr2 {
36-
const X: number;
37-
}
30+
export declare const expr: {
31+
(): void;
32+
X: number;
33+
};
34+
export declare const expr2: {
35+
(): void;
36+
X: number;
37+
};

testdata/baselines/reference/submodule/compiler/expandoFunctionExpressionsWithDynamicNames.js.diff

Lines changed: 1 addition & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -10,24 +10,4 @@
1010
+// https://github.com/microsoft/TypeScript/issues/54809
1111
const s = "X";
1212
const expr = () => { };
13-
exports.expr = expr;
14-
@@= skipped -13, +13 lines =@@
15-
16-
17-
//// [expandoFunctionExpressionsWithDynamicNames.d.ts]
18-
-export declare const expr: {
19-
- (): void;
20-
- X: number;
21-
-};
22-
-export declare const expr2: {
23-
- (): void;
24-
- X: number;
25-
-};
26-
+export declare function expr(): void;
27-
+export declare namespace expr {
28-
+ const X: number;
29-
+}
30-
+export declare function expr2(): void;
31-
+export declare namespace expr2 {
32-
+ const X: number;
33-
+}
13+
exports.expr = expr;

testdata/baselines/reference/submodule/compiler/isolatedDeclarationErrors.js

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,11 @@ declare function errorOnAssignmentBelowDecl(): void;
2525
declare namespace errorOnAssignmentBelowDecl {
2626
const a: string;
2727
}
28-
declare function errorOnAssignmentBelow(): void;
29-
declare namespace errorOnAssignmentBelow {
30-
const a: string;
31-
}
32-
declare function errorOnMissingReturn(): void;
33-
declare namespace errorOnMissingReturn {
34-
const a: string;
35-
}
28+
declare const errorOnAssignmentBelow: {
29+
(): void;
30+
a: string;
31+
};
32+
declare const errorOnMissingReturn: {
33+
(): void;
34+
a: string;
35+
};

testdata/baselines/reference/submodule/compiler/isolatedDeclarationErrors.js.diff

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,11 @@
1111
+declare namespace errorOnAssignmentBelowDecl {
1212
+ const a: string;
1313
+}
14-
+declare function errorOnAssignmentBelow(): void;
15-
+declare namespace errorOnAssignmentBelow {
16-
+ const a: string;
17-
+}
18-
+declare function errorOnMissingReturn(): void;
19-
+declare namespace errorOnMissingReturn {
20-
+ const a: string;
21-
+}
14+
+declare const errorOnAssignmentBelow: {
15+
+ (): void;
16+
+ a: string;
17+
+};
18+
+declare const errorOnMissingReturn: {
19+
+ (): void;
20+
+ a: string;
21+
+};

testdata/baselines/reference/submodule/compiler/isolatedDeclarationErrorsExpandoFunctions.js

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,21 @@ foo.length = 10;
2525

2626
//// [isolatedDeclarationErrorsExpandoFunctions.d.ts]
2727
export declare function foo(): void;
28+
export declare namespace foo {
29+
const apply: () => void;
30+
}
31+
export declare namespace foo {
32+
const call: () => void;
33+
}
34+
export declare namespace foo {
35+
const bind: () => void;
36+
}
37+
export declare namespace foo {
38+
const caller: () => void;
39+
}
40+
export declare namespace foo {
41+
const toString: () => void;
42+
}
2843
export declare namespace foo {
2944
const length: number;
3045
}
@@ -36,12 +51,27 @@ export declare namespace foo {
3651
//// [DtsFileErrors]
3752

3853

39-
isolatedDeclarationErrorsExpandoFunctions.d.ts(3,11): error TS2451: Cannot redeclare block-scoped variable 'length'.
40-
isolatedDeclarationErrorsExpandoFunctions.d.ts(6,11): error TS2451: Cannot redeclare block-scoped variable 'length'.
54+
isolatedDeclarationErrorsExpandoFunctions.d.ts(18,11): error TS2451: Cannot redeclare block-scoped variable 'length'.
55+
isolatedDeclarationErrorsExpandoFunctions.d.ts(21,11): error TS2451: Cannot redeclare block-scoped variable 'length'.
4156

4257

4358
==== isolatedDeclarationErrorsExpandoFunctions.d.ts (2 errors) ====
4459
export declare function foo(): void;
60+
export declare namespace foo {
61+
const apply: () => void;
62+
}
63+
export declare namespace foo {
64+
const call: () => void;
65+
}
66+
export declare namespace foo {
67+
const bind: () => void;
68+
}
69+
export declare namespace foo {
70+
const caller: () => void;
71+
}
72+
export declare namespace foo {
73+
const toString: () => void;
74+
}
4575
export declare namespace foo {
4676
const length: number;
4777
~~~~~~

testdata/baselines/reference/submodule/compiler/isolatedDeclarationErrorsExpandoFunctions.js.diff

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,21 @@
99
+//// [isolatedDeclarationErrorsExpandoFunctions.d.ts]
1010
+export declare function foo(): void;
1111
+export declare namespace foo {
12+
+ const apply: () => void;
13+
+}
14+
+export declare namespace foo {
15+
+ const call: () => void;
16+
+}
17+
+export declare namespace foo {
18+
+ const bind: () => void;
19+
+}
20+
+export declare namespace foo {
21+
+ const caller: () => void;
22+
+}
23+
+export declare namespace foo {
24+
+ const toString: () => void;
25+
+}
26+
+export declare namespace foo {
1227
+ const length: number;
1328
+}
1429
+export declare namespace foo {
@@ -19,13 +34,28 @@
1934
+//// [DtsFileErrors]
2035
+
2136
+
22-
+isolatedDeclarationErrorsExpandoFunctions.d.ts(3,11): error TS2451: Cannot redeclare block-scoped variable 'length'.
23-
+isolatedDeclarationErrorsExpandoFunctions.d.ts(6,11): error TS2451: Cannot redeclare block-scoped variable 'length'.
37+
+isolatedDeclarationErrorsExpandoFunctions.d.ts(18,11): error TS2451: Cannot redeclare block-scoped variable 'length'.
38+
+isolatedDeclarationErrorsExpandoFunctions.d.ts(21,11): error TS2451: Cannot redeclare block-scoped variable 'length'.
2439
+
2540
+
2641
+==== isolatedDeclarationErrorsExpandoFunctions.d.ts (2 errors) ====
2742
+ export declare function foo(): void;
2843
+ export declare namespace foo {
44+
+ const apply: () => void;
45+
+ }
46+
+ export declare namespace foo {
47+
+ const call: () => void;
48+
+ }
49+
+ export declare namespace foo {
50+
+ const bind: () => void;
51+
+ }
52+
+ export declare namespace foo {
53+
+ const caller: () => void;
54+
+ }
55+
+ export declare namespace foo {
56+
+ const toString: () => void;
57+
+ }
58+
+ export declare namespace foo {
2959
+ const length: number;
3060
+ ~~~~~~
3161
+!!! error TS2451: Cannot redeclare block-scoped variable 'length'.

testdata/baselines/reference/submodule/compiler/jsDeclarationsGlobalFileConstFunction.js

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,11 @@ SomeConstructor3.staticMember = "str";
1919

2020
//// [file.d.ts]
2121
declare const SomeConstructor: () => void;
22-
declare function SomeConstructor2(): void;
23-
declare namespace SomeConstructor2 {
24-
const staticMember: string;
25-
}
26-
declare function SomeConstructor3(): void;
27-
declare namespace SomeConstructor3 {
28-
const staticMember: string;
29-
}
22+
declare const SomeConstructor2: {
23+
(): void;
24+
staticMember: string;
25+
};
26+
declare const SomeConstructor3: {
27+
(): void;
28+
staticMember: string;
29+
};

0 commit comments

Comments
 (0)