@@ -93,7 +93,8 @@ import {
93
93
PropertyPrototype ,
94
94
IndexSignature ,
95
95
File ,
96
- mangleInternalName
96
+ mangleInternalName ,
97
+ TypeDefinition
97
98
} from "./program" ;
98
99
99
100
import {
@@ -180,7 +181,8 @@ import {
180
181
181
182
findDecorator ,
182
183
isTypeOmitted ,
183
- Source
184
+ Source ,
185
+ TypeDeclaration
184
186
} from "./ast" ;
185
187
186
188
import {
@@ -1156,7 +1158,7 @@ export class Compiler extends DiagnosticEmitter {
1156
1158
1157
1159
// Resolve type if annotated
1158
1160
if ( typeNode ) {
1159
- let resolvedType = this . resolver . resolveType ( typeNode , global . parent ) ; // reports
1161
+ let resolvedType = this . resolver . resolveType ( typeNode , null , global . parent ) ; // reports
1160
1162
if ( ! resolvedType ) {
1161
1163
global . set ( CommonFlags . Errored ) ;
1162
1164
pendingElements . delete ( global ) ;
@@ -2238,13 +2240,7 @@ export class Compiler extends DiagnosticEmitter {
2238
2240
break ;
2239
2241
}
2240
2242
case NodeKind . TypeDeclaration : {
2241
- // TODO: integrate inner type declaration into flow
2242
- this . error (
2243
- DiagnosticCode . Not_implemented_0 ,
2244
- statement . range ,
2245
- "Inner type alias"
2246
- ) ;
2247
- stmt = module . unreachable ( ) ;
2243
+ stmt = this . compileTypeDeclaration ( < TypeDeclaration > statement ) ;
2248
2244
break ;
2249
2245
}
2250
2246
case NodeKind . Module : {
@@ -2305,6 +2301,24 @@ export class Compiler extends DiagnosticEmitter {
2305
2301
return this . module . flatten ( stmts ) ;
2306
2302
}
2307
2303
2304
+ private compileTypeDeclaration ( statement : TypeDeclaration ) : ExpressionRef {
2305
+ let flow = this . currentFlow ;
2306
+ let name = statement . name . text ;
2307
+ let existedTypeAlias = flow . lookupScopedTypeAlias ( name ) ;
2308
+ if ( existedTypeAlias ) {
2309
+ this . errorRelated (
2310
+ DiagnosticCode . Duplicate_identifier_0 ,
2311
+ statement . range ,
2312
+ existedTypeAlias . declaration . range ,
2313
+ name
2314
+ ) ;
2315
+ return this . module . unreachable ( ) ;
2316
+ }
2317
+ let element = new TypeDefinition ( name , flow . sourceFunction , statement , DecoratorFlags . None ) ;
2318
+ flow . addScopedTypeAlias ( name , element ) ;
2319
+ return this . module . nop ( ) ;
2320
+ }
2321
+
2308
2322
private compileBreakStatement (
2309
2323
statement : BreakStatement
2310
2324
) : ExpressionRef {
@@ -2962,7 +2976,7 @@ export class Compiler extends DiagnosticEmitter {
2962
2976
let initializerNode = declaration . initializer ;
2963
2977
if ( typeNode ) {
2964
2978
type = resolver . resolveType ( // reports
2965
- typeNode ,
2979
+ typeNode , flow ,
2966
2980
flow . sourceFunction ,
2967
2981
cloneMap ( flow . contextualTypeArguments )
2968
2982
) ;
@@ -3729,7 +3743,7 @@ export class Compiler extends DiagnosticEmitter {
3729
3743
case AssertionKind . As : {
3730
3744
let flow = this . currentFlow ;
3731
3745
let toType = this . resolver . resolveType ( // reports
3732
- assert ( expression . toType ) ,
3746
+ assert ( expression . toType ) , flow ,
3733
3747
flow . sourceFunction ,
3734
3748
cloneMap ( flow . contextualTypeArguments )
3735
3749
) ;
@@ -6162,6 +6176,7 @@ export class Compiler extends DiagnosticEmitter {
6162
6176
typeArguments = this . resolver . resolveTypeArguments (
6163
6177
assert ( typeParameterNodes ) ,
6164
6178
typeArgumentNodes ,
6179
+ this . currentFlow ,
6165
6180
this . currentFlow . sourceFunction . parent ,
6166
6181
cloneMap ( this . currentFlow . contextualTypeArguments ) , // don't update
6167
6182
expression
@@ -7085,7 +7100,7 @@ export class Compiler extends DiagnosticEmitter {
7085
7100
let parameterNode = parameterNodes [ i ] ;
7086
7101
if ( ! isTypeOmitted ( parameterNode . type ) ) {
7087
7102
let resolvedType = this . resolver . resolveType (
7088
- parameterNode . type ,
7103
+ parameterNode . type , flow ,
7089
7104
sourceFunction . parent ,
7090
7105
contextualTypeArguments
7091
7106
) ;
@@ -7105,7 +7120,7 @@ export class Compiler extends DiagnosticEmitter {
7105
7120
let returnType = contextualSignature . returnType ;
7106
7121
if ( ! isTypeOmitted ( signatureNode . returnType ) ) {
7107
7122
let resolvedType = this . resolver . resolveType (
7108
- signatureNode . returnType ,
7123
+ signatureNode . returnType , flow ,
7109
7124
sourceFunction . parent ,
7110
7125
contextualTypeArguments
7111
7126
) ;
@@ -7135,7 +7150,7 @@ export class Compiler extends DiagnosticEmitter {
7135
7150
return module . unreachable ( ) ;
7136
7151
}
7137
7152
let resolvedType = this . resolver . resolveType (
7138
- thisTypeNode ,
7153
+ thisTypeNode , flow ,
7139
7154
sourceFunction . parent ,
7140
7155
contextualTypeArguments
7141
7156
) ;
@@ -7522,7 +7537,7 @@ export class Compiler extends DiagnosticEmitter {
7522
7537
if ( isType . kind == NodeKind . NamedType ) {
7523
7538
let namedType = < NamedTypeNode > isType ;
7524
7539
if ( ! ( namedType . isNullable || namedType . hasTypeArguments ) ) {
7525
- let element = this . resolver . resolveTypeName ( namedType . name , flow . sourceFunction , ReportMode . Swallow ) ;
7540
+ let element = this . resolver . resolveTypeName ( namedType . name , flow , flow . sourceFunction , ReportMode . Swallow ) ;
7526
7541
if ( element && element . kind == ElementKind . ClassPrototype ) {
7527
7542
let prototype = < ClassPrototype > element ;
7528
7543
if ( prototype . is ( CommonFlags . Generic ) ) {
@@ -7534,7 +7549,7 @@ export class Compiler extends DiagnosticEmitter {
7534
7549
7535
7550
// Fall back to `instanceof TYPE`
7536
7551
let expectedType = this . resolver . resolveType (
7537
- expression . isType ,
7552
+ expression . isType , flow ,
7538
7553
flow . sourceFunction ,
7539
7554
cloneMap ( flow . contextualTypeArguments )
7540
7555
) ;
@@ -8686,7 +8701,7 @@ export class Compiler extends DiagnosticEmitter {
8686
8701
let flow = this . currentFlow ;
8687
8702
8688
8703
// obtain the class being instantiated
8689
- let target = this . resolver . resolveTypeName ( expression . typeName , flow . sourceFunction ) ;
8704
+ let target = this . resolver . resolveTypeName ( expression . typeName , flow , flow . sourceFunction ) ;
8690
8705
if ( ! target ) return module . unreachable ( ) ;
8691
8706
if ( target . kind != ElementKind . ClassPrototype ) {
8692
8707
this . error (
@@ -8722,6 +8737,7 @@ export class Compiler extends DiagnosticEmitter {
8722
8737
classInstance = this . resolver . resolveClassInclTypeArguments (
8723
8738
classPrototype ,
8724
8739
typeArguments ,
8740
+ flow ,
8725
8741
flow . sourceFunction . parent , // relative to caller
8726
8742
cloneMap ( flow . contextualTypeArguments ) ,
8727
8743
expression
0 commit comments