mirror of
https://github.com/zebrajr/node.git
synced 2026-01-15 12:15:26 +00:00
Extend the assert-throws-arguments custom ESLint rule to also check for the use of template literals as a second argument to assert.throws. PR-URL: https://github.com/nodejs/node/pull/10301 Ref: https://github.com/nodejs/node/pull/10282#discussion_r92607290 Reviewed-By: Prince John Wesley <princejohnwesley@gmail.com> Reviewed-By: Jeremiah Senkpiel <fishrock123@rocketmail.com> Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: Roman Reiss <me@silverwind.io> Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Italo A. Casas <me@italoacasas.com>
61 lines
1.4 KiB
JavaScript
61 lines
1.4 KiB
JavaScript
/**
|
|
* @fileoverview Check that assert.throws is never called with a string as
|
|
* second argument.
|
|
* @author Michaël Zasso
|
|
*/
|
|
'use strict';
|
|
|
|
//------------------------------------------------------------------------------
|
|
// Rule Definition
|
|
//------------------------------------------------------------------------------
|
|
|
|
function checkThrowsArguments(context, node) {
|
|
if (node.callee.type === 'MemberExpression' &&
|
|
node.callee.object.name === 'assert' &&
|
|
node.callee.property.name === 'throws') {
|
|
const args = node.arguments;
|
|
if (args.length > 3) {
|
|
context.report({
|
|
message: 'Too many arguments',
|
|
node: node
|
|
});
|
|
} else if (args.length > 1) {
|
|
const error = args[1];
|
|
if (error.type === 'Literal' && typeof error.value === 'string' ||
|
|
error.type === 'TemplateLiteral') {
|
|
context.report({
|
|
message: 'Unexpected string as second argument',
|
|
node: error
|
|
});
|
|
}
|
|
} else {
|
|
if (context.options[0].requireTwo) {
|
|
context.report({
|
|
message: 'Expected at least two arguments',
|
|
node: node
|
|
});
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
module.exports = {
|
|
meta: {
|
|
schema: [
|
|
{
|
|
type: 'object',
|
|
properties: {
|
|
requireTwo: {
|
|
type: 'boolean'
|
|
}
|
|
}
|
|
}
|
|
]
|
|
},
|
|
create: function(context) {
|
|
return {
|
|
CallExpression: (node) => checkThrowsArguments(context, node)
|
|
};
|
|
}
|
|
};
|