2020-09-06 22:27:07 +02:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
const mustCall = 'CallExpression[callee.object.name="common"]' +
|
|
|
|
'[callee.property.name="mustCall"]';
|
|
|
|
|
2024-05-09 17:27:39 +02:00
|
|
|
module.exports = {
|
|
|
|
create(context) {
|
|
|
|
function isAssertIfError(node) {
|
|
|
|
return node.type === 'MemberExpression' &&
|
|
|
|
node.object.type === 'Identifier' &&
|
|
|
|
node.object.name === 'assert' &&
|
|
|
|
node.property.type === 'Identifier' &&
|
|
|
|
node.property.name === 'ifError';
|
|
|
|
}
|
2020-09-06 22:27:07 +02:00
|
|
|
|
2024-05-09 17:27:39 +02:00
|
|
|
function isCallToIfError(node, errName) {
|
|
|
|
return node.type === 'CallExpression' &&
|
|
|
|
isAssertIfError(node.callee) &&
|
|
|
|
node.arguments.length > 0 &&
|
|
|
|
node.arguments[0].type === 'Identifier' &&
|
|
|
|
node.arguments[0].name === errName;
|
|
|
|
}
|
2020-09-06 22:27:07 +02:00
|
|
|
|
2024-05-09 17:27:39 +02:00
|
|
|
function bodyStartsWithCallToIfError(body, errName) {
|
|
|
|
while (body.type === 'BlockStatement' && body.body.length > 0)
|
|
|
|
body = body.body[0];
|
2020-09-06 22:27:07 +02:00
|
|
|
|
2024-05-09 17:27:39 +02:00
|
|
|
let expr;
|
|
|
|
switch (body.type) {
|
|
|
|
case 'ReturnStatement':
|
|
|
|
expr = body.argument;
|
|
|
|
break;
|
|
|
|
case 'ExpressionStatement':
|
|
|
|
expr = body.expression;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
expr = body;
|
|
|
|
}
|
2020-09-06 22:27:07 +02:00
|
|
|
|
2024-05-09 17:27:39 +02:00
|
|
|
return isCallToIfError(expr, errName);
|
|
|
|
}
|
2020-09-06 22:27:07 +02:00
|
|
|
|
2024-05-09 17:27:39 +02:00
|
|
|
return {
|
|
|
|
[`${mustCall}:exit`]: (mustCall) => {
|
|
|
|
if (mustCall.arguments.length > 0) {
|
|
|
|
const callback = mustCall.arguments[0];
|
|
|
|
if (isAssertIfError(callback)) {
|
|
|
|
context.report(mustCall, 'Please use common.mustSucceed instead of ' +
|
|
|
|
'common.mustCall(assert.ifError).');
|
|
|
|
}
|
2020-09-06 22:27:07 +02:00
|
|
|
|
2024-05-09 17:27:39 +02:00
|
|
|
if (callback.type === 'ArrowFunctionExpression' ||
|
|
|
|
callback.type === 'FunctionExpression') {
|
|
|
|
if (callback.params.length > 0 &&
|
|
|
|
callback.params[0].type === 'Identifier') {
|
|
|
|
const errName = callback.params[0].name;
|
|
|
|
if (bodyStartsWithCallToIfError(callback.body, errName)) {
|
|
|
|
context.report(mustCall, 'Please use common.mustSucceed instead' +
|
|
|
|
' of common.mustCall with' +
|
|
|
|
' assert.ifError.');
|
|
|
|
}
|
2020-09-06 22:27:07 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2024-05-09 17:27:39 +02:00
|
|
|
},
|
|
|
|
};
|
|
|
|
},
|
2020-09-06 22:27:07 +02:00
|
|
|
};
|