node/tools/eslint-rules/async-iife-no-unused-result.js
cjihrig ee0b44fd93
tools: add meta.fixable to fixable lint rules
This commit adds the meta.fixable property to all fixable ESLint
rules. This is required as of ESLint 7.6.0.

PR-URL: https://github.com/nodejs/node/pull/34589
Reviewed-By: Rich Trott <rtrott@gmail.com>
Reviewed-By: Trivikram Kamat <trivikr.dev@gmail.com>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
2020-08-03 08:48:32 -04: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())');
}
});
}
}
};
}
};