Skip to content

Commit 3f62257

Browse files
committed
fix(compiler-vapor): improve expression caching for shared member roots
1 parent 8cb33d7 commit 3f62257

File tree

3 files changed

+30
-3
lines changed

3 files changed

+30
-3
lines changed

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,22 @@ export function render(_ctx) {
238238
}"
239239
`;
240240
241+
exports[`cache multiple access > shared member root 1`] = `
242+
"import { setProp as _setProp, renderEffect as _renderEffect, template as _template } from 'vue';
243+
const t0 = _template("<div></div>")
244+
245+
export function render(_ctx) {
246+
const n0 = t0()
247+
const n1 = t0()
248+
_renderEffect(() => {
249+
const _foo = _ctx.foo
250+
_setProp(n0, "id", _foo.bar)
251+
_setProp(n1, "id", _foo.baz)
252+
})
253+
return [n0, n1]
254+
}"
255+
`;
256+
241257
exports[`cache multiple access > variable name substring edge cases 1`] = `
242258
"import { setProp as _setProp, renderEffect as _renderEffect, template as _template } from 'vue';
243259
const t0 = _template("<div></div>", true)

packages/compiler-vapor/__tests__/transforms/vBind.spec.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -853,6 +853,17 @@ describe('cache multiple access', () => {
853853
expect(code).contains('_setProp(n0, "id", _obj.foo + _obj.bar)')
854854
})
855855

856+
test('shared member root', () => {
857+
const { code } = compileWithVBind(`
858+
<div :id="foo.bar"></div>
859+
<div :id="foo.baz"></div>
860+
`)
861+
expect(code).matchSnapshot()
862+
expect(code).contains('const _foo = _ctx.foo')
863+
expect(code).contains('_setProp(n0, "id", _foo.bar)')
864+
expect(code).contains('_setProp(n1, "id", _foo.baz)')
865+
})
866+
856867
test('not cache variable only used in property shorthand', () => {
857868
const { code } = compileWithVBind(`
858869
<div :style="{color}" />

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -492,10 +492,10 @@ function shouldDeclareVariable(
492492
}
493493
return true
494494
}
495-
// if arrays share common elements, no declaration needed
496-
// because they will be treat as repeated expressions
495+
// if arrays are identical, no declaration needed
496+
// because they will be treated as repeated expressions
497497
// e.g., [[foo,bar],[foo,bar]] -> const foo_bar = _ctx.foo + _ctx.bar
498-
if (vars.some(v => v.some(e => first.includes(e)))) {
498+
if (vars.every(v => v.every((e, idx) => e === first[idx]))) {
499499
return false
500500
}
501501

0 commit comments

Comments
 (0)