refactor: only inject rest args for member expression handlers + fix tests

This commit is contained in:
Evan You 2020-06-15 15:24:46 -04:00
parent 7e28173312
commit 605953a154
2 changed files with 9 additions and 11 deletions

View File

@ -142,7 +142,7 @@ describe('compiler: transform v-on', () => {
key: { content: `onClick` }, key: { content: `onClick` },
value: { value: {
type: NodeTypes.COMPOUND_EXPRESSION, type: NodeTypes.COMPOUND_EXPRESSION,
children: [`($event, ...args) => (`, { content: `i++` }, `)`] children: [`$event => (`, { content: `i++` }, `)`]
} }
} }
] ]
@ -160,18 +160,14 @@ describe('compiler: transform v-on', () => {
// should wrap with `{` for multiple statements // should wrap with `{` for multiple statements
// in this case the return value is discarded and the behavior is // in this case the return value is discarded and the behavior is
// consistent with 2.x // consistent with 2.x
children: [ children: [`$event => {`, { content: `foo();bar()` }, `}`]
`($event, ...args) => {`,
{ content: `foo();bar()` },
`}`
]
} }
} }
] ]
}) })
}) })
test('should handle multiple line statement', () => { test('should handle multi-line statement', () => {
const { node } = parseWithVOn(`<div @click="\nfoo();\nbar()\n"/>`) const { node } = parseWithVOn(`<div @click="\nfoo();\nbar()\n"/>`)
expect((node.codegenNode as VNodeCall).props).toMatchObject({ expect((node.codegenNode as VNodeCall).props).toMatchObject({
properties: [ properties: [
@ -200,7 +196,7 @@ describe('compiler: transform v-on', () => {
value: { value: {
type: NodeTypes.COMPOUND_EXPRESSION, type: NodeTypes.COMPOUND_EXPRESSION,
children: [ children: [
`($event, ...args) => (`, `$event => (`,
{ {
type: NodeTypes.COMPOUND_EXPRESSION, type: NodeTypes.COMPOUND_EXPRESSION,
children: [ children: [
@ -230,7 +226,7 @@ describe('compiler: transform v-on', () => {
value: { value: {
type: NodeTypes.COMPOUND_EXPRESSION, type: NodeTypes.COMPOUND_EXPRESSION,
children: [ children: [
`($event, ...args) => {`, `$event => {`,
{ {
children: [ children: [
{ content: `_ctx.foo` }, { content: `_ctx.foo` },
@ -452,7 +448,7 @@ describe('compiler: transform v-on', () => {
value: { value: {
type: NodeTypes.COMPOUND_EXPRESSION, type: NodeTypes.COMPOUND_EXPRESSION,
children: [ children: [
`($event, ...args) => (`, `$event => (`,
{ children: [{ content: `_ctx.foo` }, `++`] }, { children: [{ content: `_ctx.foo` }, `++`] },
`)` `)`
] ]

View File

@ -102,7 +102,9 @@ export const transformOn: DirectiveTransform = (
if (isInlineStatement || (isCacheable && isMemberExp)) { if (isInlineStatement || (isCacheable && isMemberExp)) {
// wrap inline statement in a function expression // wrap inline statement in a function expression
exp = createCompoundExpression([ exp = createCompoundExpression([
`($event, ...args) => ${hasMultipleStatements ? `{` : `(`}`, `${isInlineStatement ? `$event` : `($event, ...args)`} => ${
hasMultipleStatements ? `{` : `(`
}`,
exp, exp,
hasMultipleStatements ? `}` : `)` hasMultipleStatements ? `}` : `)`
]) ])