diff --git a/packages/compiler-vapor/src/generators/for.ts b/packages/compiler-vapor/src/generators/for.ts index 026db797ba6..8726c439c78 100644 --- a/packages/compiler-vapor/src/generators/for.ts +++ b/packages/compiler-vapor/src/generators/for.ts @@ -16,12 +16,7 @@ import { genCall, genMulti, } from './utils' -import { - type Expression, - type Identifier, - type Node, - isNodesEquivalent, -} from '@babel/types' +import type { Expression, Identifier, Node } from '@babel/types' import { parseExpression } from '@babel/parser' import { VaporVForFlags } from '../../../shared/src/vaporFlags' import { walk } from 'estree-walker' @@ -330,12 +325,12 @@ function matchPatterns( render.effect = render.effect.filter(effect => { if (keyProp !== undefined) { - const selector = matchSelectorPattern(effect, keyProp.ast, idMap) + const selector = matchSelectorPattern(effect, keyProp.content, idMap) if (selector) { selectorPatterns.push(selector) return false } - const keyOnly = matchKeyOnlyBindingPattern(effect, keyProp.ast) + const keyOnly = matchKeyOnlyBindingPattern(effect, keyProp.content) if (keyOnly) { keyOnlyBindingPatterns.push(keyOnly) return false @@ -353,7 +348,7 @@ function matchPatterns( function matchKeyOnlyBindingPattern( effect: IREffect, - keyAst: any, + key: string, ): | { effect: IREffect @@ -361,9 +356,9 @@ function matchKeyOnlyBindingPattern( | undefined { // TODO: expressions can be multiple? if (effect.expressions.length === 1) { - const ast = effect.expressions[0].ast + const { ast, content } = effect.expressions[0] if (typeof ast === 'object' && ast !== null) { - if (isKeyOnlyBinding(ast, keyAst)) { + if (isKeyOnlyBinding(ast, key, content)) { return { effect } } } @@ -372,7 +367,7 @@ function matchKeyOnlyBindingPattern( function matchSelectorPattern( effect: IREffect, - keyAst: any, + key: string, idMap: Record, ): | { @@ -382,7 +377,7 @@ function matchSelectorPattern( | undefined { // TODO: expressions can be multiple? if (effect.expressions.length === 1) { - const ast = effect.expressions[0].ast + const { ast, content } = effect.expressions[0] if (typeof ast === 'object' && ast) { const matcheds: [key: Expression, selector: Expression][] = [] @@ -400,8 +395,16 @@ function matchSelectorPattern( [left, right], [right, left], ]) { - const aIsKey = isKeyOnlyBinding(a, keyAst) - const bIsKey = isKeyOnlyBinding(b, keyAst) + const aIsKey = isKeyOnlyBinding( + a, + key, + effect.expressions[0].content, + ) + const bIsKey = isKeyOnlyBinding( + b, + key, + effect.expressions[0].content, + ) const bVars = analyzeVariableScopes(b, idMap) if (aIsKey && !bIsKey && !bVars.locals.length) { matcheds.push([a, b]) @@ -449,7 +452,6 @@ function matchSelectorPattern( } } - const content = effect.expressions[0].content if ( typeof ast === 'object' && ast && @@ -466,8 +468,8 @@ function matchSelectorPattern( [left, right], [right, left], ]) { - const aIsKey = isKeyOnlyBinding(a, keyAst) - const bIsKey = isKeyOnlyBinding(b, keyAst) + const aIsKey = isKeyOnlyBinding(a, key, content) + const bIsKey = isKeyOnlyBinding(b, key, content) const bVars = analyzeVariableScopes(b, idMap) if (aIsKey && !bIsKey && !bVars.locals.length) { return { @@ -520,11 +522,11 @@ function analyzeVariableScopes( return { globals, locals } } -function isKeyOnlyBinding(expr: Node, keyAst: any) { +function isKeyOnlyBinding(expr: Node, key: string, source: string) { let only = true walk(expr, { enter(node) { - if (isNodesEquivalent(node, keyAst)) { + if (source.slice(node.start! - 1, node.end! - 1) === key) { this.skip() return }