fix: handle case of ref declaration without initial value
This commit is contained in:
parent
ed2eb81317
commit
8485cd4843
@ -292,6 +292,7 @@ exports[`SFC compile <script setup> ref: syntax sugar convert ref declarations 1
|
|||||||
|
|
||||||
export function setup() {
|
export function setup() {
|
||||||
|
|
||||||
|
const foo = ref()
|
||||||
const a = ref(1)
|
const a = ref(1)
|
||||||
const b = ref({
|
const b = ref({
|
||||||
count: 0
|
count: 0
|
||||||
@ -299,7 +300,7 @@ export function setup() {
|
|||||||
let c = () => {}
|
let c = () => {}
|
||||||
let d
|
let d
|
||||||
|
|
||||||
return { a, b, c, d }
|
return { foo, a, b, c, d }
|
||||||
}
|
}
|
||||||
|
|
||||||
export default { setup }"
|
export default { setup }"
|
||||||
|
@ -259,6 +259,7 @@ describe('SFC compile <script setup>', () => {
|
|||||||
describe('ref: syntax sugar', () => {
|
describe('ref: syntax sugar', () => {
|
||||||
test('convert ref declarations', () => {
|
test('convert ref declarations', () => {
|
||||||
const { content, bindings } = compile(`<script setup>
|
const { content, bindings } = compile(`<script setup>
|
||||||
|
ref: foo
|
||||||
ref: a = 1
|
ref: a = 1
|
||||||
ref: b = {
|
ref: b = {
|
||||||
count: 0
|
count: 0
|
||||||
@ -267,7 +268,10 @@ describe('SFC compile <script setup>', () => {
|
|||||||
let d
|
let d
|
||||||
</script>`)
|
</script>`)
|
||||||
expect(content).toMatch(`import { ref } from 'vue'`)
|
expect(content).toMatch(`import { ref } from 'vue'`)
|
||||||
|
expect(content).not.toMatch(`ref: foo`)
|
||||||
expect(content).not.toMatch(`ref: a`)
|
expect(content).not.toMatch(`ref: a`)
|
||||||
|
expect(content).not.toMatch(`ref: b`)
|
||||||
|
expect(content).toMatch(`const foo = ref()`)
|
||||||
expect(content).toMatch(`const a = ref(1)`)
|
expect(content).toMatch(`const a = ref(1)`)
|
||||||
expect(content).toMatch(`
|
expect(content).toMatch(`
|
||||||
const b = ref({
|
const b = ref({
|
||||||
@ -279,6 +283,7 @@ describe('SFC compile <script setup>', () => {
|
|||||||
expect(content).toMatch(`let d`)
|
expect(content).toMatch(`let d`)
|
||||||
assertCode(content)
|
assertCode(content)
|
||||||
expect(bindings).toStrictEqual({
|
expect(bindings).toStrictEqual({
|
||||||
|
foo: 'setup',
|
||||||
a: 'setup',
|
a: 'setup',
|
||||||
b: 'setup',
|
b: 'setup',
|
||||||
c: 'setup',
|
c: 'setup',
|
||||||
|
@ -155,11 +155,7 @@ export function compileScript(
|
|||||||
helperImports.add('ref')
|
helperImports.add('ref')
|
||||||
const { left, right } = exp
|
const { left, right } = exp
|
||||||
if (left.type === 'Identifier') {
|
if (left.type === 'Identifier') {
|
||||||
if (left.name[0] === '$') {
|
registerRefBinding(left)
|
||||||
error(`ref variable identifiers cannot start with $.`, left)
|
|
||||||
}
|
|
||||||
refBindings[left.name] = setupBindings[left.name] = true
|
|
||||||
refIdentifiers.add(left)
|
|
||||||
s.prependRight(right.start! + startOffset, `ref(`)
|
s.prependRight(right.start! + startOffset, `ref(`)
|
||||||
s.appendLeft(right.end! + startOffset, ')')
|
s.appendLeft(right.end! + startOffset, ')')
|
||||||
} else if (left.type === 'ObjectPattern') {
|
} else if (left.type === 'ObjectPattern') {
|
||||||
@ -186,11 +182,22 @@ export function compileScript(
|
|||||||
// possible multiple declarations
|
// possible multiple declarations
|
||||||
// ref: x = 1, y = 2
|
// ref: x = 1, y = 2
|
||||||
exp.expressions.forEach(e => processRefExpression(e, statement))
|
exp.expressions.forEach(e => processRefExpression(e, statement))
|
||||||
|
} else if (exp.type === 'Identifier') {
|
||||||
|
registerRefBinding(exp)
|
||||||
|
s.appendLeft(exp.end! + startOffset, ` = ref()`)
|
||||||
} else {
|
} else {
|
||||||
error(`ref: statements can only contain assignment expressions.`, exp)
|
error(`ref: statements can only contain assignment expressions.`, exp)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function registerRefBinding(id: Identifier) {
|
||||||
|
if (id.name[0] === '$') {
|
||||||
|
error(`ref variable identifiers cannot start with $.`, id)
|
||||||
|
}
|
||||||
|
refBindings[id.name] = setupBindings[id.name] = true
|
||||||
|
refIdentifiers.add(id)
|
||||||
|
}
|
||||||
|
|
||||||
function processRefObjectPattern(
|
function processRefObjectPattern(
|
||||||
pattern: ObjectPattern,
|
pattern: ObjectPattern,
|
||||||
statement: LabeledStatement
|
statement: LabeledStatement
|
||||||
@ -227,9 +234,7 @@ export function compileScript(
|
|||||||
s.prependRight(nameId.start! + startOffset, `__`)
|
s.prependRight(nameId.start! + startOffset, `__`)
|
||||||
}
|
}
|
||||||
if (nameId) {
|
if (nameId) {
|
||||||
// register binding
|
registerRefBinding(nameId)
|
||||||
refBindings[nameId.name] = setupBindings[nameId.name] = true
|
|
||||||
refIdentifiers.add(nameId)
|
|
||||||
// append binding declarations after the parent statement
|
// append binding declarations after the parent statement
|
||||||
s.appendLeft(
|
s.appendLeft(
|
||||||
statement.end! + startOffset,
|
statement.end! + startOffset,
|
||||||
@ -261,10 +266,9 @@ export function compileScript(
|
|||||||
processRefArrayPattern(e, statement)
|
processRefArrayPattern(e, statement)
|
||||||
}
|
}
|
||||||
if (nameId) {
|
if (nameId) {
|
||||||
|
registerRefBinding(nameId)
|
||||||
|
// prefix original
|
||||||
s.prependRight(nameId.start! + startOffset, `__`)
|
s.prependRight(nameId.start! + startOffset, `__`)
|
||||||
// register binding
|
|
||||||
refBindings[nameId.name] = setupBindings[nameId.name] = true
|
|
||||||
refIdentifiers.add(nameId)
|
|
||||||
// append binding declarations after the parent statement
|
// append binding declarations after the parent statement
|
||||||
s.appendLeft(
|
s.appendLeft(
|
||||||
statement.end! + startOffset,
|
statement.end! + startOffset,
|
||||||
|
Loading…
x
Reference in New Issue
Block a user