fix(compiler-core): prevent generating invalid code for v-bind with empty expression (#1720)

This commit is contained in:
underfin 2020-07-28 06:31:08 +08:00 committed by GitHub
parent 5fbd1f4ccb
commit d4527230e4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 24 additions and 7 deletions

View File

@ -83,7 +83,8 @@ describe('compiler: transform v-bind', () => {
test('should error if no expression', () => { test('should error if no expression', () => {
const onError = jest.fn() 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({ expect(onError.mock.calls[0][0]).toMatchObject({
code: ErrorCodes.X_V_BIND_NO_EXPRESSION, code: ErrorCodes.X_V_BIND_NO_EXPRESSION,
loc: { 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', () => { test('.camel modifier', () => {

View File

@ -10,9 +10,6 @@ import { CAMELIZE } from '../runtimeHelpers'
export const transformBind: DirectiveTransform = (dir, node, context) => { export const transformBind: DirectiveTransform = (dir, node, context) => {
const { exp, modifiers, loc } = dir const { exp, modifiers, loc } = dir
const arg = dir.arg! 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 // .prop is no longer necessary due to new patch behavior
// .sync is replaced by v-model:arg // .sync is replaced by v-model:arg
if (modifiers.includes('camel')) { if (modifiers.includes('camel')) {
@ -27,9 +24,18 @@ export const transformBind: DirectiveTransform = (dir, node, context) => {
arg.children.push(`)`) 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 { return {
props: [ props: [createObjectProperty(arg!, exp)]
createObjectProperty(arg!, exp || createSimpleExpression('', true, loc))
]
} }
} }