From 7ef1d2269f97444d551879648d62d799a4e59425 Mon Sep 17 00:00:00 2001 From: soouup <413732712@qq.com> Date: Thu, 16 Jan 2020 17:29:17 +0800 Subject: [PATCH 1/3] feat(compiler-core): remove repeat key --- packages/compiler-core/src/utils.ts | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/packages/compiler-core/src/utils.ts b/packages/compiler-core/src/utils.ts index 3060c1c1095..5a48d1c5dd6 100644 --- a/packages/compiler-core/src/utils.ts +++ b/packages/compiler-core/src/utils.ts @@ -253,7 +253,14 @@ export function injectProp( } propsWithInjection = props } else if (props.type === NodeTypes.JS_OBJECT_EXPRESSION) { - props.properties.unshift(prop) + let isRepeated = false + if (prop.key.type === NodeTypes.SIMPLE_EXPRESSION) { + const propKeyName = prop.key.content + isRepeated = props.properties.some(p => p.key.type === NodeTypes.SIMPLE_EXPRESSION && p.key.content === propKeyName) + } + if (!isRepeated) { + props.properties.unshift(prop) + } propsWithInjection = props } else { // single v-bind with expression, return a merged replacement From ae03cc790a292c9b6c5545dc22e2f78204dcc2a6 Mon Sep 17 00:00:00 2001 From: soouup <413732712@qq.com> Date: Fri, 17 Jan 2020 12:15:25 +0800 Subject: [PATCH 2/3] test(compiler-core): add v-if with key --- .../transforms/__snapshots__/vIf.spec.ts.snap | 14 ++++++++++++++ .../compiler-core/__tests__/transforms/vIf.spec.ts | 14 ++++++++++++++ 2 files changed, 28 insertions(+) diff --git a/packages/compiler-core/__tests__/transforms/__snapshots__/vIf.spec.ts.snap b/packages/compiler-core/__tests__/transforms/__snapshots__/vIf.spec.ts.snap index 498c03d35c6..d647b073453 100644 --- a/packages/compiler-core/__tests__/transforms/__snapshots__/vIf.spec.ts.snap +++ b/packages/compiler-core/__tests__/transforms/__snapshots__/vIf.spec.ts.snap @@ -105,3 +105,17 @@ return function render() { } }" `; + +exports[`compiler: v-if codegen v-if with key 1`] = ` +"const _Vue = Vue + +return function render() { + with (this) { + const { openBlock: _openBlock, createVNode: _createVNode, createBlock: _createBlock, createCommentVNode: _createCommentVNode } = _Vue + + return (_openBlock(), ok + ? _createBlock(\\"div\\", { key: \\"some-key\\" }) + : _createCommentVNode(\\"v-if\\", true)) + } +}" +`; diff --git a/packages/compiler-core/__tests__/transforms/vIf.spec.ts b/packages/compiler-core/__tests__/transforms/vIf.spec.ts index f50ad66297d..b6c2e86a28c 100644 --- a/packages/compiler-core/__tests__/transforms/vIf.spec.ts +++ b/packages/compiler-core/__tests__/transforms/vIf.spec.ts @@ -530,6 +530,20 @@ describe('compiler: v-if', () => { ) }) + test('v-if with key', () => { + const { + root, + node: { codegenNode } + } = parseWithIfTransform(`
`) + const branch1 = (codegenNode.expressions[1] as ConditionalExpression) + .consequent as CallExpression + expect(branch1.arguments).toMatchObject([ + `"div"`, + createObjectMatcher({ key: 'some-key' }) + ]) + expect(generate(root).code).toMatchSnapshot() + }) + test.todo('with comments') }) }) From d949e70fed3bf7c4c548652e9688e7e38bb0a562 Mon Sep 17 00:00:00 2001 From: Evan You Date: Mon, 20 Jan 2020 10:13:26 -0500 Subject: [PATCH 3/3] Update utils.ts --- packages/compiler-core/src/utils.ts | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/packages/compiler-core/src/utils.ts b/packages/compiler-core/src/utils.ts index 5a48d1c5dd6..280e9c5e2dc 100644 --- a/packages/compiler-core/src/utils.ts +++ b/packages/compiler-core/src/utils.ts @@ -253,12 +253,16 @@ export function injectProp( } propsWithInjection = props } else if (props.type === NodeTypes.JS_OBJECT_EXPRESSION) { - let isRepeated = false + let alreadyExists = false + // check existing key to avoid overriding user provided keys if (prop.key.type === NodeTypes.SIMPLE_EXPRESSION) { const propKeyName = prop.key.content - isRepeated = props.properties.some(p => p.key.type === NodeTypes.SIMPLE_EXPRESSION && p.key.content === propKeyName) + alreadyExists = props.properties.some(p => ( + p.key.type === NodeTypes.SIMPLE_EXPRESSION && + p.key.content === propKeyName + )) } - if (!isRepeated) { + if (!alreadyExists) { props.properties.unshift(prop) } propsWithInjection = props