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

feat(compiler-core): enhance fnExpRE #9316

Closed
wants to merge 2 commits into from
Closed
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
129 changes: 129 additions & 0 deletions packages/compiler-core/__tests__/transforms/vOn.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -437,6 +437,135 @@ describe('compiler: transform v-on', () => {
})
})

test('should handle inline arrow function expression wrapped in parentheses', () => {
const { node } = parseWithVOn(`<div @click="(foo => bar = foo)" />`)
const vnodeCall = node.codegenNode as VNodeCall
expect(
(vnodeCall.props as ObjectExpression).properties[0].value
).toMatchObject({
type: NodeTypes.SIMPLE_EXPRESSION,
content: '(foo => bar = foo)'
})
})

test('should handle inline arrow function expression wrapped in parentheses (with Typescript)', () => {
const { node } = parseWithVOn(
`<div @click="((foo: any): any => bar = foo)" />`
)
const vnodeCall = node.codegenNode as VNodeCall
expect(
(vnodeCall.props as ObjectExpression).properties[0].value
).toMatchObject({
type: NodeTypes.SIMPLE_EXPRESSION,
content: '((foo: any): any => bar = foo)'
})
})

test('should handle inline function expression wrapped in parentheses', () => {
const { node } = parseWithVOn(
`<div @click="(function (foo) { bar = foo })" />`
)
const vnodeCall = node.codegenNode as VNodeCall
expect(
(vnodeCall.props as ObjectExpression).properties[0].value
).toMatchObject({
type: NodeTypes.SIMPLE_EXPRESSION,
content: '(function (foo) { bar = foo })'
})
})

test('should handle inline function expression wrapped in parentheses (with Typescript)', () => {
const { node } = parseWithVOn(
`<div @click="(function (foo: any): any { bar = foo })" />`
)
const vnodeCall = node.codegenNode as VNodeCall
expect(
(vnodeCall.props as ObjectExpression).properties[0].value
).toMatchObject({
type: NodeTypes.SIMPLE_EXPRESSION,
content: '(function (foo: any): any { bar = foo })'
})
})

test('should handle inline async arrow function expression with parameters', () => {
const { node } = parseWithVOn(
`<div @click="async foo => await fetch(foo)" />`
)
const vnodeCall = node.codegenNode as VNodeCall
expect(
(vnodeCall.props as ObjectExpression).properties[0].value
).toMatchObject({
type: NodeTypes.SIMPLE_EXPRESSION,
content: 'async foo => await fetch(foo)'
})
})

test('should handle inline async arrow function expression with parameters (with Typescript)', () => {
const { node } = parseWithVOn(
`<div @click="async (foo: any): Promise<any> => await fetch(foo)" />`
)
const vnodeCall = node.codegenNode as VNodeCall
expect(
(vnodeCall.props as ObjectExpression).properties[0].value
).toMatchObject({
type: NodeTypes.SIMPLE_EXPRESSION,
content: 'async (foo: any): Promise<any> => await fetch(foo)'
})
})

test('should handle inline async arrow function expression with parameters wrapped in parentheses', () => {
const { node } = parseWithVOn(
`<div @click="(async foo => await fetch(foo))" />`
)
const vnodeCall = node.codegenNode as VNodeCall
expect(
(vnodeCall.props as ObjectExpression).properties[0].value
).toMatchObject({
type: NodeTypes.SIMPLE_EXPRESSION,
content: '(async foo => await fetch(foo))'
})
})

test('should handle inline async arrow function expression with parameters wrapped in parentheses (with Typescript)', () => {
const { node } = parseWithVOn(
`<div @click="(async (foo: any): Promise<any> => await fetch(foo))" />`
)
const vnodeCall = node.codegenNode as VNodeCall
expect(
(vnodeCall.props as ObjectExpression).properties[0].value
).toMatchObject({
type: NodeTypes.SIMPLE_EXPRESSION,
content: '(async (foo: any): Promise<any> => await fetch(foo))'
})
})

test('should handle inline async arrow function expression with parameters wrapped in parentheses', () => {
const { node } = parseWithVOn(
`<div @click="(async function (foo) {\nawait fetch(foo)\nbar = foo\n})" />`
)
const vnodeCall = node.codegenNode as VNodeCall
expect(
(vnodeCall.props as ObjectExpression).properties[0].value
).toMatchObject({
type: NodeTypes.SIMPLE_EXPRESSION,
content: '(async function (foo) {\nawait fetch(foo)\nbar = foo\n})'
})
})

test('should handle inline async arrow function expression with parameters wrapped in parentheses (with Typescript)', () => {
const { node } = parseWithVOn(
`<div @click="(async function (foo: any): Promise<any> {\nawait fetch(foo)\nbar = foo\n}})" />`
)
const vnodeCall = node.codegenNode as VNodeCall
expect(
(vnodeCall.props as ObjectExpression).properties[0].value
).toMatchObject({
type: NodeTypes.SIMPLE_EXPRESSION,
content:
'(async function (foo: any): Promise<any> {\nawait fetch(foo)\nbar = foo\n}})'
})
})

// TODO remove in 3.4
test('case conversion for vnode hooks', () => {
const { node } = parseWithVOn(`<div v-on:vnode-mounted="onMount"/>`)
Expand Down
2 changes: 1 addition & 1 deletion packages/compiler-core/src/transforms/vOn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import { hasScopeRef, isMemberExpression } from '../utils'
import { TO_HANDLER_KEY } from '../runtimeHelpers'

const fnExpRE =
/^\s*([\w$_]+|(async\s*)?\([^)]*?\))\s*(:[^=]+)?=>|^\s*(async\s+)?function(?:\s+[\w$]+)?\s*\(/
/(?:^[\s\(]*(?:(?:async\s*)?(?:[\w$_]+|\([^)]*?\))\s*(?::[^=]+)?=>|(?:async\s+)?function(?:\s+[\w$_]+)?\s*\([^)]*?\)\s*(?::[^=]+)?{))[\s\S]+\)*/

export interface VOnDirectiveNode extends DirectiveNode {
// v-on without arg is handled directly in ./transformElements.ts due to it affecting
Expand Down