Skip to content

Commit 8485cd4

Browse files
committed
fix: handle case of ref declaration without initial value
1 parent ed2eb81 commit 8485cd4

File tree

3 files changed

+22
-12
lines changed

3 files changed

+22
-12
lines changed

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

+2-1
Original file line numberDiff line numberDiff line change
@@ -292,14 +292,15 @@ exports[`SFC compile <script setup> ref: syntax sugar convert ref declarations 1
292292
293293
export function setup() {
294294
295+
const foo = ref()
295296
const a = ref(1)
296297
const b = ref({
297298
count: 0
298299
})
299300
let c = () => {}
300301
let d
301302
302-
return { a, b, c, d }
303+
return { foo, a, b, c, d }
303304
}
304305
305306
export default { setup }"

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

+5
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,7 @@ describe('SFC compile <script setup>', () => {
259259
describe('ref: syntax sugar', () => {
260260
test('convert ref declarations', () => {
261261
const { content, bindings } = compile(`<script setup>
262+
ref: foo
262263
ref: a = 1
263264
ref: b = {
264265
count: 0
@@ -267,7 +268,10 @@ describe('SFC compile <script setup>', () => {
267268
let d
268269
</script>`)
269270
expect(content).toMatch(`import { ref } from 'vue'`)
271+
expect(content).not.toMatch(`ref: foo`)
270272
expect(content).not.toMatch(`ref: a`)
273+
expect(content).not.toMatch(`ref: b`)
274+
expect(content).toMatch(`const foo = ref()`)
271275
expect(content).toMatch(`const a = ref(1)`)
272276
expect(content).toMatch(`
273277
const b = ref({
@@ -279,6 +283,7 @@ describe('SFC compile <script setup>', () => {
279283
expect(content).toMatch(`let d`)
280284
assertCode(content)
281285
expect(bindings).toStrictEqual({
286+
foo: 'setup',
282287
a: 'setup',
283288
b: 'setup',
284289
c: 'setup',

packages/compiler-sfc/src/compileScript.ts

+15-11
Original file line numberDiff line numberDiff line change
@@ -155,11 +155,7 @@ export function compileScript(
155155
helperImports.add('ref')
156156
const { left, right } = exp
157157
if (left.type === 'Identifier') {
158-
if (left.name[0] === '$') {
159-
error(`ref variable identifiers cannot start with $.`, left)
160-
}
161-
refBindings[left.name] = setupBindings[left.name] = true
162-
refIdentifiers.add(left)
158+
registerRefBinding(left)
163159
s.prependRight(right.start! + startOffset, `ref(`)
164160
s.appendLeft(right.end! + startOffset, ')')
165161
} else if (left.type === 'ObjectPattern') {
@@ -186,11 +182,22 @@ export function compileScript(
186182
// possible multiple declarations
187183
// ref: x = 1, y = 2
188184
exp.expressions.forEach(e => processRefExpression(e, statement))
185+
} else if (exp.type === 'Identifier') {
186+
registerRefBinding(exp)
187+
s.appendLeft(exp.end! + startOffset, ` = ref()`)
189188
} else {
190189
error(`ref: statements can only contain assignment expressions.`, exp)
191190
}
192191
}
193192

193+
function registerRefBinding(id: Identifier) {
194+
if (id.name[0] === '$') {
195+
error(`ref variable identifiers cannot start with $.`, id)
196+
}
197+
refBindings[id.name] = setupBindings[id.name] = true
198+
refIdentifiers.add(id)
199+
}
200+
194201
function processRefObjectPattern(
195202
pattern: ObjectPattern,
196203
statement: LabeledStatement
@@ -227,9 +234,7 @@ export function compileScript(
227234
s.prependRight(nameId.start! + startOffset, `__`)
228235
}
229236
if (nameId) {
230-
// register binding
231-
refBindings[nameId.name] = setupBindings[nameId.name] = true
232-
refIdentifiers.add(nameId)
237+
registerRefBinding(nameId)
233238
// append binding declarations after the parent statement
234239
s.appendLeft(
235240
statement.end! + startOffset,
@@ -261,10 +266,9 @@ export function compileScript(
261266
processRefArrayPattern(e, statement)
262267
}
263268
if (nameId) {
269+
registerRefBinding(nameId)
270+
// prefix original
264271
s.prependRight(nameId.start! + startOffset, `__`)
265-
// register binding
266-
refBindings[nameId.name] = setupBindings[nameId.name] = true
267-
refIdentifiers.add(nameId)
268272
// append binding declarations after the parent statement
269273
s.appendLeft(
270274
statement.end! + startOffset,

0 commit comments

Comments
 (0)