From b2a08fb13055c76186df95b9345523ae7191bdcf Mon Sep 17 00:00:00 2001 From: Rich Trott Date: Wed, 5 Apr 2017 19:11:48 -0700 Subject: [PATCH] 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 Reviewed-By: Richard Lau Reviewed-By: Colin Ihrig Reviewed-By: Teddy Katz Reviewed-By: Gibson Fahnestock --- .eslintrc.yaml | 8 ++++--- tools/eslint-rules/new-with-error.js | 31 ---------------------------- 2 files changed, 5 insertions(+), 34 deletions(-) delete mode 100644 tools/eslint-rules/new-with-error.js diff --git a/.eslintrc.yaml b/.eslintrc.yaml index 9665a635910b44..d805997f05ec74 100644 --- a/.eslintrc.yaml +++ b/.eslintrc.yaml @@ -109,8 +109,11 @@ 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 operator-linebreak: [2, after, {overrides: {'?': ignore, ':': ignore}}] @@ -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 diff --git a/tools/eslint-rules/new-with-error.js b/tools/eslint-rules/new-with-error.js deleted file mode 100644 index 655f34bf080956..00000000000000 --- a/tools/eslint-rules/new-with-error.js +++ /dev/null @@ -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 -};