Skip to content

Commit c0b756e

Browse files
committed
refactor(compiler-vapor): remove isNodesEquivalent to reduce bundle size
1 parent 3b5e13c commit c0b756e

1 file changed

Lines changed: 18 additions & 16 deletions

File tree

  • packages/compiler-vapor/src/generators

packages/compiler-vapor/src/generators/for.ts

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,7 @@ import {
1616
genCall,
1717
genMulti,
1818
} from './utils'
19-
import {
20-
type Expression,
21-
type Identifier,
22-
type Node,
23-
isNodesEquivalent,
24-
} from '@babel/types'
19+
import type { Expression, Identifier, Node } from '@babel/types'
2520
import { parseExpression } from '@babel/parser'
2621
import { VaporVForFlags } from '../../../shared/src/vaporFlags'
2722
import { walk } from 'estree-walker'
@@ -32,7 +27,7 @@ export function genFor(
3227
oper: ForIRNode,
3328
context: CodegenContext,
3429
): CodeFragment[] {
35-
const { helper } = context
30+
const { helper, ir } = context
3631
const {
3732
source,
3833
value,
@@ -98,6 +93,7 @@ export function genFor(
9893
render,
9994
keyProp,
10095
idMap,
96+
ir.source,
10197
)
10298
const selectorDeclarations: CodeFragment[] = []
10399
const selectorSetup: CodeFragment[] = []
@@ -320,6 +316,7 @@ function matchPatterns(
320316
render: BlockIRNode,
321317
keyProp: SimpleExpressionNode | undefined,
322318
idMap: Record<string, string | SimpleExpressionNode | null>,
319+
source: string,
323320
) {
324321
const selectorPatterns: NonNullable<
325322
ReturnType<typeof matchSelectorPattern>
@@ -330,12 +327,12 @@ function matchPatterns(
330327

331328
render.effect = render.effect.filter(effect => {
332329
if (keyProp !== undefined) {
333-
const selector = matchSelectorPattern(effect, keyProp.ast, idMap)
330+
const selector = matchSelectorPattern(effect, keyProp.ast, idMap, source)
334331
if (selector) {
335332
selectorPatterns.push(selector)
336333
return false
337334
}
338-
const keyOnly = matchKeyOnlyBindingPattern(effect, keyProp.ast)
335+
const keyOnly = matchKeyOnlyBindingPattern(effect, keyProp.ast, source)
339336
if (keyOnly) {
340337
keyOnlyBindingPatterns.push(keyOnly)
341338
return false
@@ -354,6 +351,7 @@ function matchPatterns(
354351
function matchKeyOnlyBindingPattern(
355352
effect: IREffect,
356353
keyAst: any,
354+
source: string,
357355
):
358356
| {
359357
effect: IREffect
@@ -363,7 +361,7 @@ function matchKeyOnlyBindingPattern(
363361
if (effect.expressions.length === 1) {
364362
const ast = effect.expressions[0].ast
365363
if (typeof ast === 'object' && ast !== null) {
366-
if (isKeyOnlyBinding(ast, keyAst)) {
364+
if (isKeyOnlyBinding(ast, keyAst, source)) {
367365
return { effect }
368366
}
369367
}
@@ -374,6 +372,7 @@ function matchSelectorPattern(
374372
effect: IREffect,
375373
keyAst: any,
376374
idMap: Record<string, string | SimpleExpressionNode | null>,
375+
source: string,
377376
):
378377
| {
379378
effect: IREffect
@@ -400,8 +399,8 @@ function matchSelectorPattern(
400399
[left, right],
401400
[right, left],
402401
]) {
403-
const aIsKey = isKeyOnlyBinding(a, keyAst)
404-
const bIsKey = isKeyOnlyBinding(b, keyAst)
402+
const aIsKey = isKeyOnlyBinding(a, keyAst, source)
403+
const bIsKey = isKeyOnlyBinding(b, keyAst, source)
405404
const bVars = analyzeVariableScopes(b, idMap)
406405
if (aIsKey && !bIsKey && !bVars.locals.length) {
407406
matcheds.push([a, b])
@@ -466,8 +465,8 @@ function matchSelectorPattern(
466465
[left, right],
467466
[right, left],
468467
]) {
469-
const aIsKey = isKeyOnlyBinding(a, keyAst)
470-
const bIsKey = isKeyOnlyBinding(b, keyAst)
468+
const aIsKey = isKeyOnlyBinding(a, keyAst, source)
469+
const bIsKey = isKeyOnlyBinding(b, keyAst, source)
471470
const bVars = analyzeVariableScopes(b, idMap)
472471
if (aIsKey && !bIsKey && !bVars.locals.length) {
473472
return {
@@ -520,11 +519,14 @@ function analyzeVariableScopes(
520519
return { globals, locals }
521520
}
522521

523-
function isKeyOnlyBinding(expr: Node, keyAst: any) {
522+
function isKeyOnlyBinding(expr: Node, keyAst: Node, source: string) {
524523
let only = true
525524
walk(expr, {
526525
enter(node) {
527-
if (isNodesEquivalent(node, keyAst)) {
526+
if (
527+
source.slice(node.start!, node.end!) ===
528+
source.slice(keyAst.start!, keyAst.end!)
529+
) {
528530
this.skip()
529531
return
530532
}

0 commit comments

Comments
 (0)