Skip to content

Commit

Permalink
fix(compiler-sfc): fix function scope variable declaration marking
Browse files Browse the repository at this point in the history
yyx990803 committed Mar 29, 2021
1 parent bb937e9 commit 69b4727
Showing 1 changed file with 50 additions and 7 deletions.
57 changes: 50 additions & 7 deletions packages/compiler-sfc/src/compileScript.ts
Original file line number Diff line number Diff line change
@@ -1389,13 +1389,11 @@ export function walkIdentifiers(
if (node.body.type === 'BlockStatement') {
node.body.body.forEach(p => {
if (p.type === 'VariableDeclaration') {
;(walk as any)(p, {
enter(child: Node) {
if (child.type === 'Identifier') {
markScopeIdentifier(node, child, knownIds)
}
}
})
for (const decl of p.declarations) {
extractIdentifiers(decl.id).forEach(id => {
markScopeIdentifier(node, id, knownIds)
})
}
}
})
}
@@ -1692,3 +1690,48 @@ function getObjectOrArrayExpressionKeys(value: Node): string[] {
}
return []
}

function extractIdentifiers(
param: Node,
nodes: Identifier[] = []
): Identifier[] {
switch (param.type) {
case 'Identifier':
nodes.push(param)
break

case 'MemberExpression':
let object: any = param
while (object.type === 'MemberExpression') {
object = object.object
}
nodes.push(object)
break

case 'ObjectPattern':
param.properties.forEach(prop => {
if (prop.type === 'RestElement') {
extractIdentifiers(prop.argument, nodes)
} else {
extractIdentifiers(prop.value, nodes)
}
})
break

case 'ArrayPattern':
param.elements.forEach(element => {
if (element) extractIdentifiers(element, nodes)
})
break

case 'RestElement':
extractIdentifiers(param.argument, nodes)
break

case 'AssignmentPattern':
extractIdentifiers(param.left, nodes)
break
}

return nodes
}

0 comments on commit 69b4727

Please sign in to comment.