@@ -97,28 +97,43 @@ public Label lookupNamespace(String name) {
97
97
}
98
98
}
99
99
100
+ public static enum FileKind {
101
+ /** Any file not specific to one of the other file kinds. */
102
+ PLAIN ,
103
+
104
+ /** A file potentially containing template tags. */
105
+ TEMPLATE ,
106
+
107
+ /** A d.ts file, containing TypeScript ambient declarations. */
108
+ TYPESCRIPT_DECLARATION ,
109
+ }
110
+
100
111
private final TrapWriter trapWriter ;
101
112
private Scope curScope ;
102
113
private final Scope toplevelScope ;
103
114
private final ECMAVersion ecmaVersion ;
104
115
private final Set <String > implicitGlobals = new LinkedHashSet <String >();
105
116
private Scope implicitVariableScope ;
106
- private final boolean isInTemplateScope ;
117
+ private final FileKind fileKind ;
107
118
108
- public ScopeManager (TrapWriter trapWriter , ECMAVersion ecmaVersion , boolean isInTemplateScope ) {
119
+ public ScopeManager (TrapWriter trapWriter , ECMAVersion ecmaVersion , FileKind fileKind ) {
109
120
this .trapWriter = trapWriter ;
110
121
this .toplevelScope = enterScope (ScopeKind .GLOBAL , trapWriter .globalID ("global_scope" ), null );
111
122
this .ecmaVersion = ecmaVersion ;
112
- this .implicitVariableScope = toplevelScope ;
113
- this .isInTemplateScope = isInTemplateScope ;
123
+ this .implicitVariableScope = toplevelScope ;
124
+ this .fileKind = fileKind ;
114
125
}
115
126
116
127
/**
117
128
* Returns true the current scope is potentially in a template file, and may contain
118
129
* relevant template tags.
119
130
*/
120
131
public boolean isInTemplateFile () {
121
- return isInTemplateScope ;
132
+ return this .fileKind == FileKind .TEMPLATE ;
133
+ }
134
+
135
+ public boolean isInTypeScriptDeclarationFile () {
136
+ return this .fileKind == FileKind .TYPESCRIPT_DECLARATION ;
122
137
}
123
138
124
139
/**
@@ -221,7 +236,7 @@ public Scope getToplevelScope() {
221
236
222
237
/**
223
238
* Get the label for a given variable in the current scope; if it cannot be found, add it to the
224
- * implicit variable scope (usually the global scope).
239
+ * implicit variable scope (usually the global scope).
225
240
*/
226
241
public Label getVarKey (String name ) {
227
242
Label lbl = curScope .lookupVariable (name );
@@ -411,15 +426,15 @@ public Void visit(Identifier nd, Void v) {
411
426
// cases where we turn on the 'declKind' flags
412
427
@ Override
413
428
public Void visit (FunctionDeclaration nd , Void v ) {
414
- if (nd .hasDeclareKeyword ()) return null ;
429
+ if (nd .hasDeclareKeyword () && ! isInTypeScriptDeclarationFile () ) return null ;
415
430
// strict mode functions are block-scoped, non-strict mode ones aren't
416
431
if (blockscope == isStrict ) visit (nd .getId (), DeclKind .var );
417
432
return null ;
418
433
}
419
434
420
435
@ Override
421
436
public Void visit (ClassDeclaration nd , Void c ) {
422
- if (nd .hasDeclareKeyword ()) return null ;
437
+ if (nd .hasDeclareKeyword () && ! isInTypeScriptDeclarationFile () ) return null ;
423
438
if (blockscope ) visit (nd .getClassDef ().getId (), DeclKind .varAndType );
424
439
return null ;
425
440
}
@@ -468,7 +483,7 @@ public Void visit(EnhancedForStatement nd, Void v) {
468
483
469
484
@ Override
470
485
public Void visit (VariableDeclaration nd , Void v ) {
471
- if (nd .hasDeclareKeyword ()) return null ;
486
+ if (nd .hasDeclareKeyword () && ! isInTypeScriptDeclarationFile () ) return null ;
472
487
// in block scoping mode, only process 'let'; in non-block scoping
473
488
// mode, only process non-'let'
474
489
if (blockscope == nd .isBlockScoped (ecmaVersion )) visit (nd .getDeclarations ());
@@ -503,7 +518,8 @@ public Void visit(ClassBody nd, Void c) {
503
518
@ Override
504
519
public Void visit (NamespaceDeclaration nd , Void c ) {
505
520
if (blockscope ) return null ;
506
- boolean hasVariable = nd .isInstantiated () && !nd .hasDeclareKeyword ();
521
+ boolean isAmbientOutsideDtsFile = nd .hasDeclareKeyword () && !isInTypeScriptDeclarationFile ();
522
+ boolean hasVariable = nd .isInstantiated () && !isAmbientOutsideDtsFile ;
507
523
visit (nd .getName (), hasVariable ? DeclKind .varAndNamespace : DeclKind .namespace );
508
524
return null ;
509
525
}
0 commit comments