Skip to content

Commit

Permalink
tools: enforce throw new Error() with lint rule
Browse files Browse the repository at this point in the history
Add linting rule requiring `throw new Error()` over `throw Error()`.

PR-URL: #3714
Reviewed-By: Evan Lucas <evanlucas@me.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Jeremiah Senkpiel <fishrock123@rocketmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
  • Loading branch information
Trott authored and Fishrock123 committed Nov 11, 2015
1 parent 38bb0d8 commit d036b35
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,8 @@ rules:

# Custom rules in tools/eslint-rules
require-buffer: 2
new-with-error: [2, "Error", "RangeError", "TypeError", "SyntaxError", "ReferenceError"]


# Global scoped method and vars
globals:
Expand Down
36 changes: 36 additions & 0 deletions tools/eslint-rules/new-with-error.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/**
* @fileoverview Require `throw new Error()` rather than `throw Error()`
* @author Rich Trott
*/
'use strict';

//------------------------------------------------------------------------------
// Rule Definition
//------------------------------------------------------------------------------

module.exports = function(context) {

var errorList = context.options.length !== 0 ? context.options : ['Error'];

return {
'ThrowStatement': function(node) {
if (node.argument.type === 'CallExpression' &&
errorList.indexOf(node.argument.callee.name) !== -1) {
context.report(node, 'Use new keyword when throwing.');
}
}
};
};

module.exports.schema = {
'type': 'array',
'items': [
{
'enum': [0, 1, 2]
}
],
'additionalItems': {
'type': 'string'
},
'uniqueItems': true
};

0 comments on commit d036b35

Please sign in to comment.