@@ -36,6 +36,32 @@ import { createCache } from '../cache'
36
36
import type TS from 'typescript'
37
37
import { join , extname , dirname } from 'path'
38
38
39
+ /**
40
+ * TypeResolveContext is compatible with ScriptCompileContext
41
+ * but also allows a simpler version of it with minimal required properties
42
+ * when resolveType needs to be used in a non-SFC context, e.g. in a babel
43
+ * plugin. The simplest context can be just:
44
+ * ```ts
45
+ * const ctx: SimpleTypeResolveContext = {
46
+ * filename: '...',
47
+ * source: '...',
48
+ * options: {},
49
+ * error() {},
50
+ * ast: []
51
+ * }
52
+ * ```
53
+ */
54
+ export type SimpleTypeResolveContext = Pick <
55
+ ScriptCompileContext ,
56
+ // required
57
+ 'source' | 'filename' | 'error' | 'options'
58
+ > &
59
+ Partial < Pick < ScriptCompileContext , 'scope' | 'deps' > > & {
60
+ ast : Statement [ ]
61
+ }
62
+
63
+ export type TypeResolveContext = ScriptCompileContext | SimpleTypeResolveContext
64
+
39
65
type Import = Pick < ImportBinding , 'source' | 'imported' >
40
66
41
67
export interface TypeScope {
@@ -79,7 +105,7 @@ interface ResolvedElements {
79
105
* mapped to runtime props or emits.
80
106
*/
81
107
export function resolveTypeElements (
82
- ctx : ScriptCompileContext ,
108
+ ctx : TypeResolveContext ,
83
109
node : Node & WithScope & { _resolvedElements ?: ResolvedElements } ,
84
110
scope ?: TypeScope
85
111
) : ResolvedElements {
@@ -94,7 +120,7 @@ export function resolveTypeElements(
94
120
}
95
121
96
122
function innerResolveTypeElements (
97
- ctx : ScriptCompileContext ,
123
+ ctx : TypeResolveContext ,
98
124
node : Node ,
99
125
scope : TypeScope
100
126
) : ResolvedElements {
@@ -138,19 +164,19 @@ function innerResolveTypeElements(
138
164
) {
139
165
return resolveBuiltin ( ctx , node , typeName as any , scope )
140
166
}
141
- ctx . error (
167
+ return ctx . error (
142
168
`Unresolvable type reference or unsupported built-in utlility type` ,
143
169
node ,
144
170
scope
145
171
)
146
172
}
147
173
}
148
174
}
149
- ctx . error ( `Unresolvable type: ${ node . type } ` , node , scope )
175
+ return ctx . error ( `Unresolvable type: ${ node . type } ` , node , scope )
150
176
}
151
177
152
178
function typeElementsToMap (
153
- ctx : ScriptCompileContext ,
179
+ ctx : TypeResolveContext ,
154
180
elements : TSTypeElement [ ] ,
155
181
scope = ctxToScope ( ctx )
156
182
) : ResolvedElements {
@@ -227,7 +253,7 @@ function createProperty(
227
253
}
228
254
229
255
function resolveInterfaceMembers (
230
- ctx : ScriptCompileContext ,
256
+ ctx : TypeResolveContext ,
231
257
node : TSInterfaceDeclaration & WithScope ,
232
258
scope : TypeScope
233
259
) : ResolvedElements {
@@ -246,7 +272,7 @@ function resolveInterfaceMembers(
246
272
}
247
273
248
274
function resolveMappedType (
249
- ctx : ScriptCompileContext ,
275
+ ctx : TypeResolveContext ,
250
276
node : TSMappedType ,
251
277
scope : TypeScope
252
278
) : ResolvedElements {
@@ -266,7 +292,7 @@ function resolveMappedType(
266
292
}
267
293
268
294
function resolveIndexType (
269
- ctx : ScriptCompileContext ,
295
+ ctx : TypeResolveContext ,
270
296
node : TSIndexedAccessType ,
271
297
scope : TypeScope
272
298
) : ( TSType & WithScope ) [ ] {
@@ -297,7 +323,7 @@ function resolveIndexType(
297
323
}
298
324
299
325
function resolveArrayElementType (
300
- ctx : ScriptCompileContext ,
326
+ ctx : TypeResolveContext ,
301
327
node : Node ,
302
328
scope : TypeScope
303
329
) : TSType [ ] {
@@ -322,11 +348,15 @@ function resolveArrayElementType(
322
348
}
323
349
}
324
350
}
325
- ctx . error ( 'Failed to resolve element type from target type' , node )
351
+ return ctx . error (
352
+ 'Failed to resolve element type from target type' ,
353
+ node ,
354
+ scope
355
+ )
326
356
}
327
357
328
358
function resolveStringType (
329
- ctx : ScriptCompileContext ,
359
+ ctx : TypeResolveContext ,
330
360
node : Node ,
331
361
scope : TypeScope
332
362
) : string [ ] {
@@ -373,11 +403,11 @@ function resolveStringType(
373
403
}
374
404
}
375
405
}
376
- ctx . error ( 'Failed to resolve index type into finite keys' , node , scope )
406
+ return ctx . error ( 'Failed to resolve index type into finite keys' , node , scope )
377
407
}
378
408
379
409
function resolveTemplateKeys (
380
- ctx : ScriptCompileContext ,
410
+ ctx : TypeResolveContext ,
381
411
node : TemplateLiteral ,
382
412
scope : TypeScope
383
413
) : string [ ] {
@@ -420,7 +450,7 @@ const SupportedBuiltinsSet = new Set([
420
450
type GetSetType < T > = T extends Set < infer V > ? V : never
421
451
422
452
function resolveBuiltin (
423
- ctx : ScriptCompileContext ,
453
+ ctx : TypeResolveContext ,
424
454
node : TSTypeReference | TSExpressionWithTypeArguments ,
425
455
name : GetSetType < typeof SupportedBuiltinsSet > ,
426
456
scope : TypeScope
@@ -460,7 +490,7 @@ function resolveBuiltin(
460
490
}
461
491
462
492
function resolveTypeReference (
463
- ctx : ScriptCompileContext ,
493
+ ctx : TypeResolveContext ,
464
494
node : ( TSTypeReference | TSExpressionWithTypeArguments ) & {
465
495
_resolvedReference ?: Node
466
496
} ,
@@ -481,7 +511,7 @@ function resolveTypeReference(
481
511
}
482
512
483
513
function innerResolveTypeReference (
484
- ctx : ScriptCompileContext ,
514
+ ctx : TypeResolveContext ,
485
515
scope : TypeScope ,
486
516
name : string | string [ ] ,
487
517
node : TSTypeReference | TSExpressionWithTypeArguments ,
@@ -536,14 +566,17 @@ function qualifiedNameToPath(node: Identifier | TSQualifiedName): string[] {
536
566
537
567
let ts : typeof TS
538
568
569
+ /**
570
+ * @private
571
+ */
539
572
export function registerTS ( _ts : any ) {
540
573
ts = _ts
541
574
}
542
575
543
576
type FS = NonNullable < SFCScriptCompileOptions [ 'fs' ] >
544
577
545
578
function resolveTypeFromImport (
546
- ctx : ScriptCompileContext ,
579
+ ctx : TypeResolveContext ,
547
580
node : TSTypeReference | TSExpressionWithTypeArguments ,
548
581
name : string ,
549
582
scope : TypeScope
@@ -685,13 +718,16 @@ function resolveWithTS(
685
718
686
719
const fileToScopeCache = createCache < TypeScope > ( )
687
720
721
+ /**
722
+ * @private
723
+ */
688
724
export function invalidateTypeCache ( filename : string ) {
689
725
fileToScopeCache . delete ( filename )
690
726
tsConfigCache . delete ( filename )
691
727
}
692
728
693
729
function fileToScope (
694
- ctx : ScriptCompileContext ,
730
+ ctx : TypeResolveContext ,
695
731
filename : string ,
696
732
fs : FS
697
733
) : TypeScope {
@@ -717,7 +753,7 @@ function fileToScope(
717
753
}
718
754
719
755
function parseFile (
720
- ctx : ScriptCompileContext ,
756
+ ctx : TypeResolveContext ,
721
757
filename : string ,
722
758
content : string
723
759
) : Statement [ ] {
@@ -763,24 +799,30 @@ function parseFile(
763
799
return [ ]
764
800
}
765
801
766
- function ctxToScope ( ctx : ScriptCompileContext ) : TypeScope {
802
+ function ctxToScope ( ctx : TypeResolveContext ) : TypeScope {
767
803
if ( ctx . scope ) {
768
804
return ctx . scope
769
805
}
770
806
807
+ const body =
808
+ 'ast' in ctx
809
+ ? ctx . ast
810
+ : ctx . scriptAst
811
+ ? [ ...ctx . scriptAst . body , ...ctx . scriptSetupAst ! . body ]
812
+ : ctx . scriptSetupAst ! . body
813
+
771
814
const scope : TypeScope = {
772
- filename : ctx . descriptor . filename ,
773
- source : ctx . descriptor . source ,
774
- offset : ctx . startOffset ! ,
775
- imports : Object . create ( ctx . userImports ) ,
815
+ filename : ctx . filename ,
816
+ source : ctx . source ,
817
+ offset : 'startOffset' in ctx ? ctx . startOffset ! : 0 ,
818
+ imports :
819
+ 'userImports' in ctx
820
+ ? Object . create ( ctx . userImports )
821
+ : recordImports ( body ) ,
776
822
types : Object . create ( null ) ,
777
823
exportedTypes : Object . create ( null )
778
824
}
779
825
780
- const body = ctx . scriptAst
781
- ? [ ...ctx . scriptAst . body , ...ctx . scriptSetupAst ! . body ]
782
- : ctx . scriptSetupAst ! . body
783
-
784
826
recordTypes ( body , scope )
785
827
786
828
return ( ctx . scope = scope )
@@ -894,7 +936,7 @@ function recordImport(node: Node, imports: TypeScope['imports']) {
894
936
}
895
937
896
938
export function inferRuntimeType (
897
- ctx : ScriptCompileContext ,
939
+ ctx : TypeResolveContext ,
898
940
node : Node & WithScope ,
899
941
scope = node . _ownerScope || ctxToScope ( ctx )
900
942
) : string [ ] {
@@ -1052,7 +1094,7 @@ export function inferRuntimeType(
1052
1094
}
1053
1095
1054
1096
function flattenTypes (
1055
- ctx : ScriptCompileContext ,
1097
+ ctx : TypeResolveContext ,
1056
1098
types : TSType [ ] ,
1057
1099
scope : TypeScope
1058
1100
) : string [ ] {
0 commit comments