Skip to content

Commit d452723

Browse files
authored
fix(compiler-core): prevent generating invalid code for v-bind with empty expression (#1720)
1 parent 5fbd1f4 commit d452723

File tree

2 files changed

+24
-7
lines changed

2 files changed

+24
-7
lines changed

packages/compiler-core/__tests__/transforms/vBind.spec.ts

+12-1
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,8 @@ describe('compiler: transform v-bind', () => {
8383

8484
test('should error if no expression', () => {
8585
const onError = jest.fn()
86-
parseWithVBind(`<div v-bind:arg />`, { onError })
86+
const node = parseWithVBind(`<div v-bind:arg />`, { onError })
87+
const props = (node.codegenNode as VNodeCall).props as ObjectExpression
8788
expect(onError.mock.calls[0][0]).toMatchObject({
8889
code: ErrorCodes.X_V_BIND_NO_EXPRESSION,
8990
loc: {
@@ -97,6 +98,16 @@ describe('compiler: transform v-bind', () => {
9798
}
9899
}
99100
})
101+
expect(props.properties[0]).toMatchObject({
102+
key: {
103+
content: `arg`,
104+
isStatic: true
105+
},
106+
value: {
107+
content: ``,
108+
isStatic: true
109+
}
110+
})
100111
})
101112

102113
test('.camel modifier', () => {

packages/compiler-core/src/transforms/vBind.ts

+12-6
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,6 @@ import { CAMELIZE } from '../runtimeHelpers'
1010
export const transformBind: DirectiveTransform = (dir, node, context) => {
1111
const { exp, modifiers, loc } = dir
1212
const arg = dir.arg!
13-
if (!exp || (exp.type === NodeTypes.SIMPLE_EXPRESSION && !exp.content)) {
14-
context.onError(createCompilerError(ErrorCodes.X_V_BIND_NO_EXPRESSION, loc))
15-
}
1613
// .prop is no longer necessary due to new patch behavior
1714
// .sync is replaced by v-model:arg
1815
if (modifiers.includes('camel')) {
@@ -27,9 +24,18 @@ export const transformBind: DirectiveTransform = (dir, node, context) => {
2724
arg.children.push(`)`)
2825
}
2926
}
27+
28+
if (
29+
!exp ||
30+
(exp.type === NodeTypes.SIMPLE_EXPRESSION && !exp.content.trim())
31+
) {
32+
context.onError(createCompilerError(ErrorCodes.X_V_BIND_NO_EXPRESSION, loc))
33+
return {
34+
props: [createObjectProperty(arg!, createSimpleExpression('', true, loc))]
35+
}
36+
}
37+
3038
return {
31-
props: [
32-
createObjectProperty(arg!, exp || createSimpleExpression('', true, loc))
33-
]
39+
props: [createObjectProperty(arg!, exp)]
3440
}
3541
}

0 commit comments

Comments
 (0)