@@ -261,6 +261,27 @@ export function transformAST(
261
261
return s . original . slice ( node . start ! + offset , node . end ! + offset )
262
262
}
263
263
264
+ const WRAPPER_NODE_TYPES = [
265
+ 'ParenthesizedExpression' , // (foo)
266
+ 'TSNonNullExpression' , // foo!
267
+ 'TSAsExpression' , // foo as number
268
+ 'TSTypeAssertion' , // (<number>foo)
269
+ 'TSInstantiationExpression' // foo<string>
270
+ ]
271
+ function findUpParent ( ) {
272
+ return parentStack
273
+ . slice ( )
274
+ . reverse ( )
275
+ . find ( ( { type } ) => ! WRAPPER_NODE_TYPES . includes ( type ) )
276
+ }
277
+ function unwrapNode ( node : Node ) : Node {
278
+ if ( WRAPPER_NODE_TYPES . includes ( node . type ) ) {
279
+ return unwrapNode ( ( node as any ) . expression )
280
+ } else {
281
+ return node
282
+ }
283
+ }
284
+
264
285
function walkScope ( node : Program | BlockStatement , isRoot = false ) {
265
286
for ( const stmt of node . body ) {
266
287
if ( stmt . type === 'VariableDeclaration' ) {
@@ -297,20 +318,13 @@ export function transformAST(
297
318
}
298
319
for ( const decl of stmt . declarations ) {
299
320
let refCall
321
+ const init = decl . init ? unwrapNode ( decl . init ) : null
300
322
const isCall =
301
- decl . init &&
302
- decl . init . type === 'CallExpression' &&
303
- decl . init . callee . type === 'Identifier'
304
- if (
305
- isCall &&
306
- ( refCall = isRefCreationCall ( ( decl as any ) . init . callee . name ) )
307
- ) {
308
- processRefDeclaration (
309
- refCall ,
310
- decl . id ,
311
- decl . init as CallExpression ,
312
- stmt . kind === 'const'
313
- )
323
+ init &&
324
+ init . type === 'CallExpression' &&
325
+ init . callee . type === 'Identifier'
326
+ if ( isCall && ( refCall = isRefCreationCall ( ( init . callee as any ) . name ) ) ) {
327
+ processRefDeclaration ( refCall , decl . id , init , stmt . kind === 'const' )
314
328
} else {
315
329
const isProps =
316
330
isRoot && isCall && ( decl as any ) . init . callee . name === 'defineProps'
@@ -663,9 +677,7 @@ export function transformAST(
663
677
if (
664
678
parent &&
665
679
parent . type . startsWith ( 'TS' ) &&
666
- parent . type !== 'TSAsExpression' &&
667
- parent . type !== 'TSNonNullExpression' &&
668
- parent . type !== 'TSTypeAssertion'
680
+ ! WRAPPER_NODE_TYPES . includes ( parent . type )
669
681
) {
670
682
return this . skip ( )
671
683
}
@@ -692,6 +704,7 @@ export function transformAST(
692
704
const callee = node . callee . name
693
705
694
706
const refCall = isRefCreationCall ( callee )
707
+ const parent = findUpParent ( )
695
708
if ( refCall && ( ! parent || parent . type !== 'VariableDeclarator' ) ) {
696
709
return error (
697
710
`${ refCall } can only be used as the initializer of ` +
@@ -762,6 +775,8 @@ export function transformAST(
762
775
}
763
776
} )
764
777
778
+ console . log ( s . toString ( ) )
779
+
765
780
return {
766
781
rootRefs : Object . keys ( rootScope ) . filter ( key => {
767
782
const binding = rootScope [ key ]
0 commit comments