fix(compiler-sfc): fix script setup ref assignment codegen edge case (#4520)
fix #4514
This commit is contained in:
@@ -157,7 +157,7 @@ return { props, bar }
|
||||
}"
|
||||
`;
|
||||
|
||||
exports[`SFC compile <script setup> defineProps/defineEmits in multi-variable decalration (full removal) 1`] = `
|
||||
exports[`SFC compile <script setup> defineProps/defineEmits in multi-variable declaration (full removal) 1`] = `
|
||||
"export default {
|
||||
props: ['item'],
|
||||
emits: ['a'],
|
||||
@@ -173,7 +173,7 @@ return { props, emit }
|
||||
}"
|
||||
`;
|
||||
|
||||
exports[`SFC compile <script setup> defineProps/defineEmits in multi-variable decalration 1`] = `
|
||||
exports[`SFC compile <script setup> defineProps/defineEmits in multi-variable declaration 1`] = `
|
||||
"export default {
|
||||
props: ['item'],
|
||||
emits: ['a'],
|
||||
@@ -506,7 +506,7 @@ return (_ctx, _push, _parent, _attrs) => {
|
||||
`;
|
||||
|
||||
exports[`SFC compile <script setup> inlineTemplate mode template assignment expression codegen 1`] = `
|
||||
"import { createElementVNode as _createElementVNode, isRef as _isRef, Fragment as _Fragment, openBlock as _openBlock, createElementBlock as _createElementBlock } from \\"vue\\"
|
||||
"import { createElementVNode as _createElementVNode, isRef as _isRef, unref as _unref, Fragment as _Fragment, openBlock as _openBlock, createElementBlock as _createElementBlock } from \\"vue\\"
|
||||
|
||||
import { ref } from 'vue'
|
||||
|
||||
@@ -534,6 +534,26 @@ return (_ctx, _cache) => {
|
||||
}),
|
||||
_createElementVNode(\\"div\\", {
|
||||
onClick: _cache[4] || (_cache[4] = $event => (_isRef(v) ? v.value -= 1 : v -= 1))
|
||||
}),
|
||||
_createElementVNode(\\"div\\", {
|
||||
onClick: _cache[5] || (_cache[5] = () => {
|
||||
let a = '' + _unref(lett)
|
||||
_isRef(v) ? v.value = a : v = a
|
||||
})
|
||||
}),
|
||||
_createElementVNode(\\"div\\", {
|
||||
onClick: _cache[6] || (_cache[6] = () => {
|
||||
// nested scopes
|
||||
(()=>{
|
||||
let x = _ctx.a
|
||||
(()=>{
|
||||
let z = x
|
||||
let z2 = z
|
||||
})
|
||||
let lz = _ctx.z
|
||||
})
|
||||
_isRef(v) ? v.value = _ctx.a : v = _ctx.a
|
||||
})
|
||||
})
|
||||
], 64 /* STABLE_FRAGMENT */))
|
||||
}
|
||||
|
||||
@@ -98,7 +98,7 @@ const myEmit = defineEmits(['foo', 'bar'])
|
||||
emits: ['foo', 'bar'],`)
|
||||
})
|
||||
|
||||
test('defineProps/defineEmits in multi-variable decalration', () => {
|
||||
test('defineProps/defineEmits in multi-variable declaration', () => {
|
||||
const { content } = compile(`
|
||||
<script setup>
|
||||
const props = defineProps(['item']),
|
||||
@@ -112,7 +112,7 @@ const myEmit = defineEmits(['foo', 'bar'])
|
||||
expect(content).toMatch(`emits: ['a'],`)
|
||||
})
|
||||
|
||||
test('defineProps/defineEmits in multi-variable decalration (full removal)', () => {
|
||||
test('defineProps/defineEmits in multi-variable declaration (full removal)', () => {
|
||||
const { content } = compile(`
|
||||
<script setup>
|
||||
const props = defineProps(['item']),
|
||||
@@ -517,6 +517,22 @@ defineExpose({ foo: 123 })
|
||||
<div @click="lett = count"/>
|
||||
<div @click="v += 1"/>
|
||||
<div @click="v -= 1"/>
|
||||
<div @click="() => {
|
||||
let a = '' + lett
|
||||
v = a
|
||||
}"/>
|
||||
<div @click="() => {
|
||||
// nested scopes
|
||||
(()=>{
|
||||
let x = a
|
||||
(()=>{
|
||||
let z = x
|
||||
let z2 = z
|
||||
})
|
||||
let lz = z
|
||||
})
|
||||
v = a
|
||||
}"/>
|
||||
</template>
|
||||
`,
|
||||
{ inlineTemplate: true }
|
||||
@@ -531,6 +547,8 @@ defineExpose({ foo: 123 })
|
||||
)
|
||||
expect(content).toMatch(`_isRef(v) ? v.value += 1 : v += 1`)
|
||||
expect(content).toMatch(`_isRef(v) ? v.value -= 1 : v -= 1`)
|
||||
expect(content).toMatch(`_isRef(v) ? v.value = a : v = a`)
|
||||
expect(content).toMatch(`_isRef(v) ? v.value = _ctx.a : v = _ctx.a`)
|
||||
assertCode(content)
|
||||
})
|
||||
|
||||
|
||||
@@ -526,7 +526,7 @@ export function compileScript(
|
||||
/**
|
||||
* check defaults. If the default object is an object literal with only
|
||||
* static properties, we can directly generate more optimzied default
|
||||
* decalrations. Otherwise we will have to fallback to runtime merging.
|
||||
* declarations. Otherwise we will have to fallback to runtime merging.
|
||||
*/
|
||||
function checkStaticDefaults() {
|
||||
return (
|
||||
@@ -895,7 +895,7 @@ export function compileScript(
|
||||
}
|
||||
}
|
||||
|
||||
// walk decalrations to record declared bindings
|
||||
// walk declarations to record declared bindings
|
||||
if (
|
||||
(node.type === 'VariableDeclaration' ||
|
||||
node.type === 'FunctionDeclaration' ||
|
||||
|
||||
Reference in New Issue
Block a user