]> git.ipfire.org Git - thirdparty/vuejs/core.git/commitdiff
fix(compiler-core): prevent generating invalid code for v-bind with empty expression...
authorunderfin <2218301630@qq.com>
Mon, 27 Jul 2020 22:31:08 +0000 (06:31 +0800)
committerGitHub <noreply@github.com>
Mon, 27 Jul 2020 22:31:08 +0000 (18:31 -0400)
packages/compiler-core/__tests__/transforms/vBind.spec.ts
packages/compiler-core/src/transforms/vBind.ts

index b53160fd26758de7b7797d7fba8162ff43dee209..76482fcc3c8377647651044f9d40e6ebd363f601 100644 (file)
@@ -83,7 +83,8 @@ describe('compiler: transform v-bind', () => {
 
   test('should error if no expression', () => {
     const onError = jest.fn()
-    parseWithVBind(`<div v-bind:arg />`, { onError })
+    const node = parseWithVBind(`<div v-bind:arg />`, { onError })
+    const props = (node.codegenNode as VNodeCall).props as ObjectExpression
     expect(onError.mock.calls[0][0]).toMatchObject({
       code: ErrorCodes.X_V_BIND_NO_EXPRESSION,
       loc: {
@@ -97,6 +98,16 @@ describe('compiler: transform v-bind', () => {
         }
       }
     })
+    expect(props.properties[0]).toMatchObject({
+      key: {
+        content: `arg`,
+        isStatic: true
+      },
+      value: {
+        content: ``,
+        isStatic: true
+      }
+    })
   })
 
   test('.camel modifier', () => {
index b9e9d7debe3c118795c775a0e854f5e8cc41f2ff..cb10ed1f4c54b7e55ad327ea75a6e84edc24b807 100644 (file)
@@ -10,9 +10,6 @@ import { CAMELIZE } from '../runtimeHelpers'
 export const transformBind: DirectiveTransform = (dir, node, context) => {
   const { exp, modifiers, loc } = dir
   const arg = dir.arg!
-  if (!exp || (exp.type === NodeTypes.SIMPLE_EXPRESSION && !exp.content)) {
-    context.onError(createCompilerError(ErrorCodes.X_V_BIND_NO_EXPRESSION, loc))
-  }
   // .prop is no longer necessary due to new patch behavior
   // .sync is replaced by v-model:arg
   if (modifiers.includes('camel')) {
@@ -27,9 +24,18 @@ export const transformBind: DirectiveTransform = (dir, node, context) => {
       arg.children.push(`)`)
     }
   }
+
+  if (
+    !exp ||
+    (exp.type === NodeTypes.SIMPLE_EXPRESSION && !exp.content.trim())
+  ) {
+    context.onError(createCompilerError(ErrorCodes.X_V_BIND_NO_EXPRESSION, loc))
+    return {
+      props: [createObjectProperty(arg!, createSimpleExpression('', true, loc))]
+    }
+  }
+
   return {
-    props: [
-      createObjectProperty(arg!, exp || createSimpleExpression('', true, loc))
-    ]
+    props: [createObjectProperty(arg!, exp)]
   }
 }