Skip to content

Commit 2c7bd42

Browse files
authored
fix(compiler-sfc): fix template expression assignment codegen for script setup let refs (#3626)
fix #3625
1 parent aa96a0e commit 2c7bd42

File tree

3 files changed

+14
-2
lines changed

3 files changed

+14
-2
lines changed

packages/compiler-core/src/transforms/transformExpression.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -143,14 +143,14 @@ export function processExpression(
143143
// let is a local non-ref value, and we need to replicate the
144144
// right hand side value.
145145
// x = y --> isRef(x) ? x.value = y : x = y
146-
const rVal = (parent as AssignmentExpression).right
146+
const { right: rVal, operator } = parent as AssignmentExpression
147147
const rExp = rawExp.slice(rVal.start! - 1, rVal.end! - 1)
148148
const rExpString = stringifyExpression(
149149
processExpression(createSimpleExpression(rExp, false), context)
150150
)
151151
return `${context.helperString(IS_REF)}(${raw})${
152152
context.isTS ? ` //@ts-ignore\n` : ``
153-
} ? ${raw}.value = ${rExpString} : ${raw}`
153+
} ? ${raw}.value ${operator} ${rExpString} : ${raw}`
154154
} else if (isUpdateArg) {
155155
// make id replace parent in the code range so the raw update operator
156156
// is removed

packages/compiler-sfc/__tests__/__snapshots__/compileScript.spec.ts.snap

+7
Original file line numberDiff line numberDiff line change
@@ -361,6 +361,7 @@ export default {
361361
const count = ref(0)
362362
const maybe = foo()
363363
let lett = 1
364+
let v = ref(1)
364365
365366
return (_ctx, _cache) => {
366367
return (_openBlock(), _createBlock(_Fragment, null, [
@@ -372,6 +373,12 @@ return (_ctx, _cache) => {
372373
}),
373374
_createVNode(\\"div\\", {
374375
onClick: _cache[3] || (_cache[3] = $event => (_isRef(lett) ? lett.value = count.value : lett = count.value))
376+
}),
377+
_createVNode(\\"div\\", {
378+
onClick: _cache[4] || (_cache[4] = $event => (_isRef(v) ? v.value += 1 : v += 1))
379+
}),
380+
_createVNode(\\"div\\", {
381+
onClick: _cache[5] || (_cache[5] = $event => (_isRef(v) ? v.value -= 1 : v -= 1))
375382
})
376383
], 64 /* STABLE_FRAGMENT */))
377384
}

packages/compiler-sfc/__tests__/compileScript.spec.ts

+5
Original file line numberDiff line numberDiff line change
@@ -300,11 +300,14 @@ const myEmit = defineEmit(['foo', 'bar'])
300300
const count = ref(0)
301301
const maybe = foo()
302302
let lett = 1
303+
let v = ref(1)
303304
</script>
304305
<template>
305306
<div @click="count = 1"/>
306307
<div @click="maybe = count"/>
307308
<div @click="lett = count"/>
309+
<div @click="v += 1"/>
310+
<div @click="v -= 1"/>
308311
</template>
309312
`,
310313
{ inlineTemplate: true }
@@ -317,6 +320,8 @@ const myEmit = defineEmit(['foo', 'bar'])
317320
expect(content).toMatch(
318321
`_isRef(lett) ? lett.value = count.value : lett = count.value`
319322
)
323+
expect(content).toMatch(`_isRef(v) ? v.value += 1 : v += 1`)
324+
expect(content).toMatch(`_isRef(v) ? v.value -= 1 : v -= 1`)
320325
assertCode(content)
321326
})
322327

0 commit comments

Comments
 (0)