Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

tools: add ESLint rule for assert.throws arguments #10089

Merged
merged 1 commit into from
Dec 5, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -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: ['[', ']'] }]

Expand Down
59 changes: 59 additions & 0 deletions tools/eslint-rules/assert-throws-arguments.js
Original file line number Diff line number Diff line change
@@ -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)
};
}
};