Browse Source

tools: replace custom new-with-error rule

Use no-restricted-syntax to implement the requirement that `Error`
objects must be thrown with the `new` keyword.

PR-URL: https://github.com/nodejs/node/pull/12249
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Richard Lau <riclau@uk.ibm.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Teddy Katz <teddy.katz@gmail.com>
Reviewed-By: Gibson Fahnestock <gibfahn@gmail.com>
v6.x
Rich Trott 8 years ago
committed by Myles Borins
parent
commit
b2a08fb130
No known key found for this signature in database GPG Key ID: 933B01F40B5CA946
  1. 6
      .eslintrc.yaml
  2. 31
      tools/eslint-rules/new-with-error.js

6
.eslintrc.yaml

@ -109,7 +109,10 @@ rules:
message: "setTimeout() must be invoked with at least two arguments."
}, {
selector: "CallExpression[callee.name='setInterval'][arguments.length<2]",
message: "setInterval() must be invoked with at least 2 arguments"
message: "setInterval() must be invoked with at least 2 arguments."
}, {
selector: "ThrowStatement > CallExpression[callee.name=/Error$/]",
message: "Use new keyword when throwing an Error."
}]
no-tabs: 2
no-trailing-spaces: 2
@ -142,7 +145,6 @@ rules:
align-multiline-assignment: 2
assert-fail-single-argument: 2
assert-throws-arguments: [2, { requireTwo: false }]
new-with-error: [2, Error, RangeError, TypeError, SyntaxError, ReferenceError]
no-useless-regex-char-class-escape: [2, { override: ['[', ']'] }]
# Global scoped method and vars

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

@ -1,31 +0,0 @@
/**
* @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',
'additionalItems': {
'type': 'string'
},
'uniqueItems': true
};
Loading…
Cancel
Save