From 6400c3b337ba585ff8af4728ee2c4216150ddef9 Mon Sep 17 00:00:00 2001 From: James Garbutt <43081j@users.noreply.github.com> Date: Mon, 21 Jul 2025 10:06:57 +0100 Subject: [PATCH] feat(eslint-plugin): support yield/await expressions It seems we can actually support yield and await as the type checker will already resolve the yielded/awaited type. When yielding, it is possible a generator can be typed such that the `yield` resolves to an `Error`. --- .../src/rules/only-throw-error.ts | 7 --- .../tests/rules/only-throw-error.test.ts | 58 +++++++++++++++++++ 2 files changed, 58 insertions(+), 7 deletions(-) diff --git a/packages/eslint-plugin/src/rules/only-throw-error.ts b/packages/eslint-plugin/src/rules/only-throw-error.ts index 187e6ac5e5b1..ec5de09d13f0 100644 --- a/packages/eslint-plugin/src/rules/only-throw-error.ts +++ b/packages/eslint-plugin/src/rules/only-throw-error.ts @@ -143,13 +143,6 @@ export default createRule({ } function checkThrowArgument(node: TSESTree.Node): void { - if ( - node.type === AST_NODE_TYPES.AwaitExpression || - node.type === AST_NODE_TYPES.YieldExpression - ) { - return; - } - if (options.allowRethrowing && isRethrownError(node)) { return; } diff --git a/packages/eslint-plugin/tests/rules/only-throw-error.test.ts b/packages/eslint-plugin/tests/rules/only-throw-error.test.ts index a85d65cd85e5..943403abca9c 100644 --- a/packages/eslint-plugin/tests/rules/only-throw-error.test.ts +++ b/packages/eslint-plugin/tests/rules/only-throw-error.test.ts @@ -243,6 +243,30 @@ Promise.reject('foo').catch(e => { }, ], }, + { + code: ` +async function foo() { + throw await Promise.resolve(new Error('error')); +} + `, + options: [ + { + allowThrowingAny: false, + }, + ], + }, + { + code: ` +function *foo(): Generator { + throw yield 303; +} + `, + options: [ + { + allowThrowingAny: false, + }, + ], + }, ], invalid: [ { @@ -745,5 +769,39 @@ Promise.reject('foo').then(e => { }, ], }, + { + code: ` +async function foo() { + throw await bar; +} + `, + errors: [ + { + messageId: 'object', + }, + ], + options: [ + { + allowThrowingAny: false, + }, + ], + }, + { + code: ` +async function foo() { + throw await Promise.resolve(303); +} + `, + errors: [ + { + messageId: 'object', + }, + ], + options: [ + { + allowThrowingAny: false, + }, + ], + }, ], }); pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy