Skip to content

Instantly share code, notes, and snippets.

@apapirovski
Last active October 20, 2017 00:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save apapirovski/b8d0803e6bd9938d29c207d50717eef3 to your computer and use it in GitHub Desktop.
Save apapirovski/b8d0803e6bd9938d29c207d50717eef3 to your computer and use it in GitHub Desktop.
eslint rule for finding `assert.throws(fn, common.expectsError(err))`, put it in /tools/eslint-rules and add `prefer-common-expectserror: error` to /test/.eslintrc.yaml
'use strict';
//------------------------------------------------------------------------------
// Rule Definition
//------------------------------------------------------------------------------
const msg = 'Please use common.expectsError(fn, err) instead of ' +
'assert.throws(fn, common.expectsError(err)).';
function isAssertThrows(node) {
return node &&
node.callee &&
node.callee.object &&
node.callee.object.name === 'assert' &&
node.callee.property &&
node.callee.property.name === 'throws';
}
function isFunction(arg) {
return arg && (arg.type === 'ArrowFunctionExpression' ||
arg.type === 'FunctionExpression' ||
arg.type === 'CallExpression');
}
function isCommonExpectsError(node) {
return node &&
node.callee &&
node.callee.object &&
node.callee.object.name === 'common' &&
node.callee.property &&
node.callee.property.name === 'expectsError';
}
module.exports = function(context) {
return {
CallExpression(node) {
if (isAssertThrows(node) &&
isFunction(node.arguments[0]) &&
isCommonExpectsError(node.arguments[1]) &&
!node.arguments[2]) {
context.report(node, msg);
}
}
};
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment