Skip to content

Commit 9e47f5a

Browse files
committed
fix(compiler-vapor): handle TSNonNullExpression correctly in expression caching
1 parent 3f62257 commit 9e47f5a

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: 17 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
}"
@@ -254,6 +254,22 @@ export function render(_ctx) {
254254
}"
255255
`;
256256
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+
257273
exports[`cache multiple access > variable name substring edge cases 1`] = `
258274
"import { setProp as _setProp, renderEffect as _renderEffect, template as _template } from 'vue';
259275
const t0 = _template("<div></div>", true)

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

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -850,7 +850,7 @@ 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)')
854854
})
855855

856856
test('shared member root', () => {
@@ -864,6 +864,17 @@ describe('cache multiple access', () => {
864864
expect(code).contains('_setProp(n1, "id", _foo.baz)')
865865
})
866866

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)')
876+
})
877+
867878
test('not cache variable only used in property shorthand', () => {
868879
const { code } = compileWithVBind(`
869880
<div :style="{color}" />

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -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)