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>
This commit is contained in:
Minho Yoo authored and Antoine du Hamel committed 2026-11-23 12:34:22 +01:00
1 parent b6567ea812
commit c3e7d57b70
3 files changed
+6

No files matched your search

+1
View File
@@ -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);
}
+1
View File
@@ -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',
+4
View File
@@ -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()',