mirror of https://github.com/vuejs/core.git
fix(compiler-vapor): properly cache variable with optional chaining (#13519)
This commit is contained in:
parent
c86bf7b11f
commit
48a1370405
|
@ -113,6 +113,20 @@ export function render(_ctx) {
|
||||||
}"
|
}"
|
||||||
`;
|
`;
|
||||||
|
|
||||||
|
exports[`cache multiple access > optional chaining 1`] = `
|
||||||
|
"import { setProp as _setProp, renderEffect as _renderEffect, template as _template } from 'vue';
|
||||||
|
const t0 = _template("<div></div>", true)
|
||||||
|
|
||||||
|
export function render(_ctx) {
|
||||||
|
const n0 = t0()
|
||||||
|
_renderEffect(() => {
|
||||||
|
const _obj = _ctx.obj
|
||||||
|
_setProp(n0, "id", _obj?.foo + _obj?.bar)
|
||||||
|
})
|
||||||
|
return n0
|
||||||
|
}"
|
||||||
|
`;
|
||||||
|
|
||||||
exports[`cache multiple access > repeated expression in expressions 1`] = `
|
exports[`cache multiple access > repeated expression in expressions 1`] = `
|
||||||
"import { setProp as _setProp, renderEffect as _renderEffect, template as _template } from 'vue';
|
"import { setProp as _setProp, renderEffect as _renderEffect, template as _template } from 'vue';
|
||||||
const t0 = _template("<div></div>")
|
const t0 = _template("<div></div>")
|
||||||
|
|
|
@ -794,6 +794,13 @@ describe('cache multiple access', () => {
|
||||||
expect(code).contains('_setStyle(n0, {color: _color})')
|
expect(code).contains('_setStyle(n0, {color: _color})')
|
||||||
})
|
})
|
||||||
|
|
||||||
|
test('optional chaining', () => {
|
||||||
|
const { code } = compileWithVBind(`<div :id="obj?.foo + obj?.bar"></div>`)
|
||||||
|
expect(code).matchSnapshot()
|
||||||
|
expect(code).contains('const _obj = _ctx.obj')
|
||||||
|
expect(code).contains('_setProp(n0, "id", _obj?.foo + _obj?.bar)')
|
||||||
|
})
|
||||||
|
|
||||||
test('not cache variable only used in property shorthand', () => {
|
test('not cache variable only used in property shorthand', () => {
|
||||||
const { code } = compileWithVBind(`
|
const { code } = compileWithVBind(`
|
||||||
<div :style="{color}" />
|
<div :style="{color}" />
|
||||||
|
|
|
@ -588,6 +588,7 @@ function extractMemberExpression(
|
||||||
case 'CallExpression': // foo[bar(baz)]
|
case 'CallExpression': // foo[bar(baz)]
|
||||||
return `${extractMemberExpression(exp.callee, onIdentifier)}(${exp.arguments.map(arg => extractMemberExpression(arg, onIdentifier)).join(', ')})`
|
return `${extractMemberExpression(exp.callee, onIdentifier)}(${exp.arguments.map(arg => extractMemberExpression(arg, onIdentifier)).join(', ')})`
|
||||||
case 'MemberExpression': // foo[bar.baz]
|
case 'MemberExpression': // foo[bar.baz]
|
||||||
|
case 'OptionalMemberExpression': // foo?.bar
|
||||||
const object = extractMemberExpression(exp.object, onIdentifier)
|
const object = extractMemberExpression(exp.object, onIdentifier)
|
||||||
const prop = exp.computed
|
const prop = exp.computed
|
||||||
? `[${extractMemberExpression(exp.property, onIdentifier)}]`
|
? `[${extractMemberExpression(exp.property, onIdentifier)}]`
|
||||||
|
|
Loading…
Reference in New Issue