fix(compiler-core): handle unnamed function expressions with semicolons in v-on handlers#14783
fix(compiler-core): handle unnamed function expressions with semicolons in v-on handlers#14783Liuwei1125 wants to merge 1 commit intovuejs:mainfrom
Conversation
…ns in v-on handlers When a v-on handler contains a function expression with semicolons (e.g. @input="function () { ';' }"), the Babel parser incorrectly identifies it as a FunctionDeclaration (which requires a name) rather than a FunctionExpression. This fix adds a fallback in isFnExpressionNode: if the parsed result is an unnamed FunctionDeclaration, we wrap it in parentheses and try parsing again as an expression. Fixes vuejs#14287
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: defaults Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (1)
📝 WalkthroughWalkthroughEnhanced ChangesFunction Expression Detection Enhancement
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~20 minutes Poem
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
Tip 💬 Introducing Slack Agent: The best way for teams to turn conversations into code.Slack Agent is built on CodeRabbit's deep understanding of your code, so your team can collaborate across the entire SDLC without losing context.
Built for teams:
One agent for your entire SDLC. Right inside Slack. Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
|
duplicate of #14312 |
Summary
When a v-on handler contains a function expression with semicolons (e.g.
@input="function () { ';' }"), the Babel parser incorrectly identifies it as aFunctionDeclaration(which requires a name) rather than aFunctionExpression.This happens because Babel's
parseExpressiontreatsfunction () { ';' }as a statement (FunctionDeclaration without a name), which is invalid JavaScript. When this heuristic incorrectly classifies the handler as an inline statement, the generated code causes a parsing error.Fix
Added a fallback in
isFnExpressionNode: if the initial parse result is an unnamedFunctionDeclaration, we wrap it in parentheses and try parsing again as an expression. This correctly identifiesfunction () { ';' }as aFunctionExpression.Test case
Previously caused:
(2:18) Error parsing JavaScript expression: Unexpected token (1:10)Now works correctly without parsing errors.
Related
Fixes #14287
Summary by CodeRabbit