From 5abfe0274d7845db499eb8997507f7f3f6891b8a Mon Sep 17 00:00:00 2001 From: HcySunYang Date: Sun, 19 Jul 2020 23:41:58 +0800 Subject: [PATCH 1/2] fix(compiler-core): event handler of Multi-line writing should be processed correctly --- .../__tests__/transforms/vOn.spec.ts | 50 +++++++++++++++++++ packages/compiler-core/src/transforms/vOn.ts | 2 +- 2 files changed, 51 insertions(+), 1 deletion(-) diff --git a/packages/compiler-core/__tests__/transforms/vOn.spec.ts b/packages/compiler-core/__tests__/transforms/vOn.spec.ts index 873734226ab..7051b95597e 100644 --- a/packages/compiler-core/__tests__/transforms/vOn.spec.ts +++ b/packages/compiler-core/__tests__/transforms/vOn.spec.ts @@ -271,6 +271,56 @@ describe('compiler: transform v-on', () => { }) }) + test('should NOT wrap as function if expression is already function expression (Multi-line writing)', () => { + const { node } = parseWithVOn( + `
` + ) + expect((node.codegenNode as VNodeCall).props).toMatchObject({ + properties: [ + { + key: { content: `onClick` }, + value: { + type: NodeTypes.SIMPLE_EXPRESSION, + content: ` + $event => { + foo($event) + } + ` + } + } + ] + }) + }) + + test('should NOT wrap as function if expression is already function expression (Multi-line writing + function keyword)', () => { + const { node } = parseWithVOn( + `
` + ) + expect((node.codegenNode as VNodeCall).props).toMatchObject({ + properties: [ + { + key: { content: `onClick` }, + value: { + type: NodeTypes.SIMPLE_EXPRESSION, + content: ` + function($event) { + foo($event) + } + ` + } + } + ] + }) + }) + test('should NOT wrap as function if expression is complex member expression', () => { const { node } = parseWithVOn(`
`) expect((node.codegenNode as VNodeCall).props).toMatchObject({ diff --git a/packages/compiler-core/src/transforms/vOn.ts b/packages/compiler-core/src/transforms/vOn.ts index c577d52d4cf..75759708bd3 100644 --- a/packages/compiler-core/src/transforms/vOn.ts +++ b/packages/compiler-core/src/transforms/vOn.ts @@ -16,7 +16,7 @@ import { validateBrowserExpression } from '../validateExpression' import { isMemberExpression, hasScopeRef } from '../utils' import { CAPITALIZE } from '../runtimeHelpers' -const fnExpRE = /^([\w$_]+|\([^)]*?\))\s*=>|^function(?:\s+[\w$]+)?\s*\(/ +const fnExpRE = /^\s*([\w$_]+|\([^)]*?\))\s*=>|^\s*function(?:\s+[\w$]+)?\s*\(/ export interface VOnDirectiveNode extends DirectiveNode { // v-on without arg is handled directly in ./transformElements.ts due to it affecting From a797c7d3dcad05ff5f41a8a0eea86b609942e686 Mon Sep 17 00:00:00 2001 From: Evan You Date: Sun, 19 Jul 2020 11:48:05 -0400 Subject: [PATCH 2/2] Update vOn.spec.ts --- packages/compiler-core/__tests__/transforms/vOn.spec.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/compiler-core/__tests__/transforms/vOn.spec.ts b/packages/compiler-core/__tests__/transforms/vOn.spec.ts index 7051b95597e..19c44c3daee 100644 --- a/packages/compiler-core/__tests__/transforms/vOn.spec.ts +++ b/packages/compiler-core/__tests__/transforms/vOn.spec.ts @@ -271,7 +271,7 @@ describe('compiler: transform v-on', () => { }) }) - test('should NOT wrap as function if expression is already function expression (Multi-line writing)', () => { + test('should NOT wrap as function if expression is already function expression (with newlines)', () => { const { node } = parseWithVOn( `