]> git.ipfire.org Git - thirdparty/vuejs/core.git/commitdiff
refactor: use more efficient walk for importUsageCheck
authorEvan You <yyx990803@gmail.com>
Tue, 21 Nov 2023 14:10:00 +0000 (22:10 +0800)
committerEvan You <yyx990803@gmail.com>
Sat, 25 Nov 2023 08:18:29 +0000 (16:18 +0800)
packages/compiler-sfc/src/script/importUsageCheck.ts

index c3c51185c5645bb48788900e0d32f4168ae04dc1..cd97566d55f2dddbd719a70d72b85786d59b8b24 100644 (file)
@@ -5,8 +5,8 @@ import {
   SimpleExpressionNode,
   forAliasRE,
   parserOptions,
-  transform,
-  walkIdentifiers
+  walkIdentifiers,
+  TemplateChildNode
 } from '@vue/compiler-dom'
 import { createCache } from '../cache'
 import { camelize, capitalize, isBuiltInDirective } from '@vue/shared'
@@ -34,53 +34,54 @@ function resolveTemplateUsageCheckString(sfc: SFCDescriptor) {
   }
 
   let code = ''
-  transform(ast, {
-    nodeTransforms: [
-      node => {
-        if (node.type === NodeTypes.ELEMENT) {
-          if (
-            !parserOptions.isNativeTag!(node.tag) &&
-            !parserOptions.isBuiltInComponent!(node.tag)
-          ) {
-            code += `,${camelize(node.tag)},${capitalize(camelize(node.tag))}`
-          }
-          for (let i = 0; i < node.props.length; i++) {
-            const prop = node.props[i]
-            if (prop.type === NodeTypes.DIRECTIVE) {
-              if (!isBuiltInDirective(prop.name)) {
-                code += `,v${capitalize(camelize(prop.name))}`
-              }
 
-              // process dynamic directive arguments
-              if (prop.arg && !(prop.arg as SimpleExpressionNode).isStatic) {
-                code += `,${stripStrings(
-                  (prop.arg as SimpleExpressionNode).content
-                )}`
-              }
+  ast!.children.forEach(walk)
 
-              if (prop.exp) {
-                code += `,${processExp(
-                  (prop.exp as SimpleExpressionNode).content,
-                  prop.name
-                )}`
-              }
+  function walk(node: TemplateChildNode) {
+    switch (node.type) {
+      case NodeTypes.ELEMENT:
+        if (
+          !parserOptions.isNativeTag!(node.tag) &&
+          !parserOptions.isBuiltInComponent!(node.tag)
+        ) {
+          code += `,${camelize(node.tag)},${capitalize(camelize(node.tag))}`
+        }
+        for (let i = 0; i < node.props.length; i++) {
+          const prop = node.props[i]
+          if (prop.type === NodeTypes.DIRECTIVE) {
+            if (!isBuiltInDirective(prop.name)) {
+              code += `,v${capitalize(camelize(prop.name))}`
             }
-            if (
-              prop.type === NodeTypes.ATTRIBUTE &&
-              prop.name === 'ref' &&
-              prop.value?.content
-            ) {
-              code += `,${prop.value.content}`
+
+            // process dynamic directive arguments
+            if (prop.arg && !(prop.arg as SimpleExpressionNode).isStatic) {
+              code += `,${stripStrings(
+                (prop.arg as SimpleExpressionNode).content
+              )}`
+            }
+
+            if (prop.exp) {
+              code += `,${processExp(
+                (prop.exp as SimpleExpressionNode).content,
+                prop.name
+              )}`
             }
           }
-        } else if (node.type === NodeTypes.INTERPOLATION) {
-          code += `,${processExp(
-            (node.content as SimpleExpressionNode).content
-          )}`
+          if (
+            prop.type === NodeTypes.ATTRIBUTE &&
+            prop.name === 'ref' &&
+            prop.value?.content
+          ) {
+            code += `,${prop.value.content}`
+          }
         }
-      }
-    ]
-  })
+        node.children.forEach(walk)
+        break
+      case NodeTypes.INTERPOLATION:
+        code += `,${processExp((node.content as SimpleExpressionNode).content)}`
+        break
+    }
+  }
 
   code += ';'
   templateUsageCheckCache.set(content, code)