Skip to content

Commit

Permalink
feat: ES2022 no-top-level-await (#28)
Browse files Browse the repository at this point in the history
  • Loading branch information
brettz9 authored Jul 23, 2024
1 parent 4fc74eb commit 30f7398
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,7 @@ for configuration. Here's some examples:
- [no-regexp-lookbehind](./docs/no-regexp-lookbehind.md)
- [no-regexp-named-group](./docs/no-regexp-named-group.md)
- [no-regexp-s-flag](./docs/no-regexp-s-flag.md)
- [no-top-level-await](./docs/no-top-level-await.md)

## Inspiration

Expand Down
31 changes: 31 additions & 0 deletions docs/no-top-level-await.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# no-top-level-await

This prevents the use of `await` at the top-level of documents (outside
of an async function context)

```js
await asyncMethod();

const results = await getAsyncResults();
```

These will not be allowed because they are not supported in the following browsers:

- Edge < 89
- Safari < 15
- Firefox < 89
- Chrome < 89


## What is the Fix?

You can wrap your `await` in an async Immediately Invoking Function
Expression (IIFE).

```js
(async () => {
await asyncMethod();

const results = await getAsyncResults();
})();
```
6 changes: 6 additions & 0 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,12 @@ createRule(
"disallow static blocks like `static { x = 1 }`",
{ ts: 2022 }
);
createRule(
"no-top-level-await",
"edge < 89, safari < 15, firefox < 89, chrome < 89",
"disallow await keyword outside of async function context",
{ ts: 2022 }
);

// Proposals...
createRule(
Expand Down
20 changes: 20 additions & 0 deletions lib/rules/no-top-level-await.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
'use strict';

const functionTypes = new Set([
'FunctionDeclaration',
'FunctionExpression',
'ArrowFunctionExpression',
]);

module.exports = (context, badBrowser) => ({
AwaitExpression(node) {
let currentNode = node;
while (currentNode.parent) {
currentNode = currentNode.parent;
if (functionTypes.has(currentNode.type) && currentNode.async) {
return;
}
}
context.report(node, `Top-level await is not supported in ${badBrowser}`)
},
})
33 changes: 33 additions & 0 deletions test/no-top-level-await.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
'use strict';

const rule = require('../lib/index').rules['no-top-level-await']
const RuleTester = require('eslint').RuleTester

const ruleTester = new RuleTester({languageOptions: {ecmaVersion: 2022}})

ruleTester.run('no-top-level-await', rule, {
valid: [
{code: 'async function get () { return await asyncMethod(); }'},
{code: '(async () => { await getAsyncResults(); })();'},
{code: 'const get = async function get () { return await asyncMethod(); }'},
{code: 'const get = async () => { return await asyncMethod(); }'},
],
invalid: [
{
code: 'await asyncMethod();',
errors: [
{
message: 'Top-level await is not supported in undefined'
}
]
},
{
code: 'const results = await getAsyncResults();',
errors: [
{
message: 'Top-level await is not supported in undefined'
}
]
},
]
})

0 comments on commit 30f7398

Please sign in to comment.