From c3e7d57b70c27318f96255e08de9b03e17cdd57c Mon Sep 17 00:00:00 2001 From: Minho Yoo <13810291+irontaek@users.noreply.github.com> Date: Wed, 14 Oct 2026 00:08:11 +0900 Subject: [PATCH] tools: disallow constructing an Error without using it Add a no-restricted-syntax selector next to the existing ThrowStatement one. It would have caught the benchmark bug fixed in the previous commit; the two intentional sites are disabled inline. Assisted-by: a closed-source coding agent Signed-off-by: Minho Yoo <13810291+irontaek@users.noreply.github.com> PR-URL: https://github.com/nodejs/node/pull/66647 Reviewed-By: Matteo Collina Reviewed-By: Chemi Atlow --- benchmark/error/error.js | 1 + test/parallel/test-errors-systemerror.js | 1 + tools/eslint/eslint.config_utils.mjs | 4 ++++ 3 files changed, 6 insertions(+) diff --git a/benchmark/error/error.js b/benchmark/error/error.js index c856f3e07f6..b4b5292d041 100644 --- a/benchmark/error/error.js +++ b/benchmark/error/error.js @@ -9,6 +9,7 @@ const bench = common.createBenchmark(main, { function main({ n }) { bench.start(); for (let i = 0; i < n; ++i) + // eslint-disable-next-line no-restricted-syntax new Error('test'); bench.end(n); } diff --git a/test/parallel/test-errors-systemerror.js b/test/parallel/test-errors-systemerror.js index 629a3d1c086..651e7f8d896 100644 --- a/test/parallel/test-errors-systemerror.js +++ b/test/parallel/test-errors-systemerror.js @@ -7,6 +7,7 @@ const { E, SystemError, codes, kIsNodeError } = require('internal/errors'); const { inspect } = require('internal/util/inspect'); assert.throws( + // eslint-disable-next-line no-restricted-syntax () => { new SystemError(); }, { name: 'TypeError', diff --git a/tools/eslint/eslint.config_utils.mjs b/tools/eslint/eslint.config_utils.mjs index 7b09047937b..e6008c45793 100644 --- a/tools/eslint/eslint.config_utils.mjs +++ b/tools/eslint/eslint.config_utils.mjs @@ -16,6 +16,10 @@ export const noRestrictedSyntaxCommonAll = [ selector: 'ThrowStatement > CallExpression[callee.name=/Error$/]', message: 'Use `new` keyword when throwing an `Error`.', }, + { + selector: 'ExpressionStatement > NewExpression[callee.name=/Error$/]', + message: 'Throw, return, or assign the `Error`; constructing it alone does nothing.', + }, { selector: "CallExpression[callee.property.name='substr']", message: 'Use String.prototype.slice() or String.prototype.substring() instead of String.prototype.substr()',