]> git.ipfire.org Git - thirdparty/vuejs/core.git/commitdiff
fix(v-model): generate separate modifiers for v-model with args
authorEvan You <yyx990803@gmail.com>
Thu, 7 Nov 2019 14:40:34 +0000 (09:40 -0500)
committerEvan You <yyx990803@gmail.com>
Thu, 7 Nov 2019 14:40:34 +0000 (09:40 -0500)
packages/compiler-core/__tests__/transforms/__snapshots__/vModel.spec.ts.snap
packages/compiler-core/__tests__/transforms/vModel.spec.ts
packages/compiler-core/src/transforms/vModel.ts

index ea11cf7151991790709bb0dfc188274416063a95..5dac514795cae3952d51cff8a811bb6ef3cb321c 100644 (file)
@@ -76,7 +76,7 @@ export default function render() {
   const _ctx = this
   return (openBlock(), createBlock(\\"input\\", {
     [_ctx.value]: _ctx.model,
-    [\\"onUpdate:\\"+_ctx.value]: $event => (_ctx.model = $event)
+    [\\"onUpdate:\\" + _ctx.value]: $event => (_ctx.model = $event)
   }, null, 16 /* FULL_PROPS */))
 }"
 `;
@@ -90,7 +90,7 @@ return function render() {
     
     return (_openBlock(), _createBlock(\\"input\\", {
       [value]: model,
-      [\\"onUpdate:\\"+value]: $event => (model = $event)
+      [\\"onUpdate:\\" + value]: $event => (model = $event)
     }, null, 16 /* FULL_PROPS */))
   }
 }"
index a44b16c80f9d243e90ae025d8fb8f1b89bcd31d4..c2ce8dae3ea37a3da1a4ea756998e7a2d82b325a 100644 (file)
@@ -265,11 +265,7 @@ describe('compiler: transform v-model', () => {
     expect(props[1]).toMatchObject({
       key: {
         children: [
-          {
-            content: 'onUpdate:',
-            isStatic: true
-          },
-          '+',
+          '"onUpdate:" + ',
           {
             content: 'value',
             isStatic: false
@@ -313,11 +309,7 @@ describe('compiler: transform v-model', () => {
     expect(props[1]).toMatchObject({
       key: {
         children: [
-          {
-            content: 'onUpdate:',
-            isStatic: true
-          },
-          '+',
+          '"onUpdate:" + ',
           {
             content: '_ctx.value',
             isStatic: false
@@ -405,6 +397,37 @@ describe('compiler: transform v-model', () => {
     expect(args[4]).toBe(`["modelValue", "onUpdate:modelValue"]`)
   })
 
+  test('should generate modelModifers for component v-model with arguments', () => {
+    const root = parseWithVModel(
+      '<Comp v-model:foo.trim="foo" v-model:bar.number="bar" />',
+      {
+        prefixIdentifiers: true
+      }
+    )
+    const args = ((root.children[0] as ComponentNode)
+      .codegenNode as CallExpression).arguments
+    // props
+    expect(args[1]).toMatchObject({
+      properties: [
+        { key: { content: `foo` } },
+        { key: { content: `onUpdate:foo` } },
+        {
+          key: { content: 'fooModifiers' },
+          value: { content: `{ trim: true }`, isStatic: false }
+        },
+        { key: { content: `bar` } },
+        { key: { content: `onUpdate:bar` } },
+        {
+          key: { content: 'barModifiers' },
+          value: { content: `{ number: true }`, isStatic: false }
+        }
+      ]
+    })
+    // should NOT include modelModifiers in dynamicPropNames because it's never
+    // gonna change
+    expect(args[4]).toBe(`["foo", "onUpdate:foo", "bar", "onUpdate:bar"]`)
+  })
+
   describe('errors', () => {
     test('missing expression', () => {
       const onError = jest.fn()
index a0b7f1107e7509f2bbe8bf8725adf2009f57276d..0434894911e8a1c2a62dc888b7b736d59bdf530b 100644 (file)
@@ -43,13 +43,12 @@ export const transformModel: DirectiveTransform = (dir, node, context) => {
   const propName = arg ? arg : createSimpleExpression('modelValue', true)
   const eventName = arg
     ? arg.type === NodeTypes.SIMPLE_EXPRESSION && arg.isStatic
-      ? createSimpleExpression('onUpdate:' + arg.content, true)
+      ? `onUpdate:${arg.content}`
       : createCompoundExpression([
-          createSimpleExpression('onUpdate:', true),
-          '+',
+          '"onUpdate:" + ',
           ...(arg.type === NodeTypes.SIMPLE_EXPRESSION ? [arg] : arg.children)
         ])
-    : createSimpleExpression('onUpdate:modelValue', true)
+    : `onUpdate:modelValue`
 
   const props = [
     // modelValue: foo
@@ -80,9 +79,19 @@ export const transformModel: DirectiveTransform = (dir, node, context) => {
     const modifiers = dir.modifiers
       .map(m => (isSimpleIdentifier(m) ? m : JSON.stringify(m)) + `: true`)
       .join(`, `)
+    const modifiersKey = arg
+      ? arg.type === NodeTypes.SIMPLE_EXPRESSION && arg.isStatic
+        ? `${arg.content}Modifiers`
+        : createCompoundExpression([
+            ...(arg.type === NodeTypes.SIMPLE_EXPRESSION
+              ? [arg]
+              : arg.children),
+            ' + "Modifiers"'
+          ])
+      : `modelModifiers`
     props.push(
       createObjectProperty(
-        `modelModifiers`,
+        modifiersKey,
         createSimpleExpression(`{ ${modifiers} }`, false, dir.loc, true)
       )
     )