-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add
es-roikoren/no-top-level-await
rule (#12)
* feat: add `es-roikoren/no-top-level-await` rule * docs: tiny update * fix: import order * test: fix * chore: update watch script * test: small fix
- Loading branch information
1 parent
ae75c1b
commit f54eb1a
Showing
8 changed files
with
151 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'eslint-plugin-es-roikoren': patch | ||
--- | ||
|
||
feat: add `es-roikoren/no-top-level-await` rule |
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 @@ | ||
# es-roikoren/no-top-level-await | ||
> disallow top-level `await`. | ||
- ✅ The following configurations enable this rule: `plugin:es-roikoren/no-new-in-esnext` | ||
|
||
This rule reports ES2022 [Top-level `await`](https://github.com/tc39/proposal-top-level-await) as errors. | ||
|
||
## Examples | ||
|
||
⛔ Examples of **incorrect** code for this rule: | ||
|
||
```js | ||
/*eslint es-roikoren/no-top-level-await: error */ | ||
await expr; | ||
``` | ||
|
||
## 📚 References | ||
|
||
- [Rule source](https://github.com/roikoren755/eslint-plugin-es/blob/v0.0.6/src/rules/no-top-level-await.ts) | ||
- [Test source](https://github.com/roikoren755/eslint-plugin-es/blob/v0.0.6/tests/src/rules/no-top-level-await.ts) |
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
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,37 @@ | ||
import type { TSESTree } from '@typescript-eslint/typescript-estree'; | ||
|
||
import { createRule } from '../util/create-rule'; | ||
|
||
export const category = 'ES2022'; | ||
export default createRule<[], 'forbidden'>({ | ||
name: 'no-top-level-await', | ||
meta: { | ||
type: 'problem', | ||
docs: { description: 'disallow top-level `await`.', recommended: false }, | ||
schema: [], | ||
messages: { forbidden: "ES2022 top-level 'await' is forbidden." }, | ||
}, | ||
defaultOptions: [], | ||
create(context) { | ||
let inFunction: TSESTree.FunctionExpression | null = null; | ||
|
||
return { | ||
':function'(node: TSESTree.FunctionExpression) { | ||
inFunction = node; | ||
}, | ||
':function:exit'(node: TSESTree.FunctionExpression) { | ||
if (inFunction === node) { | ||
inFunction = null; | ||
} | ||
}, | ||
'AwaitExpression, ForOfStatement[await=true]'(node: TSESTree.AwaitExpression | TSESTree.ForOfStatement) { | ||
if (inFunction) { | ||
// not top-level | ||
return; | ||
} | ||
|
||
context.report({ node, messageId: 'forbidden' }); | ||
}, | ||
}; | ||
}, | ||
}); |
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,84 @@ | ||
import type { TSESLint } from '@typescript-eslint/experimental-utils'; | ||
import { AST_NODE_TYPES } from '@typescript-eslint/types'; | ||
|
||
import { RuleTester } from '../../tester'; | ||
import rule from '../../../src/rules/no-top-level-await'; | ||
|
||
const error = { messageId: 'forbidden' as const, line: 1, column: 1, type: AST_NODE_TYPES.AwaitExpression, data: {} }; | ||
|
||
if (!RuleTester.isSupported(2022)) { | ||
console.log('Skip the tests of no-top-level-await.'); | ||
} else { | ||
new RuleTester({ parserOptions: { sourceType: 'module' } } as TSESLint.RuleTesterConfig).run( | ||
'no-top-level-await', | ||
rule, | ||
{ | ||
valid: [ | ||
'async function f() { await expr }', | ||
'expr;', | ||
'const f = async function() { await expr }', | ||
'const f = async () => { await expr }', | ||
'({ async method() { await expr } })', | ||
'class A { async method() { await expr } }', | ||
'(class { async method() { await expr } })', | ||
'async function f() { for await (a of b); }', | ||
'async function f() { for await (var a of b); }', | ||
'async function f() { for await (let a of b); }', | ||
'async function f() { for await (const a of b); }', | ||
'function f() { async function f() { await expr } }', | ||
], | ||
invalid: [ | ||
{ code: 'await expr', errors: [{ ...error }] }, | ||
{ code: 'for await (a of b);', errors: [{ ...error, type: AST_NODE_TYPES.ForOfStatement }] }, | ||
{ code: 'for await (var a of b);', errors: [{ ...error, type: AST_NODE_TYPES.ForOfStatement }] }, | ||
{ code: 'for await (let a of b);', errors: [{ ...error, type: AST_NODE_TYPES.ForOfStatement }] }, | ||
{ code: 'for await (const a of b);', errors: [{ ...error, type: AST_NODE_TYPES.ForOfStatement }] }, | ||
{ | ||
code: ` | ||
await expr | ||
async function f() { | ||
await expr | ||
} | ||
await expr`, | ||
errors: [ | ||
{ ...error, line: 2 }, | ||
{ ...error, line: 6 }, | ||
], | ||
}, | ||
{ | ||
code: ` | ||
await expr | ||
async function f() { | ||
await expr | ||
async function f() { | ||
await expr | ||
} | ||
} | ||
await expr`, | ||
errors: [ | ||
{ ...error, line: 2 }, | ||
{ ...error, line: 9 }, | ||
], | ||
}, | ||
{ | ||
code: ` | ||
let jQuery; | ||
try { | ||
jQuery = await import('https://cdn-a.com/jQuery'); | ||
} catch { | ||
jQuery = await import('https://cdn-b.com/jQuery'); | ||
}`, | ||
errors: [ | ||
{ ...error, line: 4, column: 12 }, | ||
{ ...error, line: 6, column: 12 }, | ||
], | ||
}, | ||
{ code: '{ await expr }', errors: [{ ...error, column: 3 }] }, | ||
{ code: '( await expr )', errors: [{ ...error, column: 3 }] }, | ||
{ code: 'fn( await expr )', errors: [{ ...error, column: 5 }] }, | ||
{ code: 'if (foo) { await expr }', errors: [{ ...error, column: 12 }] }, | ||
{ code: 'for (const foo of bar) { await expr }', errors: [{ ...error, column: 26 }] }, | ||
], | ||
}, | ||
); | ||
} |