Skip to content

Commit 25ec4f4

Browse files
authored
fix(compiler-vapor): improve expression caching for shared member roots (#14132)
1 parent 8cb33d7 commit 25ec4f4

File tree

3 files changed

+60
-6
lines changed

3 files changed

+60
-6
lines changed

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

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ export function render(_ctx) {
88
const n0 = t0()
99
_renderEffect(() => {
1010
const _obj = _ctx.obj
11-
_setProp(n0, "id", _obj.foo + _obj.bar)
11+
_setProp(n0, "id", _obj!.foo + _obj!.bar)
1212
})
1313
return n0
1414
}"
@@ -238,6 +238,38 @@ 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+
257+
exports[`cache multiple access > shared member root with TSNonNullExpression 1`] = `
258+
"import { setProp as _setProp, renderEffect as _renderEffect, template as _template } from 'vue';
259+
const t0 = _template("<div></div>")
260+
261+
export function render(_ctx) {
262+
const n0 = t0()
263+
const n1 = t0()
264+
_renderEffect(() => {
265+
const _foo = _ctx.foo
266+
_setProp(n0, "id", _foo!.bar)
267+
_setProp(n1, "id", _foo!.baz)
268+
})
269+
return [n0, n1]
270+
}"
271+
`;
272+
241273
exports[`cache multiple access > variable name substring edge cases 1`] = `
242274
"import { setProp as _setProp, renderEffect as _renderEffect, template as _template } from 'vue';
243275
const t0 = _template("<div></div>", true)

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

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -850,7 +850,29 @@ describe('cache multiple access', () => {
850850
const { code } = compileWithVBind(`<div :id="obj!.foo + obj!.bar"></div>`)
851851
expect(code).matchSnapshot()
852852
expect(code).contains('const _obj = _ctx.obj')
853-
expect(code).contains('_setProp(n0, "id", _obj.foo + _obj.bar)')
853+
expect(code).contains('_setProp(n0, "id", _obj!.foo + _obj!.bar)')
854+
})
855+
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+
867+
test('shared member root with TSNonNullExpression', () => {
868+
const { code } = compileWithVBind(`
869+
<div :id="foo!.bar"></div>
870+
<div :id="foo!.baz"></div>
871+
`)
872+
expect(code).matchSnapshot()
873+
expect(code).contains('const _foo = _ctx.foo')
874+
expect(code).contains('_setProp(n0, "id", _foo!.bar)')
875+
expect(code).contains('_setProp(n1, "id", _foo!.baz)')
854876
})
855877

856878
test('not cache variable only used in property shorthand', () => {

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

Lines changed: 4 additions & 4 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

@@ -679,7 +679,7 @@ function extractMemberExpression(
679679
: `.${extractMemberExpression(exp.property, NOOP)}`
680680
return `${object}${prop}`
681681
case 'TSNonNullExpression': // foo!.bar
682-
return `${extractMemberExpression(exp.expression, onIdentifier)}!`
682+
return `${extractMemberExpression(exp.expression, onIdentifier)}`
683683
default:
684684
return ''
685685
}

0 commit comments

Comments
 (0)