Commit 2ab7bc0246e for nodejs
commit 2ab7bc0246e72dd7a54fcc267a9ce40a897bdd1d
Author: Minho Yoo <13810291+irontaek@users.noreply.github.com>
Date: Wed Oct 14 00:08:11 2026 +0900
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 <matteo.collina@gmail.com>
Reviewed-By: Chemi Atlow <chemi@atlow.co.il>
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()',