From 4c3e7e331a8f8f4a7b91a131665cc74bf035a019 Mon Sep 17 00:00:00 2001 From: Evan You Date: Thu, 19 Sep 2019 13:48:32 -0400 Subject: [PATCH] refactor: cleanup unused variables in vFor --- packages/compiler-core/src/directives/vFor.ts | 58 ++++++++----------- 1 file changed, 24 insertions(+), 34 deletions(-) diff --git a/packages/compiler-core/src/directives/vFor.ts b/packages/compiler-core/src/directives/vFor.ts index 201adfa5..bd961f39 100644 --- a/packages/compiler-core/src/directives/vFor.ts +++ b/packages/compiler-core/src/directives/vFor.ts @@ -1,5 +1,5 @@ -import { createDirectiveTransform, TransformContext } from '../transform' -import { NodeTypes, ExpressionNode, Node, SourceLocation } from '../ast' +import { createDirectiveTransform } from '../transform' +import { NodeTypes, ExpressionNode, Node } from '../ast' import { createCompilerError, ErrorCodes } from '../errors' import { getInnerRange } from '../utils' @@ -16,16 +16,13 @@ export const transformFor = createDirectiveTransform( context.replaceNode({ type: NodeTypes.FOR, loc: node.loc, - source: createExpression(aliases.source, dir.exp, context), - valueAlias: aliases.value - ? createExpression(aliases.value, dir.exp, context) - : undefined, - keyAlias: aliases.key - ? createExpression(aliases.key, dir.exp, context) - : undefined, - objectIndexAlias: aliases.index - ? createExpression(aliases.index, dir.exp, context) - : undefined, + source: maybeCreateExpression( + aliases.source, + dir.exp + ) as ExpressionNode, + valueAlias: maybeCreateExpression(aliases.value, dir.exp), + keyAlias: maybeCreateExpression(aliases.key, dir.exp), + objectIndexAlias: maybeCreateExpression(aliases.index, dir.exp), children: [node] }) } else { @@ -44,25 +41,6 @@ export const transformFor = createDirectiveTransform( } ) -function createExpression( - alias: AliasExpression, - node: Node, - context: TransformContext -): ExpressionNode { - const loc: SourceLocation = getInnerRange( - node.loc, - alias.offset, - alias.content.length - ) - - return { - type: NodeTypes.EXPRESSION, - loc: loc, - content: alias.content, - isStatic: false - } -} - interface AliasExpression { offset: number content: string @@ -75,13 +53,11 @@ interface AliasExpressions { index: AliasExpression | undefined } -function parseAliasExpressions(source: string): null | AliasExpressions { +function parseAliasExpressions(source: string): AliasExpressions | null { const inMatch = source.match(forAliasRE) - if (!inMatch) return null const [, LHS, RHS] = inMatch - const result: AliasExpressions = { source: { offset: source.indexOf(RHS, LHS.length), @@ -135,3 +111,17 @@ function parseAliasExpressions(source: string): null | AliasExpressions { return result } + +function maybeCreateExpression( + alias: AliasExpression | undefined, + node: Node +): ExpressionNode | undefined { + if (alias) { + return { + type: NodeTypes.EXPRESSION, + loc: getInnerRange(node.loc, alias.offset, alias.content.length), + content: alias.content, + isStatic: false + } + } +}