mirror of
https://github.com/nodejs/node.git
synced 2024-11-27 22:16:50 +01:00
3c1069bb06
Refs: https://eslint.org/docs/latest/use/migrate-to-9.0.0 PR-URL: https://github.com/nodejs/node/pull/52889 Reviewed-By: Moshe Atlow <moshe@atlow.co.il> Reviewed-By: Luigi Pinca <luigipinca@gmail.com> Reviewed-By: Mohammed Keyvanzadeh <mohammadkeyvanzade94@gmail.com> Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com> Reviewed-By: Yagiz Nizipli <yagiz.nizipli@sentry.io>
65 lines
1.7 KiB
JavaScript
65 lines
1.7 KiB
JavaScript
/**
|
|
* @fileoverview Prohibit the `if (err) throw err;` pattern
|
|
* @author Teddy Katz
|
|
*/
|
|
|
|
'use strict';
|
|
|
|
const utils = require('./rules-utils.js');
|
|
|
|
module.exports = {
|
|
meta: {
|
|
fixable: 'code',
|
|
},
|
|
create(context) {
|
|
const sourceCode = context.sourceCode;
|
|
let assertImported = false;
|
|
|
|
function hasSameTokens(nodeA, nodeB) {
|
|
const aTokens = sourceCode.getTokens(nodeA);
|
|
const bTokens = sourceCode.getTokens(nodeB);
|
|
|
|
return aTokens.length === bTokens.length &&
|
|
aTokens.every((token, index) => {
|
|
return token.type === bTokens[index].type &&
|
|
token.value === bTokens[index].value;
|
|
});
|
|
}
|
|
|
|
function checkAssertNode(node) {
|
|
if (utils.isRequired(node, ['assert'])) {
|
|
assertImported = true;
|
|
}
|
|
}
|
|
|
|
return {
|
|
'CallExpression': (node) => checkAssertNode(node),
|
|
'IfStatement': (node) => {
|
|
const firstStatement = node.consequent.type === 'BlockStatement' ?
|
|
node.consequent.body[0] :
|
|
node.consequent;
|
|
if (
|
|
firstStatement &&
|
|
firstStatement.type === 'ThrowStatement' &&
|
|
hasSameTokens(node.test, firstStatement.argument)
|
|
) {
|
|
const argument = sourceCode.getText(node.test);
|
|
context.report({
|
|
node: firstStatement,
|
|
message: 'Use assert.ifError({{argument}}) instead.',
|
|
data: { argument },
|
|
fix: (fixer) => {
|
|
if (assertImported) {
|
|
return fixer.replaceText(
|
|
node,
|
|
`assert.ifError(${argument});`,
|
|
);
|
|
}
|
|
},
|
|
});
|
|
}
|
|
},
|
|
};
|
|
},
|
|
};
|