Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export function render(_ctx) {
const n0 = t0()
_renderEffect(() => {
const _obj = _ctx.obj
_setProp(n0, "id", _obj.foo + _obj.bar)
_setProp(n0, "id", _obj!.foo + _obj!.bar)
})
return n0
}"
Expand Down Expand Up @@ -238,6 +238,38 @@ export function render(_ctx) {
}"
`;

exports[`cache multiple access > shared member root 1`] = `
"import { setProp as _setProp, renderEffect as _renderEffect, template as _template } from 'vue';
const t0 = _template("<div></div>")

export function render(_ctx) {
const n0 = t0()
const n1 = t0()
_renderEffect(() => {
const _foo = _ctx.foo
_setProp(n0, "id", _foo.bar)
_setProp(n1, "id", _foo.baz)
})
return [n0, n1]
}"
`;

exports[`cache multiple access > shared member root with TSNonNullExpression 1`] = `
"import { setProp as _setProp, renderEffect as _renderEffect, template as _template } from 'vue';
const t0 = _template("<div></div>")

export function render(_ctx) {
const n0 = t0()
const n1 = t0()
_renderEffect(() => {
const _foo = _ctx.foo
_setProp(n0, "id", _foo!.bar)
_setProp(n1, "id", _foo!.baz)
})
return [n0, n1]
}"
`;

exports[`cache multiple access > variable name substring edge cases 1`] = `
"import { setProp as _setProp, renderEffect as _renderEffect, template as _template } from 'vue';
const t0 = _template("<div></div>", true)
Expand Down
24 changes: 23 additions & 1 deletion packages/compiler-vapor/__tests__/transforms/vBind.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -850,7 +850,29 @@ describe('cache multiple access', () => {
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)')
expect(code).contains('_setProp(n0, "id", _obj!.foo + _obj!.bar)')
})

test('shared member root', () => {
const { code } = compileWithVBind(`
<div :id="foo.bar"></div>
<div :id="foo.baz"></div>
`)
expect(code).matchSnapshot()
expect(code).contains('const _foo = _ctx.foo')
expect(code).contains('_setProp(n0, "id", _foo.bar)')
expect(code).contains('_setProp(n1, "id", _foo.baz)')
})

test('shared member root with TSNonNullExpression', () => {
const { code } = compileWithVBind(`
<div :id="foo!.bar"></div>
<div :id="foo!.baz"></div>
`)
expect(code).matchSnapshot()
expect(code).contains('const _foo = _ctx.foo')
expect(code).contains('_setProp(n0, "id", _foo!.bar)')
expect(code).contains('_setProp(n1, "id", _foo!.baz)')
})

test('not cache variable only used in property shorthand', () => {
Expand Down
8 changes: 4 additions & 4 deletions packages/compiler-vapor/src/generators/expression.ts
Original file line number Diff line number Diff line change
Expand Up @@ -492,10 +492,10 @@ function shouldDeclareVariable(
}
return true
}
// if arrays share common elements, no declaration needed
// because they will be treat as repeated expressions
// if arrays are identical, no declaration needed
// because they will be treated as repeated expressions
// e.g., [[foo,bar],[foo,bar]] -> const foo_bar = _ctx.foo + _ctx.bar
if (vars.some(v => v.some(e => first.includes(e)))) {
if (vars.every(v => v.every((e, idx) => e === first[idx]))) {
return false
}

Expand Down Expand Up @@ -679,7 +679,7 @@ function extractMemberExpression(
: `.${extractMemberExpression(exp.property, NOOP)}`
return `${object}${prop}`
case 'TSNonNullExpression': // foo!.bar
return `${extractMemberExpression(exp.expression, onIdentifier)}!`
return `${extractMemberExpression(exp.expression, onIdentifier)}`
default:
return ''
}
Expand Down
Loading