@@ -328,9 +328,10 @@ ${commandLine.vueOptions.target < 3 ? vue2TypeHelpersCode : typeHelpersCode}
328
328
329
329
// fill defaults
330
330
const printer = ts . createPrinter ( checkerOptions . printer ) ;
331
- const snapshot = language . scripts . get ( componentPath ) ?. snapshot ! ;
331
+ const sourceScript = language . scripts . get ( componentPath ) ! ;
332
+ const { snapshot } = sourceScript ;
332
333
333
- const vueFile = language . scripts . get ( componentPath ) ? .generated ?. root ;
334
+ const vueFile = sourceScript . generated ?. root ;
334
335
const vueDefaults = vueFile && exportName === 'default'
335
336
? ( vueFile instanceof vue . VueVirtualCode ? readVueComponentDefaultProps ( vueFile , printer , ts ) : { } )
336
337
: { } ;
@@ -674,9 +675,9 @@ function createSchemaResolvers(
674
675
}
675
676
function getDeclaration ( declaration : ts . Declaration ) : Declaration | undefined {
676
677
const fileName = declaration . getSourceFile ( ) . fileName ;
677
- const sourceFile = language . scripts . get ( fileName ) ;
678
- if ( sourceFile ?. generated ) {
679
- const script = sourceFile . generated . languagePlugin . typescript ?. getServiceScript ( sourceFile . generated . root ) ;
678
+ const sourceScript = language . scripts . get ( fileName ) ;
679
+ if ( sourceScript ?. generated ) {
680
+ const script = sourceScript . generated . languagePlugin . typescript ?. getServiceScript ( sourceScript . generated . root ) ;
680
681
if ( script ) {
681
682
for ( const [ sourceScript , map ] of language . maps . forEach ( script . code ) ) {
682
683
for ( const [ start ] of map . toSourceLocation ( declaration . getStart ( ) ) ) {
@@ -711,7 +712,10 @@ function readVueComponentDefaultProps(
711
712
printer : ts . Printer | undefined ,
712
713
ts : typeof import ( 'typescript' )
713
714
) {
714
- let result : Record < string , { default ?: string , required ?: boolean ; } > = { } ;
715
+ let result : Record < string , {
716
+ default ?: string ;
717
+ required ?: boolean ;
718
+ } > = { } ;
715
719
716
720
scriptSetupWorker ( ) ;
717
721
scriptWorker ( ) ;
@@ -720,16 +724,16 @@ function readVueComponentDefaultProps(
720
724
721
725
function scriptSetupWorker ( ) {
722
726
723
- const sfc = root . _sfc ;
724
- const codegen = vue . tsCodegen . get ( sfc ) ;
725
- const scriptSetupRanges = codegen ?. scriptSetupRanges . get ( ) ;
726
-
727
- if ( sfc . scriptSetup && scriptSetupRanges ?. withDefaults ?. arg ) {
727
+ const ast = root . _sfc . scriptSetup ?. ast ;
728
+ if ( ! ast ) {
729
+ return ;
730
+ }
728
731
729
- const defaultsText = sfc . scriptSetup . content . slice ( scriptSetupRanges . withDefaults . arg . start , scriptSetupRanges . withDefaults . arg . end ) ;
730
- const ast = ts . createSourceFile ( '/tmp.' + sfc . scriptSetup . lang , '(' + defaultsText + ')' , ts . ScriptTarget . Latest ) ;
731
- const obj = findObjectLiteralExpression ( ast ) ;
732
+ const codegen = vue . tsCodegen . get ( root . _sfc ) ;
733
+ const scriptSetupRanges = codegen ?. scriptSetupRanges . get ( ) ;
732
734
735
+ if ( scriptSetupRanges ?. withDefaults ?. argNode ) {
736
+ const obj = findObjectLiteralExpression ( scriptSetupRanges . withDefaults . argNode ) ;
733
737
if ( obj ) {
734
738
for ( const prop of obj . properties ) {
735
739
if ( ts . isPropertyAssignment ( prop ) ) {
@@ -743,19 +747,17 @@ function readVueComponentDefaultProps(
743
747
}
744
748
}
745
749
}
746
- } else if ( sfc . scriptSetup && scriptSetupRanges ?. defineProps ?. arg ) {
747
- const defaultsText = sfc . scriptSetup . content . slice ( scriptSetupRanges . defineProps . arg . start , scriptSetupRanges . defineProps . arg . end ) ;
748
- const ast = ts . createSourceFile ( '/tmp.' + sfc . scriptSetup . lang , '(' + defaultsText + ')' , ts . ScriptTarget . Latest ) ;
749
- const obj = findObjectLiteralExpression ( ast ) ;
750
-
750
+ }
751
+ else if ( scriptSetupRanges ?. defineProps ?. argNode ) {
752
+ const obj = findObjectLiteralExpression ( scriptSetupRanges . defineProps . argNode ) ;
751
753
if ( obj ) {
752
754
result = {
753
755
...result ,
754
756
...resolvePropsOption ( ast , obj , printer , ts ) ,
755
757
} ;
756
758
}
757
- } else if ( sfc . scriptSetup && scriptSetupRanges ?. defineProps ?. destructured ) {
758
- const ast = sfc . scriptSetup . ast ;
759
+ }
760
+ else if ( scriptSetupRanges ?. defineProps ?. destructured ) {
759
761
for ( const [ prop , initializer ] of scriptSetupRanges . defineProps . destructured ) {
760
762
if ( initializer ) {
761
763
const expText = printer ?. printNode ( ts . EmitHint . Expression , initializer , ast ) ?? initializer . getText ( ast ) ;
@@ -780,10 +782,10 @@ function readVueComponentDefaultProps(
780
782
781
783
function scriptWorker ( ) {
782
784
783
- const descriptor = root . _sfc ;
785
+ const sfc = root . _sfc ;
784
786
785
- if ( descriptor . script ) {
786
- const scriptResult = readTsComponentDefaultProps ( descriptor . script . lang , descriptor . script . content , 'default' , printer , ts ) ;
787
+ if ( sfc . script ) {
788
+ const scriptResult = readTsComponentDefaultProps ( sfc . script . lang , sfc . script . content , 'default' , printer , ts ) ;
787
789
for ( const [ key , value ] of Object . entries ( scriptResult ) ) {
788
790
result [ key ] = value ;
789
791
}
0 commit comments