Skip to content

Commit 52ee973

Browse files
committed
Chore: small refactoring
1 parent 3b7dced commit 52ee973

File tree

1 file changed

+9
-28
lines changed

1 file changed

+9
-28
lines changed

Diff for: src/script/index.ts

+9-28
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import sortedIndexBy from "lodash/sortedIndexBy"
99
import {
1010
traverseNodes,
1111
ESLintArrayPattern,
12-
ESLintBlockStatement,
1312
ESLintCallExpression,
1413
ESLintExpression,
1514
ESLintExpressionStatement,
@@ -132,24 +131,6 @@ function normalizeLeft(
132131
return [id]
133132
}
134133

135-
/**
136-
* Remove references by name.
137-
* @param references The array of references to remove.
138-
* @param name The name of target references.
139-
*/
140-
function removeByName(references: Reference[], name: string): void {
141-
let i = 0
142-
while (i < references.length) {
143-
const reference = references[i]
144-
145-
if (reference.id.name === name) {
146-
references.splice(i, 1)
147-
} else {
148-
i += 1
149-
}
150-
}
151-
}
152-
153134
/**
154135
* Get the comma token before a given node.
155136
* @param tokens The token list.
@@ -500,20 +481,23 @@ export function parseVOnExpression(
500481
locationCalculator: LocationCalculator,
501482
parserOptions: any,
502483
): ExpressionParseResult {
503-
debug('[script] parse v-on expression: "{%s}"', code)
484+
debug('[script] parse v-on expression: "void function($event){%s}"', code)
504485

505486
if (code.trim() === "") {
506487
throwEmptyError(locationCalculator, "statements")
507488
}
508489

509490
try {
510491
const ast = parseScriptFragment(
511-
`{${code}}`,
512-
locationCalculator.getSubCalculatorAfter(-1),
492+
`void function($event){${code}}`,
493+
locationCalculator.getSubCalculatorAfter(-22),
513494
parserOptions,
514495
).ast
515496
const references = analyzeExternalReferences(ast, parserOptions)
516-
const block = ast.body[0] as ESLintBlockStatement
497+
const outermostStatement = ast.body[0] as ESLintExpressionStatement
498+
const functionDecl = (outermostStatement.expression as ESLintUnaryExpression)
499+
.argument as ESLintFunctionExpression
500+
const block = functionDecl.body
517501
const body = block.body
518502
const firstStatement = first(body)
519503
const lastStatement = last(body)
@@ -548,13 +532,10 @@ export function parseVOnExpression(
548532
b.parent = expression
549533
}
550534

551-
// Remvoe braces.
552-
tokens.shift()
535+
// Remove braces.
536+
tokens.splice(0, 6)
553537
tokens.pop()
554538

555-
// Remove $event: https://vuejs.org/v2/api/#v-on
556-
removeByName(references, "$event")
557-
558539
return { expression, tokens, comments, references, variables: [] }
559540
} catch (err) {
560541
return throwErrorAsAdjustingOutsideOfCode(err, code, locationCalculator)

0 commit comments

Comments
 (0)