Skip to content
Open
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
42 changes: 22 additions & 20 deletions packages/compiler-vapor/src/generators/for.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -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
Expand All @@ -353,17 +348,17 @@ function matchPatterns(

function matchKeyOnlyBindingPattern(
effect: IREffect,
keyAst: any,
key: string,
):
| {
effect: IREffect
}
| 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 }
}
}
Expand All @@ -372,7 +367,7 @@ function matchKeyOnlyBindingPattern(

function matchSelectorPattern(
effect: IREffect,
keyAst: any,
key: string,
idMap: Record<string, string | SimpleExpressionNode | null>,
):
| {
Expand All @@ -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][] = []

Expand All @@ -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])
Expand Down Expand Up @@ -449,7 +452,6 @@ function matchSelectorPattern(
}
}

const content = effect.expressions[0].content
if (
typeof ast === 'object' &&
ast &&
Expand All @@ -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 {
Expand Down Expand Up @@ -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
}
Expand Down
Loading