Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit d036b35

Browse filesBrowse files
TrottFishrock123
authored andcommitted
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 <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>
1 parent 38bb0d8 commit d036b35
Copy full SHA for d036b35

File tree

Expand file treeCollapse file tree

2 files changed

+38
-0
lines changed
Open diff view settings
Filter options
Expand file treeCollapse file tree

2 files changed

+38
-0
lines changed
Open diff view settings
Collapse file

‎.eslintrc‎

Copy file name to clipboardExpand all lines: .eslintrc
+2Lines changed: 2 additions & 0 deletions
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:
Collapse file
+36Lines changed: 36 additions & 0 deletions
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 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.