mirror of
https://github.com/zebrajr/node.git
synced 2026-01-15 12:15:26 +00:00
tools: enforce throw new Error() with lint rule
Add linting rule requiring `throw new Error()` over `throw Error()`. PR-URL: https://github.com/nodejs/node/pull/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>
This commit is contained in:
@@ -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:
|
||||
|
||||
36
tools/eslint-rules/new-with-error.js
Normal file
36
tools/eslint-rules/new-with-error.js
Normal file
@@ -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
|
||||
};
|
||||
Reference in New Issue
Block a user