Skip to content

Commit 5d4e449

Browse files
committed
fix: wip
1 parent 0fbc19f commit 5d4e449

File tree

1 file changed

+31
-16
lines changed

1 file changed

+31
-16
lines changed

packages/reactivity-transform/src/reactivityTransform.ts

Lines changed: 31 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,27 @@ export function transformAST(
261261
return s.original.slice(node.start! + offset, node.end! + offset)
262262
}
263263

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+
264285
function walkScope(node: Program | BlockStatement, isRoot = false) {
265286
for (const stmt of node.body) {
266287
if (stmt.type === 'VariableDeclaration') {
@@ -297,20 +318,13 @@ export function transformAST(
297318
}
298319
for (const decl of stmt.declarations) {
299320
let refCall
321+
const init = decl.init ? unwrapNode(decl.init) : null
300322
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')
314328
} else {
315329
const isProps =
316330
isRoot && isCall && (decl as any).init.callee.name === 'defineProps'
@@ -663,9 +677,7 @@ export function transformAST(
663677
if (
664678
parent &&
665679
parent.type.startsWith('TS') &&
666-
parent.type !== 'TSAsExpression' &&
667-
parent.type !== 'TSNonNullExpression' &&
668-
parent.type !== 'TSTypeAssertion'
680+
!WRAPPER_NODE_TYPES.includes(parent.type)
669681
) {
670682
return this.skip()
671683
}
@@ -692,6 +704,7 @@ export function transformAST(
692704
const callee = node.callee.name
693705

694706
const refCall = isRefCreationCall(callee)
707+
const parent = findUpParent()
695708
if (refCall && (!parent || parent.type !== 'VariableDeclarator')) {
696709
return error(
697710
`${refCall} can only be used as the initializer of ` +
@@ -762,6 +775,8 @@ export function transformAST(
762775
}
763776
})
764777

778+
console.log(s.toString())
779+
765780
return {
766781
rootRefs: Object.keys(rootScope).filter(key => {
767782
const binding = rootScope[key]

0 commit comments

Comments
 (0)