0
0
mirror of https://github.com/nodejs/node.git synced 2024-11-24 12:10:08 +01:00
nodejs/tools/eslint-rules/async-iife-no-unused-result.js
Antoine du Hamel 3ce4cef4e6
tools: enforce use of trailing commas in tools/
PR-URL: https://github.com/nodejs/node/pull/45889
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com>
2022-12-18 16:39:39 +00:00

41 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 = {
meta: {
fixable: 'code',
},
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())');
},
});
}
},
};
},
};