Skip to content
This repository was archived by the owner on Dec 25, 2024. It is now read-only.

Commit e98500d

Browse files
committed
fix: throw on top-level await
1 parent 232709d commit e98500d

File tree

3 files changed

+69
-1
lines changed

3 files changed

+69
-1
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ If you enjoy using `<script setup>`, you might also want to try [`vue-global-api
161161
- [x] Vite plugin
162162
- [x] Webpack plugin
163163
- [x] Nuxt module
164-
- [ ] Top-level await
164+
- [ ] ~~Top-level await~~ (not supported)
165165

166166
## How?
167167

src/core/macros.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,11 @@ export function applyMacros(nodes: Statement[]) {
245245
}
246246
}
247247

248+
function throwIfAwait(node: Node) {
249+
if (node.type === 'AwaitExpression')
250+
error('top-level await is not supported in Vue 2', node)
251+
}
252+
248253
nodes = nodes
249254
.map((raw: Node) => {
250255
let node = raw
@@ -260,13 +265,17 @@ export function applyMacros(nodes: Statement[]) {
260265
decl.init = t.memberExpression(t.identifier('__ctx'), t.identifier('emit')) as any
261266
else if (processDefineProps(decl.init) || processWithDefaults(decl.init))
262267
decl.init = t.identifier('__props') as any
268+
else
269+
throwIfAwait(decl.init)
263270
}
264271
}
265272
}
266273

267274
if (processDefineEmits(node) || processDefineProps(node) || processDefineExpose(node))
268275
return null
269276

277+
throwIfAwait(node)
278+
270279
return raw
271280
})
272281
.filter(Boolean) as Statement[]

test/errors.test.ts

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
import { transform as t } from '../src'
2+
3+
describe('errors', () => {
4+
it('langs', () => {
5+
expect(() =>
6+
t(`
7+
<script setup>
8+
const a = 1
9+
</script>
10+
11+
<script lang="ts">
12+
export default {}
13+
</script>
14+
`))
15+
.toThrowError('<script setup> language must be the same as <script>')
16+
})
17+
18+
it('defineProps', () => {
19+
expect(() =>
20+
t(`
21+
<script setup>
22+
defineProps()
23+
const a = defineProps()
24+
</script>
25+
`))
26+
.toThrowError('duplicate defineProps() call')
27+
})
28+
29+
it('top-level await', () => {
30+
expect(() =>
31+
t(`
32+
<script setup>
33+
defineProps()
34+
await something()
35+
</script>
36+
`))
37+
.toThrowError('top-level await is not supported in Vue 2')
38+
39+
expect(() =>
40+
t(`
41+
<script setup>
42+
defineProps()
43+
const {data} = await something()
44+
</script>
45+
`))
46+
.toThrowError('top-level await is not supported in Vue 2')
47+
48+
expect(() =>
49+
t(`
50+
<script setup>
51+
defineProps()
52+
const a = async () => {
53+
await something()
54+
}
55+
</script>
56+
`))
57+
.not.toThrow()
58+
})
59+
})

0 commit comments

Comments
 (0)