diff --git a/.eslintrc b/.eslintrc index 9f150c5d217e0c..8d3689443bd1de 100644 --- a/.eslintrc +++ b/.eslintrc @@ -121,6 +121,7 @@ rules: align-function-arguments: 2 align-multiline-assignment: 2 assert-fail-single-argument: 2 + assert-throws-arguments: [2, { requireTwo: false }] new-with-error: [2, Error, RangeError, TypeError, SyntaxError, ReferenceError] no-useless-regex-char-class-escape: [2, { override: ['[', ']'] }] diff --git a/tools/eslint-rules/assert-throws-arguments.js b/tools/eslint-rules/assert-throws-arguments.js new file mode 100644 index 00000000000000..434a0ca455b471 --- /dev/null +++ b/tools/eslint-rules/assert-throws-arguments.js @@ -0,0 +1,59 @@ +/** + * @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') { + 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) + }; + } +};