]> git.ipfire.org Git - thirdparty/vuejs/core.git/commitdiff
refactor(compiler-vapor): extract v-on
author三咲智子 Kevin Deng <sxzz@sxzz.moe>
Wed, 6 Dec 2023 16:39:31 +0000 (00:39 +0800)
committer三咲智子 Kevin Deng <sxzz@sxzz.moe>
Wed, 6 Dec 2023 16:39:31 +0000 (00:39 +0800)
packages/compiler-vapor/src/compile.ts
packages/compiler-vapor/src/transforms/transformElement.ts
packages/compiler-vapor/src/transforms/vBind.ts [new file with mode: 0644]

index 5a5c0cc1466cd2066ea14af6f16808bebe1e2d2e..e386de3b3944e5d782bb52d4664e8c32f13edc4a 100644 (file)
@@ -18,6 +18,7 @@ import { transformOnce } from './transforms/vOnce'
 import { transformElement } from './transforms/transformElement'
 import { transformVHtml } from './transforms/vHtml'
 import { transformVText } from './transforms/vText'
+import { transformVBind } from './transforms/vBind'
 import { transformVOn } from './transforms/vOn'
 import { transformInterpolation } from './transforms/transformInterpolation'
 import type { HackOptions } from './ir'
@@ -92,6 +93,7 @@ export function getBaseTransformPreset(
   return [
     [transformOnce, transformInterpolation, transformElement],
     {
+      bind: transformVBind,
       on: transformVOn,
       html: transformVHtml,
       text: transformVText,
index c0d0a34508fb488cfd7bf43f42ae81300467fc90..b27509a281fb4c127ab3ea046e3af50cd969d706 100644 (file)
@@ -2,12 +2,9 @@ import {
   type ElementNode,
   type AttributeNode,
   NodeTypes,
-  ErrorCodes,
-  createCompilerError,
   ElementTypes,
-  createSimpleExpression,
 } from '@vue/compiler-dom'
-import { camelize, isBuiltInDirective, isVoidTag } from '@vue/shared'
+import { isBuiltInDirective, isVoidTag } from '@vue/shared'
 import { NodeTransform, TransformContext } from '../transform'
 import { VaporDirectiveNode, IRNodeTypes } from '../ir'
 
@@ -69,54 +66,17 @@ function transformProp(
     return
   }
 
-  let { arg, exp, loc } = prop
   const directiveTransform = context.options.directiveTransforms[name]
   if (directiveTransform) {
     directiveTransform(prop, node, context)
   } else if (!isBuiltInDirective(name)) {
+    // custom directive
     context.registerOperation({
       type: IRNodeTypes.WITH_DIRECTIVE,
       element: context.reference(),
       name,
-      binding: exp,
+      binding: prop.exp,
       loc: prop.loc,
     })
   }
-
-  switch (name) {
-    case 'bind': {
-      if (!arg) {
-        // TODO support v-bind="{}"
-        return
-      }
-      if (!exp) {
-        // shorthand syntax https://github.com/vuejs/core/pull/9451
-        const propName = camelize(arg.content)
-        exp = createSimpleExpression(propName, false, arg.loc)
-        exp.ast = null
-      }
-
-      if (!exp.content.trim()) {
-        context.options.onError(
-          createCompilerError(ErrorCodes.X_V_BIND_NO_EXPRESSION, loc),
-        )
-        context.template += ` ${arg.content}=""`
-        return
-      }
-
-      context.registerEffect(
-        [exp],
-        [
-          {
-            type: IRNodeTypes.SET_PROP,
-            loc: prop.loc,
-            element: context.reference(),
-            name: arg,
-            value: exp,
-          },
-        ],
-      )
-      break
-    }
-  }
 }
diff --git a/packages/compiler-vapor/src/transforms/vBind.ts b/packages/compiler-vapor/src/transforms/vBind.ts
new file mode 100644 (file)
index 0000000..1502a29
--- /dev/null
@@ -0,0 +1,44 @@
+import {
+  createCompilerError,
+  createSimpleExpression,
+  ErrorCodes,
+} from '@vue/compiler-core'
+import { camelize } from '@vue/shared'
+import { IRNodeTypes } from '../ir'
+import type { DirectiveTransform } from '../transform'
+
+export const transformVBind: DirectiveTransform = (dir, node, context) => {
+  let { arg, exp, loc } = dir
+
+  if (!arg) {
+    // TODO support v-bind="{}"
+    return
+  }
+  if (!exp) {
+    // shorthand syntax https://github.com/vuejs/core/pull/9451
+    const propName = camelize(arg.content)
+    exp = createSimpleExpression(propName, false, arg.loc)
+    exp.ast = null
+  }
+
+  if (!exp.content.trim()) {
+    context.options.onError(
+      createCompilerError(ErrorCodes.X_V_BIND_NO_EXPRESSION, loc),
+    )
+    context.template += ` ${arg.content}=""`
+    return
+  }
+
+  context.registerEffect(
+    [exp],
+    [
+      {
+        type: IRNodeTypes.SET_PROP,
+        loc: dir.loc,
+        element: context.reference(),
+        name: arg,
+        value: exp,
+      },
+    ],
+  )
+}