-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: ES2022
no-top-level-await
(#28)
- Loading branch information
Showing
5 changed files
with
91 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
})(); | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}`) | ||
}, | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' | ||
} | ||
] | ||
}, | ||
] | ||
}) |