mirror of
https://github.com/nodejs/node.git
synced 2025-05-21 14:39:17 +00:00

The result of an async IIFE should always be handled in our tests, typically by adding `.then(common.mustCall())` to verify that the async function actually finishes executing at some point. PR-URL: https://github.com/nodejs/node/pull/34363 Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: Rich Trott <rtrott@gmail.com> Reviewed-By: Tobias Nießen <tniessen@tnie.de> Reviewed-By: Richard Lau <riclau@uk.ibm.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
38 lines
1.0 KiB
JavaScript
38 lines
1.0 KiB
JavaScript
'use strict';
|
|
const { isCommonModule } = require('./rules-utils.js');
|
|
|
|
function isAsyncIIFE(node) {
|
|
const { callee: { type, async } } = node;
|
|
const types = ['FunctionExpression', 'ArrowFunctionExpression'];
|
|
return types.includes(type) && async;
|
|
}
|
|
|
|
const message =
|
|
'The result of an immediately-invoked async function needs to be used ' +
|
|
'(e.g. with `.then(common.mustCall())`)';
|
|
|
|
module.exports = {
|
|
create: function(context) {
|
|
let hasCommonModule = false;
|
|
return {
|
|
CallExpression: function(node) {
|
|
if (isCommonModule(node) && node.parent.type === 'VariableDeclarator') {
|
|
hasCommonModule = true;
|
|
}
|
|
|
|
if (!isAsyncIIFE(node)) return;
|
|
if (node.parent && node.parent.type === 'ExpressionStatement') {
|
|
context.report({
|
|
node,
|
|
message,
|
|
fix: (fixer) => {
|
|
if (hasCommonModule)
|
|
return fixer.insertTextAfter(node, '.then(common.mustCall())');
|
|
}
|
|
});
|
|
}
|
|
}
|
|
};
|
|
}
|
|
};
|