Skip to content

fix(compiler-vapor): properly cache variable used in object property shorthand #12815

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Feb 28, 2025
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
@@ -1,5 +1,20 @@
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html

exports[`cache multiple access > cache variable used in both property shorthand and normal binding 1`] = `
"import { setStyle as _setStyle, 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 _color = _ctx.color
_setStyle(n0, {color: _color})
_setProp(n0, "id", _color)
})
return n0
}"
`;

exports[`cache multiple access > dynamic key bindings with expressions 1`] = `
"import { setDynamicProps as _setDynamicProps, renderEffect as _renderEffect, template as _template } from 'vue';
const t0 = _template("<div></div>", true)
Expand Down Expand Up @@ -60,6 +75,17 @@ export function render(_ctx) {
}"
`;

exports[`cache multiple access > not cache variable only used in property shorthand 1`] = `
"import { setStyle as _setStyle, renderEffect as _renderEffect, template as _template } from 'vue';
const t0 = _template("<div></div>", true)

export function render(_ctx) {
const n0 = t0()
_renderEffect(() => _setStyle(n0, {color: _ctx.color}))
return n0
}"
`;

exports[`cache multiple access > object property chain access 1`] = `
"import { setProp as _setProp, renderEffect as _renderEffect, template as _template } from 'vue';
const t0 = _template("<div></div>")
Expand Down
17 changes: 17 additions & 0 deletions packages/compiler-vapor/__tests__/transforms/vBind.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -785,6 +785,23 @@ describe('cache multiple access', () => {
expect(code).contains('_setProp(n0, "id", _obj[1][_ctx.baz] + _obj.bar)')
})

test('cache variable used in both property shorthand and normal binding', () => {
const { code } = compileWithVBind(`
<div :style="{color}" :id="color"/>
`)
expect(code).matchSnapshot()
expect(code).contains('const _color = _ctx.color')
expect(code).contains('_setStyle(n0, {color: _color})')
})

test('not cache variable only used in property shorthand', () => {
const { code } = compileWithVBind(`
<div :style="{color}" />
`)
expect(code).matchSnapshot()
expect(code).not.contains('const _color = _ctx.color')
})

test('not cache variable and member expression with the same name', () => {
const { code } = compileWithVBind(`
<div :id="bar + obj.bar"></div>
Expand Down
18 changes: 16 additions & 2 deletions packages/compiler-vapor/src/generators/expression.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,11 @@ function genIdentifier(
if (idMap && idMap.length) {
const replacement = idMap[0]
if (isString(replacement)) {
return [[replacement, NewlineType.None, loc]]
if (parent && parent.type === 'ObjectProperty' && parent.shorthand) {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

const frags = context.withId(() => genEffect(effect, context), ids)

this will rewrite identifiers used in the effect with passed in ids (due to #12568), and {foo} should be replaced with {foo:_ctx.foo} instead of {_ctx.foo}

return [[`${name}: ${replacement}`, NewlineType.None, loc]]
} else {
return [[replacement, NewlineType.None, loc]]
}
} else {
// replacement is an expression - process it again
return genExpression(replacement, context, assignment)
Expand Down Expand Up @@ -292,7 +296,7 @@ function analyzeExpressions(expressions: SimpleExpressionNode[]) {
}

walk(exp.ast, {
enter(currentNode: Node) {
enter(currentNode: Node, parent: Node | null) {
if (currentNode.type === 'MemberExpression') {
const memberExp = extractMemberExpression(
currentNode,
Expand All @@ -304,6 +308,16 @@ function analyzeExpressions(expressions: SimpleExpressionNode[]) {
return this.skip()
}

// skip shorthand or non-computed property keys
if (
parent &&
parent.type === 'ObjectProperty' &&
parent.key === currentNode &&
(parent.shorthand || !parent.computed)
) {
return this.skip()
}

if (currentNode.type === 'Identifier') {
registerVariable(currentNode.name, exp, true)
}
Expand Down
Loading