-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #108 from endojs/mfig-107-eslint-await
ESLint `await` rule cleanup
- Loading branch information
Showing
11 changed files
with
333 additions
and
139 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
User-visible changes in `@endo/eslint-plugin` | ||
|
||
# v0.4.4 (2023-04-14) | ||
|
||
- Separate rules into subsets (documented in README.md) |
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,16 @@ | ||
{ | ||
"compilerOptions": { | ||
"baseUrl": ".", | ||
"moduleResolution": "node", | ||
"target": "esnext", | ||
"module": "CommonJS", | ||
"noImplicitAny": true, | ||
"noEmit": true, | ||
"noUnusedLocals": true, | ||
"noUnusedParameters": true, | ||
"alwaysStrict": true, | ||
"downlevelIteration": true | ||
}, | ||
"include": ["lib/**/*.js"], | ||
"exclude": ["node_modules/**"] | ||
} |
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,60 @@ | ||
// @ts-check | ||
/* eslint-env node */ | ||
|
||
/** | ||
* @author Michael FIG | ||
* See LICENSE file in root directory for full license. | ||
*/ | ||
|
||
'use strict'; | ||
|
||
const { isFunctionLike, makeAwaitAllowedVisitor } = require('../tools.js'); | ||
|
||
/** @type {import('eslint').Rule.RuleModule} */ | ||
module.exports = { | ||
meta: { | ||
docs: { | ||
description: `ensure the first \`await\` in an \`async\` function is non-nested so that it is clear when the synchronous portion of the function is finished`, | ||
category: 'Possible Errors', | ||
recommended: true, | ||
url: | ||
'https://github.com/endojs/Jessie/blob/main/packages/eslint-plugin/lib/rules/safe-await-separator', | ||
}, | ||
type: 'problem', | ||
fixable: null, | ||
hasSuggestions: true, | ||
messages: { | ||
unsafeAwaitSeparator: | ||
'The first `await` appearing in an async function must not be nested', | ||
insertAwaitNull: 'Insert `await null;` before the first `await`', | ||
}, | ||
schema: [], | ||
supported: true, | ||
}, | ||
create(context) { | ||
/** | ||
* @param {import('eslint').Rule.Node} node | ||
*/ | ||
const makeReport = node => { | ||
context.report({ | ||
node, | ||
messageId: 'unsafeAwaitSeparator', | ||
suggest: [ | ||
{ | ||
messageId: 'insertAwaitNull', | ||
fix: fixer => { | ||
let cur = node; | ||
let parent = cur.parent; | ||
while (!isFunctionLike(parent.parent)) { | ||
cur = parent; | ||
parent = cur.parent; | ||
} | ||
return fixer.insertTextBefore(cur, 'await null;'); | ||
}, | ||
}, | ||
], | ||
}); | ||
}; | ||
return makeAwaitAllowedVisitor(context, makeReport, true); | ||
}, | ||
}; |
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,135 @@ | ||
// @ts-check | ||
/* eslint-env node */ | ||
|
||
'use strict'; | ||
|
||
/** @typedef {import('eslint').Rule.Node} Node */ | ||
|
||
const harden = Object.freeze; | ||
|
||
/** @type {Node['type'][]} */ | ||
const functionLikeNodeTypes = [ | ||
'ArrowFunctionExpression', | ||
'FunctionExpression', | ||
|
||
'FunctionDeclaration', | ||
'MethodDefinition', | ||
|
||
'Program', | ||
'StaticBlock', | ||
]; | ||
harden(functionLikeNodeTypes); | ||
|
||
/** | ||
* @param {Node} node | ||
*/ | ||
const isFunctionLike = node => functionLikeNodeTypes.includes(node.type); | ||
harden(isFunctionLike); | ||
|
||
/** | ||
* @param {Node} node | ||
* @param {WeakSet<Node>} already | ||
* @returns {boolean} | ||
*/ | ||
const isAwaitAllowedInNode = (node, already = new WeakSet()) => { | ||
// Climb the AST until finding the nearest enclosing function or | ||
// function-like node. | ||
let result = true; | ||
for (; node && !isFunctionLike(node); node = node.parent) { | ||
if (already.has(node)) { | ||
return true; | ||
} | ||
already.add(node); | ||
|
||
if (node.type === 'BlockStatement') { | ||
// do nothing | ||
} else if (node.type === 'ForOfStatement' && node.await) { | ||
// do nothing | ||
} else { | ||
// Encountering anything other than an enclosing block is grounds for | ||
// rejection. | ||
result = false; | ||
} | ||
} | ||
|
||
if (!node) { | ||
// Should never reach here because Program is a FunctionLike for | ||
// top-level await. | ||
throw new Error(`unexpected non-Program AST!`); | ||
} | ||
|
||
return result; | ||
}; | ||
harden(isAwaitAllowedInNode); | ||
|
||
/** | ||
* @param {import('eslint').Rule.RuleContext} _context | ||
* @param {(node: Node) => void} makeReport | ||
* @param {boolean} [addToCache] | ||
*/ | ||
const makeAwaitAllowedVisitor = ( | ||
_context, | ||
makeReport, | ||
addToCache = undefined, | ||
) => { | ||
const already = addToCache ? new WeakSet() : undefined; | ||
return harden({ | ||
/** | ||
* An `await` expression is treated as non-nested if it is: | ||
* - a non-nested expression statement, | ||
* - the right-hand side of a non-nested declarator, or | ||
* - the right-hand side of an assignment expression. | ||
* | ||
* As a hint to future readers, the following ASTExplorer workspace | ||
* reveals some of the relevant AST shapes (note that node.parent is not | ||
* displayed in the ASTExplorer, but is available in the ESLint visitor): | ||
* | ||
* @see https://astexplorer.net/#/gist/4508eec25a8d5be1e0248c4cc06b9634/f6b22f2e8e3abd82a911ca6286a304ef0a3018c4 | ||
* | ||
* This will need different handling for do expressions if they are ever | ||
* added to the language. | ||
* @see https://github.com/tc39/proposal-do-expressions | ||
* | ||
* @param {Node} node | ||
*/ | ||
AwaitExpression: node => { | ||
let parent = node.parent; | ||
if (parent.type === 'VariableDeclarator' && parent.init === node) { | ||
// It's a declarator, so look up to the declaration statement's parent. | ||
parent = parent.parent.parent; | ||
} else if ( | ||
parent.type === 'AssignmentExpression' && | ||
parent.right === node && | ||
parent.operator === '=' | ||
) { | ||
// It's an assignment, so look up to the assigment's parent. | ||
parent = parent.parent; | ||
} | ||
if (parent.type === 'ExpressionStatement') { | ||
// Try to find the parent block. | ||
parent = parent.parent; | ||
} | ||
|
||
if (!isAwaitAllowedInNode(parent, already)) { | ||
makeReport(node); | ||
} | ||
}, | ||
/** | ||
* A `for-await-of` loop is treated as a simple `await` statement. | ||
* | ||
* @param {Node} node | ||
*/ | ||
'ForOfStatement[await=true]': node => { | ||
if (!isAwaitAllowedInNode(node.parent, already)) { | ||
makeReport(node); | ||
} | ||
}, | ||
}); | ||
}; | ||
harden(makeAwaitAllowedVisitor); | ||
|
||
module.exports = harden({ | ||
isFunctionLike, | ||
isAwaitAllowedInNode, | ||
makeAwaitAllowedVisitor, | ||
}); |
Oops, something went wrong.