Skip to content

Commit 25cd455

Browse files
committed
tools: enforce throw new Error() with lint rule
Add linting rule requiring `throw new Error()` over `throw Error()`. PR-URL: #3714 Reviewed-By: Evan Lucas <[email protected]> Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: Jeremiah Senkpiel <[email protected]> Reviewed-By: James M Snell <[email protected]>
1 parent 35f2f64 commit 25cd455

File tree

2 files changed

+38
-0
lines changed

2 files changed

+38
-0
lines changed

.eslintrc

+2
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,8 @@ rules:
8787

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

9193
# Global scoped method and vars
9294
globals:

tools/eslint-rules/new-with-error.js

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/**
2+
* @fileoverview Require `throw new Error()` rather than `throw Error()`
3+
* @author Rich Trott
4+
*/
5+
'use strict';
6+
7+
//------------------------------------------------------------------------------
8+
// Rule Definition
9+
//------------------------------------------------------------------------------
10+
11+
module.exports = function(context) {
12+
13+
var errorList = context.options.length !== 0 ? context.options : ['Error'];
14+
15+
return {
16+
'ThrowStatement': function(node) {
17+
if (node.argument.type === 'CallExpression' &&
18+
errorList.indexOf(node.argument.callee.name) !== -1) {
19+
context.report(node, 'Use new keyword when throwing.');
20+
}
21+
}
22+
};
23+
};
24+
25+
module.exports.schema = {
26+
'type': 'array',
27+
'items': [
28+
{
29+
'enum': [0, 1, 2]
30+
}
31+
],
32+
'additionalItems': {
33+
'type': 'string'
34+
},
35+
'uniqueItems': true
36+
};

0 commit comments

Comments
 (0)