Skip to content

Commit d1c76ce

Browse files
committed
fix typeof enclosing declaration
1 parent cd93375 commit d1c76ce

File tree

68 files changed

+1041
-898
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

68 files changed

+1041
-898
lines changed

internal/transformers/declarations/transform.go

Lines changed: 38 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1812,12 +1812,39 @@ func (tx *DeclarationTransformer) collectExpandoAssignments(node *ast.BinaryExpr
18121812
return
18131813
}
18141814

1815+
namespaceName := ast.GetFirstIdentifier(left.AsPropertyAccessExpression().Expression).Text()
1816+
id := ast.GetSymbolId(host)
1817+
modifierFlags := ast.ModifierFlagsAmbient
1818+
if host.ValueDeclaration.ModifierFlags()&ast.ModifierFlagsExport != 0 && host.ValueDeclaration.ModifierFlags()&ast.ModifierFlagsDefault == 0 {
1819+
modifierFlags |= ast.ModifierFlagsExport
1820+
}
1821+
modifiers := tx.Factory().NewModifierList(ast.CreateModifiersFromModifierFlags(modifierFlags, tx.Factory().NewModifier))
1822+
1823+
synthesizedModuleDeclaration := core.IfElse(
1824+
tx.pendingExpando[id] == nil,
1825+
tx.Factory().NewModuleDeclaration(
1826+
modifiers,
1827+
ast.KindNamespaceKeyword,
1828+
tx.Factory().NewIdentifier(namespaceName),
1829+
tx.Factory().NewModuleBlock(tx.Factory().NewNodeList([]*ast.Node{})),
1830+
),
1831+
tx.pendingExpando[id],
1832+
).AsModuleDeclaration()
1833+
1834+
synthesizedModuleDeclaration.Parent = tx.enclosingDeclaration
1835+
1836+
declarationData := synthesizedModuleDeclaration.DeclarationData()
1837+
declarationData.Symbol = host
1838+
1839+
containerData := synthesizedModuleDeclaration.LocalsContainerData()
1840+
containerData.Locals = make(ast.SymbolTable, 0)
1841+
18151842
saveDiag := tx.state.getSymbolAccessibilityDiagnostic
18161843
tx.state.getSymbolAccessibilityDiagnostic = createGetSymbolAccessibilityDiagnosticForNode(node.AsNode())
18171844
t := tx.resolver.CreateTypeOfExpression(
18181845
tx.EmitContext(),
18191846
right,
1820-
host.ValueDeclaration,
1847+
synthesizedModuleDeclaration.AsNode(),
18211848
declarationEmitNodeBuilderFlags,
18221849
declarationEmitInternalNodeBuilderFlags|nodebuilder.InternalFlagsNoSyntacticPrinter,
18231850
tx.tracker,
@@ -1849,33 +1876,16 @@ func (tx *DeclarationTransformer) collectExpandoAssignments(node *ast.BinaryExpr
18491876
)
18501877
}
18511878

1852-
id := ast.GetSymbolId(host)
1853-
if tx.pendingExpando[id] == nil {
1854-
modifierFlags := ast.ModifierFlagsAmbient
1855-
if host.ValueDeclaration.ModifierFlags()&ast.ModifierFlagsExport != 0 && host.ValueDeclaration.ModifierFlags()&ast.ModifierFlagsDefault == 0 {
1856-
modifierFlags |= ast.ModifierFlagsExport
1857-
}
1858-
modifiers := tx.Factory().NewModifierList(ast.CreateModifiersFromModifierFlags(modifierFlags, tx.Factory().NewModifier))
1859-
name := tx.Factory().NewIdentifier(ast.GetFirstIdentifier(left.AsPropertyAccessExpression().Expression).Text())
1860-
tx.pendingExpando[id] = tx.Factory().NewModuleDeclaration(
1861-
modifiers,
1862-
ast.KindNamespaceKeyword,
1863-
name,
1864-
tx.Factory().NewModuleBlock(tx.Factory().NewNodeList(statements)),
1865-
)
1866-
} else {
1867-
namespace := tx.pendingExpando[id].AsModuleDeclaration()
1868-
tx.pendingExpando[id] = tx.Factory().UpdateModuleDeclaration(
1869-
namespace,
1870-
namespace.Modifiers(),
1871-
namespace.Keyword,
1872-
namespace.Name(),
1873-
tx.Factory().UpdateModuleBlock(
1874-
namespace.Body.AsModuleBlock(),
1875-
tx.Factory().NewNodeList(append(namespace.Body.AsModuleBlock().Statements.Nodes, statements...)),
1876-
),
1877-
)
1878-
}
1879+
tx.pendingExpando[id] = tx.Factory().UpdateModuleDeclaration(
1880+
synthesizedModuleDeclaration,
1881+
synthesizedModuleDeclaration.Modifiers(),
1882+
synthesizedModuleDeclaration.Keyword,
1883+
synthesizedModuleDeclaration.Name(),
1884+
tx.Factory().UpdateModuleBlock(
1885+
synthesizedModuleDeclaration.Body.AsModuleBlock(),
1886+
tx.Factory().NewNodeList(append(synthesizedModuleDeclaration.Body.AsModuleBlock().Statements.Nodes, statements...)),
1887+
),
1888+
)
18791889
}
18801890

18811891
func (tx *DeclarationTransformer) transformPendingExpandoAssignments() []*ast.Node {

testdata/baselines/reference/compiler/declarationEmitExpandoFunction.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,9 @@ export declare enum C {
4747
C = 0
4848
}
4949
export declare namespace A {
50-
var a: typeof C;
51-
var b: typeof C;
50+
const a: typeof C;
51+
const b: typeof C;
5252
}
5353
export declare namespace B {
54-
var c: typeof C;
54+
const c: typeof C;
5555
}

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

Lines changed: 5 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -82,94 +82,30 @@ C.B = B;
8282
export declare class Foo {
8383
}
8484
//// [index1.d.ts]
85-
import { Foo } from './foo';
8685
declare function Example(): void;
8786
declare namespace Example {
88-
var Foo: typeof Foo;
87+
const Foo: typeof import("./foo").Foo;
8988
}
9089
export default Example;
9190
//// [index2.d.ts]
9291
import { Foo } from './foo';
9392
export { Foo };
9493
declare function Example(): void;
9594
declare namespace Example {
96-
var Foo: typeof Foo;
95+
const Foo: typeof import("./foo").Foo;
9796
}
9897
export default Example;
9998
//// [index3.d.ts]
10099
export declare class Bar {
101100
}
102101
declare function Example(): void;
103102
declare namespace Example {
104-
var Bar: typeof Bar;
103+
const Bar: typeof import("./index3").Bar;
105104
}
106105
export default Example;
107106
//// [index4.d.ts]
108-
declare function A(): void;
109-
declare function B(): void;
110107
export declare function C(): any;
111108
export declare namespace C {
112-
var A: typeof A;
113-
var B: typeof B;
109+
const A: () => void;
110+
const B: () => void;
114111
}
115-
116-
117-
//// [DtsFileErrors]
118-
119-
120-
index1.d.ts(4,9): error TS2502: 'Foo' is referenced directly or indirectly in its own type annotation.
121-
index2.d.ts(5,9): error TS2502: 'Foo' is referenced directly or indirectly in its own type annotation.
122-
index3.d.ts(5,9): error TS2502: 'Bar' is referenced directly or indirectly in its own type annotation.
123-
index4.d.ts(5,9): error TS2502: 'A' is referenced directly or indirectly in its own type annotation.
124-
index4.d.ts(6,9): error TS2502: 'B' is referenced directly or indirectly in its own type annotation.
125-
126-
127-
==== foo.d.ts (0 errors) ====
128-
export declare class Foo {
129-
}
130-
131-
==== index1.d.ts (1 errors) ====
132-
import { Foo } from './foo';
133-
declare function Example(): void;
134-
declare namespace Example {
135-
var Foo: typeof Foo;
136-
~~~
137-
!!! error TS2502: 'Foo' is referenced directly or indirectly in its own type annotation.
138-
}
139-
export default Example;
140-
141-
==== index2.d.ts (1 errors) ====
142-
import { Foo } from './foo';
143-
export { Foo };
144-
declare function Example(): void;
145-
declare namespace Example {
146-
var Foo: typeof Foo;
147-
~~~
148-
!!! error TS2502: 'Foo' is referenced directly or indirectly in its own type annotation.
149-
}
150-
export default Example;
151-
152-
==== index3.d.ts (1 errors) ====
153-
export declare class Bar {
154-
}
155-
declare function Example(): void;
156-
declare namespace Example {
157-
var Bar: typeof Bar;
158-
~~~
159-
!!! error TS2502: 'Bar' is referenced directly or indirectly in its own type annotation.
160-
}
161-
export default Example;
162-
163-
==== index4.d.ts (2 errors) ====
164-
declare function A(): void;
165-
declare function B(): void;
166-
export declare function C(): any;
167-
export declare namespace C {
168-
var A: typeof A;
169-
~
170-
!!! error TS2502: 'A' is referenced directly or indirectly in its own type annotation.
171-
var B: typeof B;
172-
~
173-
!!! error TS2502: 'B' is referenced directly or indirectly in its own type annotation.
174-
}
175-

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

Lines changed: 8 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -18,24 +18,21 @@
1818
Object.defineProperty(exports, "Foo", { enumerable: true, get: function () { return foo_1.Foo; } });
1919
function Example() { }
2020
Example.Foo = foo_1.Foo;
21-
@@= skipped -31, +31 lines =@@
22-
export declare class Foo {
23-
}
21+
@@= skipped -33, +33 lines =@@
2422
//// [index1.d.ts]
25-
+import { Foo } from './foo';
2623
declare function Example(): void;
2724
declare namespace Example {
2825
- var Foo: typeof import("./foo").Foo;
29-
+ var Foo: typeof Foo;
26+
+ const Foo: typeof import("./foo").Foo;
3027
}
3128
export default Example;
3229
//// [index2.d.ts]
33-
@@= skipped -10, +11 lines =@@
30+
@@= skipped -8, +8 lines =@@
3431
export { Foo };
3532
declare function Example(): void;
3633
declare namespace Example {
3734
- var Foo: typeof import("./foo").Foo;
38-
+ var Foo: typeof Foo;
35+
+ const Foo: typeof import("./foo").Foo;
3936
}
4037
export default Example;
4138
//// [index3.d.ts]
@@ -44,77 +41,14 @@
4441
declare function Example(): void;
4542
declare namespace Example {
4643
- var Bar: typeof import("./index3").Bar;
47-
+ var Bar: typeof Bar;
44+
+ const Bar: typeof import("./index3").Bar;
4845
}
4946
export default Example;
5047
//// [index4.d.ts]
51-
+declare function A(): void;
52-
+declare function B(): void;
5348
export declare function C(): any;
5449
export declare namespace C {
5550
- var A: () => void;
5651
- var B: () => void;
57-
+ var A: typeof A;
58-
+ var B: typeof B;
59-
}
60-
+
61-
+
62-
+//// [DtsFileErrors]
63-
+
64-
+
65-
+index1.d.ts(4,9): error TS2502: 'Foo' is referenced directly or indirectly in its own type annotation.
66-
+index2.d.ts(5,9): error TS2502: 'Foo' is referenced directly or indirectly in its own type annotation.
67-
+index3.d.ts(5,9): error TS2502: 'Bar' is referenced directly or indirectly in its own type annotation.
68-
+index4.d.ts(5,9): error TS2502: 'A' is referenced directly or indirectly in its own type annotation.
69-
+index4.d.ts(6,9): error TS2502: 'B' is referenced directly or indirectly in its own type annotation.
70-
+
71-
+
72-
+==== foo.d.ts (0 errors) ====
73-
+ export declare class Foo {
74-
+ }
75-
+
76-
+==== index1.d.ts (1 errors) ====
77-
+ import { Foo } from './foo';
78-
+ declare function Example(): void;
79-
+ declare namespace Example {
80-
+ var Foo: typeof Foo;
81-
+ ~~~
82-
+!!! error TS2502: 'Foo' is referenced directly or indirectly in its own type annotation.
83-
+ }
84-
+ export default Example;
85-
+
86-
+==== index2.d.ts (1 errors) ====
87-
+ import { Foo } from './foo';
88-
+ export { Foo };
89-
+ declare function Example(): void;
90-
+ declare namespace Example {
91-
+ var Foo: typeof Foo;
92-
+ ~~~
93-
+!!! error TS2502: 'Foo' is referenced directly or indirectly in its own type annotation.
94-
+ }
95-
+ export default Example;
96-
+
97-
+==== index3.d.ts (1 errors) ====
98-
+ export declare class Bar {
99-
+ }
100-
+ declare function Example(): void;
101-
+ declare namespace Example {
102-
+ var Bar: typeof Bar;
103-
+ ~~~
104-
+!!! error TS2502: 'Bar' is referenced directly or indirectly in its own type annotation.
105-
+ }
106-
+ export default Example;
107-
+
108-
+==== index4.d.ts (2 errors) ====
109-
+ declare function A(): void;
110-
+ declare function B(): void;
111-
+ export declare function C(): any;
112-
+ export declare namespace C {
113-
+ var A: typeof A;
114-
+ ~
115-
+!!! error TS2502: 'A' is referenced directly or indirectly in its own type annotation.
116-
+ var B: typeof B;
117-
+ ~
118-
+!!! error TS2502: 'B' is referenced directly or indirectly in its own type annotation.
119-
+ }
120-
+
52+
+ const A: () => void;
53+
+ const B: () => void;
54+
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,5 +32,5 @@ export {};
3232
//// [b.d.ts]
3333
export declare function q(): void;
3434
export declare namespace q {
35-
var val: I;
35+
const val: I;
3636
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,5 +16,5 @@
1616
+//// [b.d.ts]
1717
+export declare function q(): void;
1818
+export declare namespace q {
19-
+ var val: I;
19+
+ const val: I;
2020
+}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ export declare const Point: {
4242
};
4343
export declare const Rect: <p extends Point>(a: p, b: p) => Rect<p>;
4444
declare namespace Point {
45-
var zero: () => Point;
45+
const zero: () => Point;
4646
}
4747

4848

@@ -81,6 +81,6 @@ declarationEmitExpandoWithGenericConstraint.d.ts(14,19): error TS2451: Cannot re
8181
!!! error TS2395: Individual declarations in merged declaration 'Point' must be all exported or all local.
8282
~~~~~
8383
!!! error TS2451: Cannot redeclare block-scoped variable 'Point'.
84-
var zero: () => Point;
84+
const zero: () => Point;
8585
}
8686

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
};
1010
export declare const Rect: <p extends Point>(a: p, b: p) => Rect<p>;
1111
+declare namespace Point {
12-
+ var zero: () => Point;
12+
+ const zero: () => Point;
1313
+}
1414
+
1515
+
@@ -48,6 +48,6 @@
4848
+!!! error TS2395: Individual declarations in merged declaration 'Point' must be all exported or all local.
4949
+ ~~~~~
5050
+!!! error TS2451: Cannot redeclare block-scoped variable 'Point'.
51-
+ var zero: () => Point;
51+
+ const zero: () => Point;
5252
+ }
5353
+

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,5 +21,5 @@ f.x = 2;
2121
declare function f(a: 0): 0;
2222
declare function f(a: 1): 1;
2323
declare namespace f {
24-
var x: 2;
24+
const x: 2;
2525
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,5 @@
55
declare function f(a: 1): 1;
66
declare namespace f {
77
- var x: number;
8-
+ var x: 2;
8+
+ const x: 2;
99
}

0 commit comments

Comments
 (0)