Skip to content

Commit 2ab7bc0

Browse files
irontaeknodejs-github-bot
authored andcommitted
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: #66647 Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Chemi Atlow <chemi@atlow.co.il>
1 parent 8fd96f4 commit 2ab7bc0

3 files changed

Lines changed: 6 additions & 0 deletions

File tree

‎benchmark/error/error.js‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ const bench = common.createBenchmark(main, {
99
function main({ n }) {
1010
bench.start();
1111
for (let i = 0; i < n; ++i)
12+
// eslint-disable-next-line no-restricted-syntax
1213
new Error('test');
1314
bench.end(n);
1415
}

‎test/parallel/test-errors-systemerror.js‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ const { E, SystemError, codes, kIsNodeError } = require('internal/errors');
77
const { inspect } = require('internal/util/inspect');
88

99
assert.throws(
10+
// eslint-disable-next-line no-restricted-syntax
1011
() => { new SystemError(); },
1112
{
1213
name: 'TypeError',

‎tools/eslint/eslint.config_utils.mjs‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@ export const noRestrictedSyntaxCommonAll = [
1616
selector: 'ThrowStatement > CallExpression[callee.name=/Error$/]',
1717
message: 'Use `new` keyword when throwing an `Error`.',
1818
},
19+
{
20+
selector: 'ExpressionStatement > NewExpression[callee.name=/Error$/]',
21+
message: 'Throw, return, or assign the `Error`; constructing it alone does nothing.',
22+
},
1923
{
2024
selector: "CallExpression[callee.property.name='substr']",
2125
message: 'Use String.prototype.slice() or String.prototype.substring() instead of String.prototype.substr()',

0 commit comments

Comments
 (0)