Skip to content

Fixed syntax error when function expression on v-on. #35

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

Merged
merged 5 commits into from
Dec 31, 2018
Merged
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
23 changes: 23 additions & 0 deletions src/script/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,11 @@ import {
const ALIAS_PARENS = /^(\s*)\(([\s\S]+)\)(\s*(?:in|of)\b[\s\S]+)$/u
const DUMMY_PARENT: any = {}

// Like Vue, it judges whether it is a function expression or not.
// https://github.com/vuejs/vue/blob/0948d999f2fddf9f90991956493f976273c5da1f/src/compiler/codegen/events.js#L3
const IS_FUNCTION_EXPRESSION = /^\s*([\w$_]+|\([^)]*?\))\s*=>|^function\s*\(/u
const IS_SIMPLE_PATH = /^[A-Za-z_$][\w$]*(?:\.[A-Za-z_$][\w$]*|\['[^']*?'\]|\["[^"]*?"\]|\[\d+\]|\[[A-Za-z_$][\w$]*\])*$/u

/**
* The interface of ESLint custom parsers.
*/
Expand Down Expand Up @@ -785,6 +790,24 @@ export function parseVOnExpression(
code: string,
locationCalculator: LocationCalculator,
parserOptions: any,
): ExpressionParseResult<ESLintExpression | VOnExpression> {
if (IS_FUNCTION_EXPRESSION.test(code) || IS_SIMPLE_PATH.test(code)) {
return parseExpressionBody(code, locationCalculator, parserOptions)
}
return parseVOnExpressionBody(code, locationCalculator, parserOptions)
}

/**
* Parse the source code of inline scripts.
* @param code The source code of inline scripts.
* @param locationCalculator The location calculator for the inline script.
* @param parserOptions The parser options.
* @returns The result of parsing.
*/
function parseVOnExpressionBody(
code: string,
locationCalculator: LocationCalculator,
parserOptions: any,
): ExpressionParseResult<VOnExpression> {
debug('[script] parse v-on expression: "void function($event){%s}"', code)

Expand Down
Loading