@@ -136,6 +136,12 @@ export interface ImportBinding {
136136 isUsedInTemplate : boolean
137137}
138138
139+ type FromNormalScript < T > = T & { __fromNormalScript ?: boolean | null }
140+ type PropsDeclType = FromNormalScript < TSTypeLiteral | TSInterfaceBody >
141+ type EmitsDeclType = FromNormalScript <
142+ TSFunctionType | TSTypeLiteral | TSInterfaceBody
143+ >
144+
139145/**
140146 * Compile `<script setup>`
141147 * It requires the whole SFC descriptor because we need to handle and merge
@@ -287,15 +293,11 @@ export function compileScript(
287293 let propsRuntimeDefaults : ObjectExpression | undefined
288294 let propsDestructureDecl : Node | undefined
289295 let propsDestructureRestId : string | undefined
290- let propsTypeDecl : TSTypeLiteral | TSInterfaceBody | undefined
296+ let propsTypeDecl : PropsDeclType | undefined
291297 let propsTypeDeclRaw : Node | undefined
292298 let propsIdentifier : string | undefined
293299 let emitsRuntimeDecl : Node | undefined
294- let emitsTypeDecl :
295- | TSFunctionType
296- | TSTypeLiteral
297- | TSInterfaceBody
298- | undefined
300+ let emitsTypeDecl : EmitsDeclType | undefined
299301 let emitsTypeDeclRaw : Node | undefined
300302 let emitIdentifier : string | undefined
301303 let hasAwait = false
@@ -436,7 +438,7 @@ export function compileScript(
436438 propsTypeDecl = resolveQualifiedType (
437439 propsTypeDeclRaw ,
438440 node => node . type === 'TSTypeLiteral'
439- ) as TSTypeLiteral | TSInterfaceBody | undefined
441+ ) as PropsDeclType | undefined
440442
441443 if ( ! propsTypeDecl ) {
442444 error (
@@ -567,7 +569,7 @@ export function compileScript(
567569 emitsTypeDecl = resolveQualifiedType (
568570 emitsTypeDeclRaw ,
569571 node => node . type === 'TSFunctionType' || node . type === 'TSTypeLiteral'
570- ) as TSFunctionType | TSTypeLiteral | TSInterfaceBody | undefined
572+ ) as EmitsDeclType | undefined
571573
572574 if ( ! emitsTypeDecl ) {
573575 error (
@@ -667,7 +669,7 @@ export function compileScript(
667669 function resolveQualifiedType (
668670 node : Node ,
669671 qualifier : ( node : Node ) => boolean
670- ) {
672+ ) : Node | undefined {
671673 if ( qualifier ( node ) ) {
672674 return node
673675 }
@@ -677,7 +679,8 @@ export function compileScript(
677679 ) {
678680 const refName = node . typeName . name
679681 const body = getAstBody ( )
680- for ( const node of body ) {
682+ for ( let i = 0 ; i < body . length ; i ++ ) {
683+ const node = body [ i ]
681684 let qualified = isQualifiedType (
682685 node ,
683686 qualifier ,
@@ -690,6 +693,8 @@ export function compileScript(
690693 filterExtendsType ( extendsTypes , bodies )
691694 qualified . body = bodies
692695 }
696+ ; ( qualified as FromNormalScript < Node > ) . __fromNormalScript =
697+ scriptAst && i >= scriptSetupAst . body . length
693698 return qualified
694699 }
695700 }
@@ -877,8 +882,10 @@ export function compileScript(
877882 }
878883 }
879884
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
882889 if ( hasStaticWithDefaults ( ) ) {
883890 // if withDefaults() is used, we need to remove the optional flags
884891 // on props that have default values
@@ -903,20 +910,19 @@ export function compileScript(
903910 res +=
904911 m . key . name +
905912 ( m . type === 'TSMethodSignature' ? '()' : '' ) +
906- scriptSetupSource . slice (
913+ scriptSource . slice (
907914 m . typeAnnotation . start ! ,
908915 m . typeAnnotation . end !
909916 ) +
910917 ', '
911918 } else {
912- res +=
913- scriptSetupSource . slice ( m . start ! , m . typeAnnotation . end ! ) + `, `
919+ res += scriptSource . slice ( m . start ! , m . typeAnnotation . end ! ) + `, `
914920 }
915921 }
916922 }
917923 return ( res . length ? res . slice ( 0 , - 2 ) : res ) + ` }`
918924 } else {
919- return scriptSetupSource . slice ( node . start ! , node . end ! )
925+ return scriptSource . slice ( node . start ! , node . end ! )
920926 }
921927 }
922928
@@ -1480,7 +1486,10 @@ export function compileScript(
14801486 if ( destructureElements . length ) {
14811487 args += `, { ${ destructureElements . join ( ', ' ) } }`
14821488 if ( emitsTypeDecl ) {
1483- args += `: { emit: (${ scriptSetup . content . slice (
1489+ const content = emitsTypeDecl . __fromNormalScript
1490+ ? script ! . content
1491+ : scriptSetup . content
1492+ args += `: { emit: (${ content . slice (
14841493 emitsTypeDecl . start ! ,
14851494 emitsTypeDecl . end !
14861495 ) } ), expose: any, slots: any, attrs: any }`
0 commit comments