4
4
"fmt"
5
5
6
6
"github.com/microsoft/typescript-go/internal/ast"
7
+ "github.com/microsoft/typescript-go/internal/collections"
7
8
"github.com/microsoft/typescript-go/internal/core"
8
9
"github.com/microsoft/typescript-go/internal/debug"
9
10
"github.com/microsoft/typescript-go/internal/diagnostics"
@@ -58,13 +59,14 @@ type DeclarationTransformer struct {
58
59
declarationMapPath string
59
60
60
61
isBundledEmit bool
61
- declareModifier DeclareModifier
62
+ needsDeclare bool
62
63
needsScopeFixMarker bool
63
64
resultHasScopeMarker bool
64
65
enclosingDeclaration * ast.Node
65
66
resultHasExternalModuleIndicator bool
66
67
suppressNewDiagnosticContexts bool
67
68
lateStatementReplacementMap map [ast.NodeId ]* ast.Node
69
+ expandoHosts collections.Set [ast.NodeId ]
68
70
rawReferencedFiles []ReferencedFilePair
69
71
rawTypeReferenceDirectives []* ast.FileReference
70
72
rawLibReferenceDirectives []* ast.FileReference
@@ -167,7 +169,7 @@ func (tx *DeclarationTransformer) visitSourceFile(node *ast.SourceFile) *ast.Nod
167
169
}
168
170
169
171
tx .isBundledEmit = false
170
- tx .declareModifier = DeclareModifierEligible
172
+ tx .needsDeclare = true
171
173
tx .needsScopeFixMarker = false
172
174
tx .resultHasScopeMarker = false
173
175
tx .enclosingDeclaration = node .AsNode ()
@@ -176,6 +178,7 @@ func (tx *DeclarationTransformer) visitSourceFile(node *ast.SourceFile) *ast.Nod
176
178
tx .suppressNewDiagnosticContexts = false
177
179
tx .state .lateMarkedStatements = make ([]* ast.Node , 0 )
178
180
tx .lateStatementReplacementMap = make (map [ast.NodeId ]* ast.Node )
181
+ tx .expandoHosts = collections.Set [ast.NodeId ]{}
179
182
tx .rawReferencedFiles = make ([]ReferencedFilePair , 0 )
180
183
tx .rawTypeReferenceDirectives = make ([]* ast.FileReference , 0 )
181
184
tx .rawLibReferenceDirectives = make ([]* ast.FileReference , 0 )
@@ -241,16 +244,12 @@ func (tx *DeclarationTransformer) transformAndReplaceLatePaintedStatements(state
241
244
next := tx .state .lateMarkedStatements [0 ]
242
245
tx .state .lateMarkedStatements = tx .state .lateMarkedStatements [1 :]
243
246
244
- saveDeclareModifier := tx .declareModifier
245
- tx .declareModifier = core .IfElse (
246
- next .Parent != nil && ast .IsSourceFile (next .Parent ) && ! (ast .IsExternalModule (next .Parent .AsSourceFile ()) && tx .isBundledEmit ),
247
- DeclareModifierEligible ,
248
- DeclareModifierNone ,
249
- )
247
+ saveNeedsDeclare := tx .needsDeclare
248
+ tx .needsDeclare = next .Parent != nil && ast .IsSourceFile (next .Parent ) && ! (ast .IsExternalModule (next .Parent .AsSourceFile ()) && tx .isBundledEmit )
250
249
251
250
result := tx .transformTopLevelDeclaration (next )
252
251
253
- tx .declareModifier = saveDeclareModifier
252
+ tx .needsDeclare = saveNeedsDeclare
254
253
original := tx .EmitContext ().MostOriginal (next )
255
254
id := ast .GetNodeId (original )
256
255
tx .lateStatementReplacementMap [id ] = result
@@ -954,7 +953,7 @@ func (tx *DeclarationTransformer) visitDeclarationStatements(input *ast.Node) *a
954
953
varDecl := tx .Factory ().NewVariableDeclaration (newId , nil , type_ , nil )
955
954
tx .tracker .PopErrorFallbackNode ()
956
955
var modList * ast.ModifierList
957
- if tx .declareModifier == DeclareModifierEligible {
956
+ if tx .needsDeclare {
958
957
modList = tx .Factory ().NewModifierList ([]* ast.Node {tx .Factory ().NewModifier (ast .KindDeclareKeyword )})
959
958
} else {
960
959
modList = tx .Factory ().NewModifierList ([]* ast.Node {})
@@ -1112,7 +1111,7 @@ func (tx *DeclarationTransformer) transformTopLevelDeclaration(input *ast.Node)
1112
1111
if canProdiceDiagnostic {
1113
1112
tx .state .getSymbolAccessibilityDiagnostic = createGetSymbolAccessibilityDiagnosticForNode (input )
1114
1113
}
1115
- saveDeclareModifier := tx .declareModifier
1114
+ saveNeedsDeclare := tx .needsDeclare
1116
1115
1117
1116
var result * ast.Node
1118
1117
switch input .Kind {
@@ -1137,13 +1136,13 @@ func (tx *DeclarationTransformer) transformTopLevelDeclaration(input *ast.Node)
1137
1136
1138
1137
tx .enclosingDeclaration = previousEnclosingDeclaration
1139
1138
tx .state .getSymbolAccessibilityDiagnostic = oldDiag
1140
- tx .declareModifier = saveDeclareModifier
1139
+ tx .needsDeclare = saveNeedsDeclare
1141
1140
tx .state .errorNameNode = oldName
1142
1141
return result
1143
1142
}
1144
1143
1145
1144
func (tx * DeclarationTransformer ) transformTypeAliasDeclaration (input * ast.TypeAliasDeclaration ) * ast.Node {
1146
- tx .declareModifier = DeclareModifierNone
1145
+ tx .needsDeclare = false
1147
1146
return tx .Factory ().UpdateTypeAliasDeclaration (
1148
1147
input ,
1149
1148
tx .ensureModifiers (input .AsNode ()),
@@ -1183,8 +1182,8 @@ func (tx *DeclarationTransformer) transformModuleDeclaration(input *ast.ModuleDe
1183
1182
// It'd be good to collapse those back in the declaration output, but the AST can't represent the
1184
1183
// `namespace a.b.c` shape for the printer (without using invalid identifier names).
1185
1184
mods := tx .ensureModifiers (input .AsNode ())
1186
- saveDeclareModifier := tx .declareModifier
1187
- tx .declareModifier = DeclareModifierNone
1185
+ saveNeedsDeclare := tx .needsDeclare
1186
+ tx .needsDeclare = false
1188
1187
inner := input .Body
1189
1188
keyword := input .Keyword
1190
1189
if keyword != ast .KindGlobalKeyword && (input .Name () == nil || ! ast .IsStringLiteral (input .Name ())) {
@@ -1214,7 +1213,7 @@ func (tx *DeclarationTransformer) transformModuleDeclaration(input *ast.ModuleDe
1214
1213
}
1215
1214
1216
1215
body := tx .Factory ().UpdateModuleBlock (inner .AsModuleBlock (), lateStatements )
1217
- tx .declareModifier = saveDeclareModifier
1216
+ tx .needsDeclare = saveNeedsDeclare
1218
1217
tx .needsScopeFixMarker = oldNeedsScopeFix
1219
1218
tx .resultHasScopeMarker = oldHasScopeFix
1220
1219
@@ -1363,7 +1362,7 @@ func (tx *DeclarationTransformer) transformClassDeclaration(input *ast.ClassDecl
1363
1362
nil ,
1364
1363
)
1365
1364
var mods * ast.ModifierList
1366
- if tx .declareModifier == DeclareModifierEligible {
1365
+ if tx .needsDeclare {
1367
1366
mods = tx .Factory ().NewModifierList ([]* ast.Node {tx .Factory ().NewModifier (ast .KindDeclareKeyword )})
1368
1367
}
1369
1368
statement := tx .Factory ().NewVariableStatement (
@@ -1508,13 +1507,8 @@ func (tx *DeclarationTransformer) ensureModifiers(node *ast.Node) *ast.ModifierL
1508
1507
func (tx * DeclarationTransformer ) ensureModifierFlags (node * ast.Node ) ast.ModifierFlags {
1509
1508
mask := ast .ModifierFlagsAll ^ (ast .ModifierFlagsPublic | ast .ModifierFlagsAsync | ast .ModifierFlagsOverride ) // No async and override modifiers in declaration files
1510
1509
additions := ast .ModifierFlagsNone
1511
- if tx .declareModifier == DeclareModifierEligible && ! isAlwaysType (node ) {
1512
- additions = ast .ModifierFlagsAmbient
1513
- }
1514
- if tx .declareModifier == DeclareModifierEnforced {
1510
+ if tx .needsDeclare && ! isAlwaysType (node ) {
1515
1511
additions = ast .ModifierFlagsAmbient
1516
- mask ^= ast .ModifierFlagsDefault
1517
- mask ^= ast .ModifierFlagsExport
1518
1512
}
1519
1513
parentIsFile := node .Parent .Kind == ast .KindSourceFile
1520
1514
if ! parentIsFile || (tx .isBundledEmit && parentIsFile && ast .IsExternalModule (node .Parent .AsSourceFile ())) {
@@ -1901,38 +1895,90 @@ func (tx *DeclarationTransformer) transformExpandoAssignment(node *ast.BinaryExp
1901
1895
statements = append (statements , tx .Factory ().NewExportDeclaration (nil /*modifiers*/ , false /*isTypeOnly*/ , namedExports , nil /*moduleSpecifier*/ , nil /*attributes*/ ))
1902
1896
}
1903
1897
1898
+ tx .transformExpandoHost (name , declaration )
1899
+
1900
+ flags := tx .host .GetEffectiveDeclarationFlags (tx .EmitContext ().ParseNode (declaration ), ast .ModifierFlagsAll )
1904
1901
modifierFlags := ast .ModifierFlagsAmbient
1905
- replacement := make ([]* ast.Node , 0 )
1906
1902
1907
- flags := ast .GetCombinedModifierFlags (declaration )
1908
1903
if flags & ast .ModifierFlagsExport != 0 {
1909
- if flags & ast .ModifierFlagsDefault != 0 {
1910
- if n := tx .transformExpandoHost (name , declaration ); n != nil {
1911
- replacement = append (replacement , n )
1912
- }
1913
- } else {
1904
+ if flags & ast .ModifierFlagsDefault == 0 {
1914
1905
modifierFlags |= ast .ModifierFlagsExport
1915
1906
}
1916
1907
tx .resultHasScopeMarker = true
1917
1908
tx .resultHasExternalModuleIndicator = true
1918
1909
}
1919
1910
1911
+ return tx .Factory ().NewModuleDeclaration (tx .Factory ().NewModifierList (ast .CreateModifiersFromModifierFlags (modifierFlags , tx .Factory ().NewModifier )), ast .KindNamespaceKeyword , name , tx .Factory ().NewModuleBlock (tx .Factory ().NewNodeList (statements )))
1912
+ }
1913
+
1914
+ func (tx * DeclarationTransformer ) transformExpandoHost (name * ast.Node , declaration * ast.Declaration ) {
1915
+ root := core .IfElse (ast .IsVariableDeclaration (declaration ), declaration .Parent .Parent , declaration )
1916
+ id := ast .GetNodeId (tx .EmitContext ().MostOriginal (root ))
1917
+
1918
+ if tx .expandoHosts .Has (id ) {
1919
+ return
1920
+ }
1921
+
1922
+ saveNeedsDeclare := tx .needsDeclare
1923
+ tx .needsDeclare = true
1924
+
1925
+ modifierFlags := tx .ensureModifierFlags (root )
1926
+ defaultExport := modifierFlags & ast .ModifierFlagsExport != 0 && modifierFlags & ast .ModifierFlagsDefault != 0
1927
+
1928
+ tx .needsDeclare = saveNeedsDeclare
1929
+
1930
+ if defaultExport {
1931
+ modifierFlags |= ast .ModifierFlagsAmbient
1932
+ modifierFlags ^= ast .ModifierFlagsDefault
1933
+ modifierFlags ^= ast .ModifierFlagsExport
1934
+ }
1935
+
1920
1936
modifiers := tx .Factory ().NewModifierList (ast .CreateModifiersFromModifierFlags (modifierFlags , tx .Factory ().NewModifier ))
1921
- namespace := tx . Factory (). NewModuleDeclaration ( modifiers , ast .KindNamespaceKeyword , name , tx . Factory (). NewModuleBlock ( tx . Factory (). NewNodeList ( statements )) )
1937
+ replacement := make ([] * ast.Node , 0 )
1922
1938
1923
- replacement = append (replacement , namespace )
1924
- return tx .Factory ().NewSyntaxList (replacement )
1925
- }
1939
+ var typeParameters * ast.TypeParameterList
1940
+ var parameters * ast.ParameterList
1941
+ var returnType * ast.Node
1942
+ var asteriskToken * ast.TokenNode
1943
+
1944
+ if ast .IsFunctionDeclaration (declaration ) {
1945
+ fn := declaration .AsFunctionDeclaration ()
1946
+ typeParameters = tx .ensureTypeParams (fn .AsNode (), fn .TypeParameters )
1947
+ parameters = tx .updateParamList (fn .AsNode (), fn .Parameters )
1948
+ returnType = tx .ensureType (fn .AsNode (), false )
1949
+ asteriskToken = fn .AsteriskToken
1950
+ } else if ast .IsVariableDeclaration (declaration ) && ast .IsFunctionExpressionOrArrowFunction (declaration .Initializer ()) {
1951
+ if ast .IsFunctionExpression (declaration .Initializer ()) {
1952
+ fn := declaration .Initializer ().AsFunctionExpression ()
1953
+ typeParameters = tx .ensureTypeParams (fn .AsNode (), fn .TypeParameters )
1954
+ parameters = tx .updateParamList (fn .AsNode (), fn .Parameters )
1955
+ returnType = tx .ensureType (fn .AsNode (), false )
1956
+ asteriskToken = fn .AsteriskToken
1957
+ } else if ast .IsArrowFunction (declaration .Initializer ()) {
1958
+ fn := declaration .Initializer ().AsArrowFunction ()
1959
+ typeParameters = tx .ensureTypeParams (fn .AsNode (), fn .TypeParameters )
1960
+ parameters = tx .updateParamList (fn .AsNode (), fn .Parameters )
1961
+ returnType = tx .ensureType (fn .AsNode (), false )
1962
+ asteriskToken = fn .AsteriskToken
1963
+ } else {
1964
+ return
1965
+ }
1966
+ } else {
1967
+ return
1968
+ }
1926
1969
1927
- func ( tx * DeclarationTransformer ) transformExpandoHost ( name * ast.Node , node * ast. Node ) * ast. Node {
1928
- id := ast . GetNodeId ( tx .EmitContext ().MostOriginal ( node ))
1929
- if tx . lateStatementReplacementMap [ id ] == nil || ast .HasModifier ( tx . lateStatementReplacementMap [ id ], ast . ModifierFlagsDefault ) {
1930
- saveDeclareModifier := tx .declareModifier
1931
- tx . declareModifier = DeclareModifierEnforced
1932
- tx . lateStatementReplacementMap [ id ] = tx . transformTopLevelDeclaration ( node )
1933
- tx . declareModifier = saveDeclareModifier
1970
+ if ast .IsFunctionDeclaration ( declaration ) {
1971
+ replacement = append ( replacement , tx .Factory ().UpdateFunctionDeclaration ( root . AsFunctionDeclaration (), modifiers , asteriskToken , root . Name (), typeParameters , parameters , returnType , nil /*fullSignature*/ , nil /*body*/ ))
1972
+ } else if ast .IsVariableDeclaration ( declaration ) {
1973
+ replacement = append ( replacement , tx .Factory (). NewFunctionDeclaration ( modifiers , asteriskToken , tx . Factory (). NewIdentifier ( name . Text ()), typeParameters , parameters , returnType , nil /*fullSignature*/ , nil /*body*/ ))
1974
+ } else {
1975
+ return
1976
+ }
1934
1977
1935
- return tx .Factory ().NewExportAssignment (nil /*modifiers*/ , false /*isExportEquals*/ , nil /*typeNode*/ , name )
1978
+ if defaultExport {
1979
+ replacement = append (replacement , tx .Factory ().NewExportAssignment (nil /*modifiers*/ , false /*isExportEquals*/ , nil /*typeNode*/ , name ))
1936
1980
}
1937
- return nil
1981
+
1982
+ tx .expandoHosts .Add (id )
1983
+ tx .lateStatementReplacementMap [id ] = tx .Factory ().NewSyntaxList (replacement )
1938
1984
}
0 commit comments