Skip to content

Commit 4840849

Browse files
ota-meshimysticatea
authored andcommitted
🐛 fix syntax error when function expression on v-on. (#35)
1 parent e44fc8b commit 4840849

File tree

33 files changed

+6531
-0
lines changed

33 files changed

+6531
-0
lines changed

Diff for: src/script/index.ts

+23
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,11 @@ import {
4646
const ALIAS_PARENS = /^(\s*)\(([\s\S]+)\)(\s*(?:in|of)\b[\s\S]+)$/u
4747
const DUMMY_PARENT: any = {}
4848

49+
// Like Vue, it judges whether it is a function expression or not.
50+
// https://github.com/vuejs/vue/blob/0948d999f2fddf9f90991956493f976273c5da1f/src/compiler/codegen/events.js#L3
51+
const IS_FUNCTION_EXPRESSION = /^\s*([\w$_]+|\([^)]*?\))\s*=>|^function\s*\(/u
52+
const IS_SIMPLE_PATH = /^[A-Za-z_$][\w$]*(?:\.[A-Za-z_$][\w$]*|\['[^']*?'\]|\["[^"]*?"\]|\[\d+\]|\[[A-Za-z_$][\w$]*\])*$/u
53+
4954
/**
5055
* The interface of ESLint custom parsers.
5156
*/
@@ -807,6 +812,24 @@ export function parseVOnExpression(
807812
code: string,
808813
locationCalculator: LocationCalculator,
809814
parserOptions: any,
815+
): ExpressionParseResult<ESLintExpression | VOnExpression> {
816+
if (IS_FUNCTION_EXPRESSION.test(code) || IS_SIMPLE_PATH.test(code)) {
817+
return parseExpressionBody(code, locationCalculator, parserOptions)
818+
}
819+
return parseVOnExpressionBody(code, locationCalculator, parserOptions)
820+
}
821+
822+
/**
823+
* Parse the source code of inline scripts.
824+
* @param code The source code of inline scripts.
825+
* @param locationCalculator The location calculator for the inline script.
826+
* @param parserOptions The parser options.
827+
* @returns The result of parsing.
828+
*/
829+
function parseVOnExpressionBody(
830+
code: string,
831+
locationCalculator: LocationCalculator,
832+
parserOptions: any,
810833
): ExpressionParseResult<VOnExpression> {
811834
debug('[script] parse v-on expression: "void function($event){%s}"', code)
812835

0 commit comments

Comments
 (0)