Last active
October 20, 2017 00:48
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'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