Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 6ef0f10

Browse files
authored
perf(compiler-vapor): omit redundant nthChild logical index (#14848)
1 parent b2db8e5 commit 6ef0f10

6 files changed

Lines changed: 52 additions & 9 deletions

File tree

‎packages/compiler-vapor/__tests__/__snapshots__/compile.spec.ts.snap‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -269,7 +269,7 @@ const t1 = _template("<div><div></div><!><div></div><!><div><button>", 1)
269269
export function render(_ctx) {
270270
const n6 = t1()
271271
const n5 = _next(_child(n6), 1)
272-
const n7 = _nthChild(n6, 3, 3)
272+
const n7 = _nthChild(n6, 3)
273273
const p0 = _next(n7, 4)
274274
const n4 = _child(p0)
275275
_setInsertionState(n6, n5, 1)
@@ -307,7 +307,7 @@ export function render(_ctx) {
307307
const n3 = t0()
308308
const n0 = _child(n3)
309309
const n1 = _next(n0, 1)
310-
const n2 = _nthChild(n3, 3, 3)
310+
const n2 = _nthChild(n3, 3)
311311
const x0 = _txt(n0)
312312
_setText(x0, _toDisplayString(_ctx.foo))
313313
_renderEffect(() => {

‎packages/compiler-vapor/__tests__/transforms/__snapshots__/logicalIndex.spec.ts.snap‎

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,26 @@ export function render(_ctx) {
5151
}"
5252
`;
5353
54+
exports[`compiler: logicalIndex > child/nthChild/next with logicalIndex > nthChild keeps logicalIndex when it differs from element index 1`] = `
55+
"import { resolveComponent as _resolveComponent, setInsertionState as _setInsertionState, createComponentWithFallback as _createComponentWithFallback, child as _child, nthChild as _nthChild, createIf as _createIf, template as _template } from 'vue';
56+
const t0 = _template("<div>", 2)
57+
const t1 = _template("<div><div></div><span></span><!><div><button>", 1)
58+
59+
export function render(_ctx) {
60+
const _component_Comp = _resolveComponent("Comp")
61+
const n4 = t1()
62+
const n5 = _nthChild(n4, 2, 3)
63+
_setInsertionState(n4, 0, 0)
64+
const n0 = _createComponentWithFallback(_component_Comp)
65+
_setInsertionState(n4, n5, 3)
66+
const n1 = _createIf(() => (true), () => {
67+
const n3 = t0()
68+
return n3
69+
}, null, 1, true)
70+
return n4
71+
}"
72+
`;
73+
5474
exports[`compiler: logicalIndex > child/nthChild/next with logicalIndex > nthChild with logicalIndex 1`] = `
5575
"import { child as _child, next as _next, setInsertionState as _setInsertionState, createAssetComponent as _createAssetComponent, nthChild as _nthChild, createIf as _createIf, template as _template } from 'vue';
5676
const t0 = _template("<div>", 2)
@@ -59,7 +79,7 @@ const t1 = _template("<div><div></div><!><div></div><!><div><button>", 1)
5979
export function render(_ctx) {
6080
const n5 = t1()
6181
const n4 = _next(_child(n5), 1)
62-
const n6 = _nthChild(n5, 3, 3)
82+
const n6 = _nthChild(n5, 3)
6383
_setInsertionState(n5, n4, 1)
6484
const n0 = _createAssetComponent("Comp")
6585
_setInsertionState(n5, n6, 3)

‎packages/compiler-vapor/__tests__/transforms/__snapshots__/transformChildren.spec.ts.snap‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ const t0 = _template("<div><div>x</div><div>x</div><div> ", 1)
4343
4444
export function render(_ctx) {
4545
const n1 = t0()
46-
const n0 = _nthChild(n1, 2, 2)
46+
const n0 = _nthChild(n1, 2)
4747
const x0 = _txt(n0)
4848
_renderEffect(() => _setText(x0, _toDisplayString(_ctx.msg)))
4949
return n1

‎packages/compiler-vapor/__tests__/transforms/logicalIndex.spec.ts‎

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,27 @@ describe('compiler: logicalIndex', () => {
5757
</div>
5858
</div>
5959
`)
60-
// nthChild(parent, elementIndex=3, logicalIndex=3) for the anchor
61-
expect(code).toContain('_nthChild(n5, 3, 3)')
60+
// nthChild(parent, elementIndex=3) for the anchor
61+
expect(code).toContain('_nthChild(n5, 3)')
62+
expect(code).toMatchSnapshot()
63+
})
64+
65+
test('nthChild keeps logicalIndex when it differs from element index', () => {
66+
// <div><Comp /><div /><span /><div v-if="true" /><div><button :disabled="foo" /></div></div>
67+
// Comp is prepended and not part of the template, so the v-if append
68+
// anchor is elementIndex=2 but logicalIndex=3.
69+
const { code } = compileWithTransforms(`
70+
<div>
71+
<Comp />
72+
<div />
73+
<span />
74+
<div v-if="true" />
75+
<div>
76+
<button :disabled="foo" />
77+
</div>
78+
</div>
79+
`)
80+
expect(code).toMatch(/_nthChild\(n\d+, 2, 3\)/)
6281
expect(code).toMatchSnapshot()
6382
})
6483

‎packages/compiler-vapor/__tests__/transforms/transformChildren.spec.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ describe('compiler: children transform', () => {
5656
<div>{{ msg }}</div>
5757
</div>`,
5858
)
59-
expect(code).contains(`const n0 = _nthChild(n1, 2, 2)`)
59+
expect(code).contains(`const n0 = _nthChild(n1, 2)`)
6060
expect(code).toMatchSnapshot()
6161
})
6262

‎packages/compiler-vapor/src/generators/template.ts‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,10 @@ export function genChildren(
110110
const elementIndex = index + offset
111111
const logicalIndex =
112112
child.logicalIndex !== undefined ? String(child.logicalIndex) : undefined
113+
// nthChild defaults the logical index to the element index at runtime, so
114+
// the third argument is only needed when hydration uses a different index.
115+
const nthChildLogicalIndex =
116+
child.logicalIndex === elementIndex ? undefined : logicalIndex
113117
// p for "placeholder" variables that are meant for possible reuse by
114118
// other access paths
115119
const variable =
@@ -125,7 +129,7 @@ export function genChildren(
125129
helper('nthChild'),
126130
from,
127131
String(elementIndex),
128-
logicalIndex,
132+
nthChildLogicalIndex,
129133
),
130134
)
131135
}
@@ -148,7 +152,7 @@ export function genChildren(
148152
helper('nthChild'),
149153
from,
150154
String(elementIndex),
151-
logicalIndex,
155+
nthChildLogicalIndex,
152156
)
153157
}
154158
pushBlock(...init)

0 commit comments

Comments
 (0)