feat(compiler-sfc): <script setup> support (experimental)

This is the last commit for the feature which adds async/await detection.
This commit is contained in:
Evan You
2020-07-10 18:00:13 -04:00
parent 73bfce3706
commit 4c43d4e5b9
3 changed files with 80 additions and 14 deletions

View File

@@ -49,10 +49,6 @@ describe('SFC compile <script setup>', () => {
)
})
test('async/await detection', () => {
// TODO
})
describe('exports', () => {
test('export const x = ...', () => {
const { content, bindings } = compile(
@@ -333,6 +329,45 @@ describe('SFC compile <script setup>', () => {
})
})
describe('async/await detection', () => {
function assertAwaitDetection(code: string, shouldAsync = true) {
const { content } = compile(`<script setup>${code}</script>`)
expect(content).toMatch(
`export ${shouldAsync ? `async ` : ``}function setup`
)
}
test('expression statement', () => {
assertAwaitDetection(`await foo`)
})
test('variable', () => {
assertAwaitDetection(`const a = 1 + (await foo)`)
})
test('export', () => {
assertAwaitDetection(`export const a = 1 + (await foo)`)
})
test('nested statements', () => {
assertAwaitDetection(`if (ok) { await foo } else { await bar }`)
})
test('should ignore await inside functions', () => {
// function declaration
assertAwaitDetection(`export async function foo() { await bar }`, false)
// function expression
assertAwaitDetection(`const foo = async () => { await bar }`, false)
// object method
assertAwaitDetection(`const obj = { async method() { await bar }}`, false)
// class method
assertAwaitDetection(
`const cls = class Foo { async method() { await bar }}`,
false
)
})
})
describe('errors', () => {
test('<script> and <script setup> must have same lang', () => {
expect(