Skip to content

Commit f9363e3

Browse files
authored
no-top-level-await: Check if we are at the top-level using a depth counter (#31)
Fixes #30
1 parent b8e3e1f commit f9363e3

File tree

2 files changed

+7
-8
lines changed

2 files changed

+7
-8
lines changed

lib/rules/no-top-level-await.js

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,18 +20,16 @@ module.exports = {
2020
type: "problem",
2121
},
2222
create(context) {
23-
let inFunction = null
23+
let functionDepth = 0
2424
return {
25-
":function"(node) {
26-
inFunction = node
25+
":function"() {
26+
functionDepth++
2727
},
28-
":function:exit"(node) {
29-
if (inFunction === node) {
30-
inFunction = null
31-
}
28+
":function:exit"() {
29+
functionDepth--
3230
},
3331
"AwaitExpression, ForOfStatement[await=true]"(node) {
34-
if (inFunction) {
32+
if (functionDepth > 0) {
3533
// not top-level
3634
return
3735
}

tests/lib/rules/no-top-level-await.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ new RuleTester({
2828
"async function f() { for await (var a of b); }",
2929
"async function f() { for await (let a of b); }",
3030
"async function f() { for await (const a of b); }",
31+
"async function f() { function g() {}; await expr; }",
3132
"function f() { async function f() { await expr } }",
3233
],
3334
invalid: [

0 commit comments

Comments
 (0)