refactor(compiler): improve member expression check for v-on & v-model

This commit is contained in:
Evan You
2019-10-10 11:15:24 -04:00
parent 87c3d2edae
commit f11dadc1d2
9 changed files with 63 additions and 13 deletions

View File

@@ -351,5 +351,17 @@ describe('compiler: transform v-model', () => {
})
)
})
test('mal-formed expression', () => {
const onError = jest.fn()
parseWithVModel('<span v-model="a + b" />', { onError })
expect(onError).toHaveBeenCalledTimes(1)
expect(onError).toHaveBeenCalledWith(
expect.objectContaining({
code: ErrorCodes.X_V_MODEL_MALFORMED_EXPRESSION
})
)
})
})
})

View File

@@ -175,6 +175,34 @@ describe('compiler: transform v-on', () => {
})
})
test('should NOT wrap as function if expression is complex member expression', () => {
const node = parseWithVOn(`<div @click="a['b' + c]"/>`)
const props = (node.codegenNode as CallExpression)
.arguments[1] as ObjectExpression
expect(props.properties[0]).toMatchObject({
key: { content: `onClick` },
value: {
type: NodeTypes.SIMPLE_EXPRESSION,
content: `a['b' + c]`
}
})
})
test('complex member expression w/ prefixIdentifiers: true', () => {
const node = parseWithVOn(`<div @click="a['b' + c]"/>`, {
prefixIdentifiers: true
})
const props = (node.codegenNode as CallExpression)
.arguments[1] as ObjectExpression
expect(props.properties[0]).toMatchObject({
key: { content: `onClick` },
value: {
type: NodeTypes.COMPOUND_EXPRESSION,
children: [{ content: `_ctx.a` }, `['b' + `, { content: `_ctx.c` }, `]`]
}
})
})
test('function expression w/ prefixIdentifiers: true', () => {
const node = parseWithVOn(`<div @click="e => foo(e)"/>`, {
prefixIdentifiers: true