]> git.ipfire.org Git - thirdparty/vuejs/core.git/commitdiff
fix(compiler-sfc): fix function scope variable declaration marking
authorEvan You <yyx990803@gmail.com>
Mon, 29 Mar 2021 04:32:46 +0000 (00:32 -0400)
committerEvan You <yyx990803@gmail.com>
Mon, 29 Mar 2021 04:32:46 +0000 (00:32 -0400)
packages/compiler-sfc/src/compileScript.ts

index 0cfe248958669b5d261a466e429a0392dac94835..29cf4b2dc33a01ecfe2c75c187e5942afcc0e92c 100644 (file)
@@ -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
+}