-
-
Notifications
You must be signed in to change notification settings - Fork 9k
Open
Labels
Description
Vue version
3.5.26
Link to minimal reproduction
Steps to reproduce
Any function expression with semicolon inside it:
<template>
<input @input="function () { ';' }" />
</template>What is expected?
No parsing errors.
What is actually happening?
(2:18) Error parsing JavaScript expression: Unexpected token (1:10)
When an event handler's source code string contains at least one semicolon (;), the handler is parsed as a statement. However, in JS, function declarations must have a name function DECL() {}. When the handler doesn't contain a semicolon, it's parsed as an expression; in that case a function expression may not have a name function () {}, so no parsing error is raised.
| const hasMultipleStatements = exp.content.includes(`;`) |
Workarounds:
- Use an arrow functions
@input="() => { ';' }" - Wrap the function in parens (enforcing it to be parsed as a function expression rather than a function declaration)
@input="(function () { ';' })"
What if, instead of using a heuristic (exp.content.includes(';')), the Vue parser tried to parse the event handler as an expression first (by wrapping the handler code in parens), and then, if a parser error occurred, fell back to parsing it as statements?
System Info
Any additional comments?
Doing so will also probably fix #8854
Reactions are currently unavailable