@@ -136,6 +136,12 @@ export interface ImportBinding {
136
136
isUsedInTemplate : boolean
137
137
}
138
138
139
+ type FromNormalScript < T > = T & { __fromNormalScript ?: boolean | null }
140
+ type PropsDeclType = FromNormalScript < TSTypeLiteral | TSInterfaceBody >
141
+ type EmitsDeclType = FromNormalScript <
142
+ TSFunctionType | TSTypeLiteral | TSInterfaceBody
143
+ >
144
+
139
145
/**
140
146
* Compile `<script setup>`
141
147
* It requires the whole SFC descriptor because we need to handle and merge
@@ -287,15 +293,11 @@ export function compileScript(
287
293
let propsRuntimeDefaults : ObjectExpression | undefined
288
294
let propsDestructureDecl : Node | undefined
289
295
let propsDestructureRestId : string | undefined
290
- let propsTypeDecl : TSTypeLiteral | TSInterfaceBody | undefined
296
+ let propsTypeDecl : PropsDeclType | undefined
291
297
let propsTypeDeclRaw : Node | undefined
292
298
let propsIdentifier : string | undefined
293
299
let emitsRuntimeDecl : Node | undefined
294
- let emitsTypeDecl :
295
- | TSFunctionType
296
- | TSTypeLiteral
297
- | TSInterfaceBody
298
- | undefined
300
+ let emitsTypeDecl : EmitsDeclType | undefined
299
301
let emitsTypeDeclRaw : Node | undefined
300
302
let emitIdentifier : string | undefined
301
303
let hasAwait = false
@@ -436,7 +438,7 @@ export function compileScript(
436
438
propsTypeDecl = resolveQualifiedType (
437
439
propsTypeDeclRaw ,
438
440
node => node . type === 'TSTypeLiteral'
439
- ) as TSTypeLiteral | TSInterfaceBody | undefined
441
+ ) as PropsDeclType | undefined
440
442
441
443
if ( ! propsTypeDecl ) {
442
444
error (
@@ -567,7 +569,7 @@ export function compileScript(
567
569
emitsTypeDecl = resolveQualifiedType (
568
570
emitsTypeDeclRaw ,
569
571
node => node . type === 'TSFunctionType' || node . type === 'TSTypeLiteral'
570
- ) as TSFunctionType | TSTypeLiteral | TSInterfaceBody | undefined
572
+ ) as EmitsDeclType | undefined
571
573
572
574
if ( ! emitsTypeDecl ) {
573
575
error (
@@ -667,7 +669,7 @@ export function compileScript(
667
669
function resolveQualifiedType (
668
670
node : Node ,
669
671
qualifier : ( node : Node ) => boolean
670
- ) {
672
+ ) : Node | undefined {
671
673
if ( qualifier ( node ) ) {
672
674
return node
673
675
}
@@ -677,7 +679,8 @@ export function compileScript(
677
679
) {
678
680
const refName = node . typeName . name
679
681
const body = getAstBody ( )
680
- for ( const node of body ) {
682
+ for ( let i = 0 ; i < body . length ; i ++ ) {
683
+ const node = body [ i ]
681
684
let qualified = isQualifiedType (
682
685
node ,
683
686
qualifier ,
@@ -690,6 +693,8 @@ export function compileScript(
690
693
filterExtendsType ( extendsTypes , bodies )
691
694
qualified . body = bodies
692
695
}
696
+ ; ( qualified as FromNormalScript < Node > ) . __fromNormalScript =
697
+ scriptAst && i >= scriptSetupAst . body . length
693
698
return qualified
694
699
}
695
700
}
@@ -877,8 +882,10 @@ export function compileScript(
877
882
}
878
883
}
879
884
880
- function genSetupPropsType ( node : TSTypeLiteral | TSInterfaceBody ) {
881
- const scriptSetupSource = scriptSetup ! . content
885
+ function genSetupPropsType ( node : PropsDeclType ) {
886
+ const scriptSource = node . __fromNormalScript
887
+ ? script ! . content
888
+ : scriptSetup ! . content
882
889
if ( hasStaticWithDefaults ( ) ) {
883
890
// if withDefaults() is used, we need to remove the optional flags
884
891
// on props that have default values
@@ -903,20 +910,19 @@ export function compileScript(
903
910
res +=
904
911
m . key . name +
905
912
( m . type === 'TSMethodSignature' ? '()' : '' ) +
906
- scriptSetupSource . slice (
913
+ scriptSource . slice (
907
914
m . typeAnnotation . start ! ,
908
915
m . typeAnnotation . end !
909
916
) +
910
917
', '
911
918
} else {
912
- res +=
913
- scriptSetupSource . slice ( m . start ! , m . typeAnnotation . end ! ) + `, `
919
+ res += scriptSource . slice ( m . start ! , m . typeAnnotation . end ! ) + `, `
914
920
}
915
921
}
916
922
}
917
923
return ( res . length ? res . slice ( 0 , - 2 ) : res ) + ` }`
918
924
} else {
919
- return scriptSetupSource . slice ( node . start ! , node . end ! )
925
+ return scriptSource . slice ( node . start ! , node . end ! )
920
926
}
921
927
}
922
928
@@ -1480,7 +1486,10 @@ export function compileScript(
1480
1486
if ( destructureElements . length ) {
1481
1487
args += `, { ${ destructureElements . join ( ', ' ) } }`
1482
1488
if ( emitsTypeDecl ) {
1483
- args += `: { emit: (${ scriptSetup . content . slice (
1489
+ const content = emitsTypeDecl . __fromNormalScript
1490
+ ? script ! . content
1491
+ : scriptSetup . content
1492
+ args += `: { emit: (${ content . slice (
1484
1493
emitsTypeDecl . start ! ,
1485
1494
emitsTypeDecl . end !
1486
1495
) } ), expose: any, slots: any, attrs: any }`
0 commit comments