-
Notifications
You must be signed in to change notification settings - Fork 30.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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>
- Loading branch information
1 parent
38bb0d8
commit d036b35
Showing
2 changed files
with
38 additions
and
0 deletions.
There are no files selected for viewing
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
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
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 | ||
}; |