Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(compiler-core): prevent generate empty key value for vBind #1720

Merged
merged 2 commits into from
Jul 27, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion packages/compiler-core/__tests__/transforms/vBind.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,8 @@ describe('compiler: transform v-bind', () => {

test('should error if no expression', () => {
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({
code: ErrorCodes.X_V_BIND_NO_EXPRESSION,
loc: {
Expand All @@ -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', () => {
Expand Down
18 changes: 12 additions & 6 deletions packages/compiler-core/src/transforms/vBind.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,6 @@ import { CAMELIZE } from '../runtimeHelpers'
export const transformBind: DirectiveTransform = (dir, node, context) => {
const { exp, modifiers, loc } = dir
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
// .sync is replaced by v-model:arg
if (modifiers.includes('camel')) {
Expand All @@ -27,9 +24,18 @@ export const transformBind: DirectiveTransform = (dir, node, context) => {
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 {
props: [
createObjectProperty(arg!, exp || createSimpleExpression('', true, loc))
]
props: [createObjectProperty(arg!, exp)]
}
}