refactor(compiler-core): emit error on v-if key usage

This commit is contained in:
Evan You
2020-07-28 15:18:41 -04:00
parent 355c052622
commit 58b4a382f9
4 changed files with 23 additions and 32 deletions

View File

@@ -145,17 +145,3 @@ return function render(_ctx, _cache) {
}
}"
`;
exports[`compiler: v-if codegen v-if with key 1`] = `
"const _Vue = Vue
return function render(_ctx, _cache) {
with (_ctx) {
const { createVNode: _createVNode, openBlock: _openBlock, createBlock: _createBlock, createCommentVNode: _createCommentVNode } = _Vue
return ok
? (_openBlock(), _createBlock(\\"div\\", { key: \\"some-key\\" }))
: _createCommentVNode(\\"v-if\\", true)
}
}"
`;

View File

@@ -282,6 +282,16 @@ describe('compiler: v-if', () => {
}
])
})
test('error on user key', () => {
const onError = jest.fn()
parseWithIfTransform(`<div v-if="ok" :key="1" />`, { onError })
expect(onError.mock.calls[0]).toMatchObject([
{
code: ErrorCodes.X_V_IF_KEY
}
])
})
})
describe('codegen', () => {
@@ -581,18 +591,6 @@ describe('compiler: v-if', () => {
expect(branch1.props).toMatchObject(createObjectMatcher({ key: `[0]` }))
})
test('v-if with key', () => {
const {
root,
node: { codegenNode }
} = parseWithIfTransform(`<div v-if="ok" key="some-key"/>`)
expect(codegenNode.consequent).toMatchObject({
tag: `"div"`,
props: createObjectMatcher({ key: 'some-key' })
})
expect(generate(root).code).toMatchSnapshot()
})
test('with comments', () => {
const { node } = parseWithIfTransform(`
<template v-if="ok">