diff --git a/.eslintrc b/.eslintrc index 1d14b72abb2bf3..bf3fc3378f2d92 100644 --- a/.eslintrc +++ b/.eslintrc @@ -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: diff --git a/test/addons/make-callback/test.js b/test/addons/make-callback/test.js index 80ea0db796ab30..f3c98770ef335a 100644 --- a/test/addons/make-callback/test.js +++ b/test/addons/make-callback/test.js @@ -40,7 +40,7 @@ assert.strictEqual(42, makeCallback(recv, 'two', 1337)); const target = vm.runInNewContext(` (function($Object) { if (Object === $Object) - throw Error('bad'); + throw new Error('bad'); return Object; }) `); @@ -55,7 +55,7 @@ const forward = vm.runInNewContext(` // Runs in outer context. const endpoint = function($Object) { if (Object === $Object) - throw Error('bad'); + throw new Error('bad'); return Object; }; assert.strictEqual(Object, makeCallback(process, forward, endpoint)); diff --git a/test/parallel/test-assert.js b/test/parallel/test-assert.js index 64d2eb9d9ec565..0b300e737b367d 100644 --- a/test/parallel/test-assert.js +++ b/test/parallel/test-assert.js @@ -466,6 +466,6 @@ testBlockTypeError(assert.doesNotThrow, undefined); // https://github.com/nodejs/node/issues/3275 assert.throws(() => { throw 'error'; }, err => err === 'error'); -assert.throws(() => { throw Error(); }, err => err instanceof Error); +assert.throws(() => { throw new Error(); }, err => err instanceof Error); console.log('All OK'); diff --git a/test/parallel/test-http-mutable-headers.js b/test/parallel/test-http-mutable-headers.js index 2a60117328c580..af8fd6d7e8f938 100644 --- a/test/parallel/test-http-mutable-headers.js +++ b/test/parallel/test-http-mutable-headers.js @@ -110,7 +110,7 @@ function nextTest() { break; default: - throw Error('?'); + throw new Error('?'); } response.setEncoding('utf8'); diff --git a/test/pummel/test-regress-GH-814.js b/test/pummel/test-regress-GH-814.js index 4ed4b31ae28143..820903d68873e7 100644 --- a/test/pummel/test-regress-GH-814.js +++ b/test/pummel/test-regress-GH-814.js @@ -46,7 +46,7 @@ var timeToQuit = Date.now() + 8e3; //Test during no more than this seconds. } } else { - throw Error("Buffer GC'ed test -> FAIL"); + throw new Error("Buffer GC'ed test -> FAIL"); } if (Date.now() < timeToQuit) { diff --git a/test/pummel/test-regress-GH-814_2.js b/test/pummel/test-regress-GH-814_2.js index 9de1a2c3f3b86b..d7c6d947919d59 100644 --- a/test/pummel/test-regress-GH-814_2.js +++ b/test/pummel/test-regress-GH-814_2.js @@ -21,7 +21,7 @@ function tailCB(data) { console.error('[FAIL]\n DATA -> '); console.error(data); console.error('\n'); - throw Error('Buffers GC test -> FAIL'); + throw new Error('Buffers GC test -> FAIL'); } } diff --git a/tools/eslint-rules/new-with-error.js b/tools/eslint-rules/new-with-error.js new file mode 100644 index 00000000000000..b0f550db2ffa2c --- /dev/null +++ b/tools/eslint-rules/new-with-error.js @@ -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 +};