diff --git a/packages/eslint-plugin/docs/rules/no-throw-literal.md b/packages/eslint-plugin/docs/rules/no-throw-literal.md index bffbed755f3..ea99d592fe7 100644 --- a/packages/eslint-plugin/docs/rules/no-throw-literal.md +++ b/packages/eslint-plugin/docs/rules/no-throw-literal.md @@ -3,7 +3,7 @@ It is considered good practice to only `throw` the `Error` object itself or an object using the `Error` object as base objects for user-defined exceptions. The fundamental benefit of `Error` objects is that they automatically keep track of where they were built and originated. -This rule restricts what can be thrown as an exception. When it was first created, it only prevented literals from being thrown (hence the name), but it has now been expanded to only allow expressions which have a possibility of being an `Error` object. +This rule restricts what can be thrown as an exception. When it was first created, it only prevented literals from being thrown (hence the name), but it has now been expanded to only allow expressions which have a possibility of being an `Error` object. With the `allowThrowingAny` and `allowThrowingUnknown`, it can be configured to only allow throwing values which are guaranteed to be an instance of `Error`. ## Rule Details @@ -93,6 +93,20 @@ throw new CustomError(); } ``` +### Options + +```jsonc +{ + "@typescript-eslint/no-throw-literal": [ + "error", + { + "allowThrowingAny": true, // Default is to allow throwing values of type any + "allowThrowingUnknown": true // Default is to allow throwing values of type unknown + } + ] +} +``` + --- diff --git a/packages/eslint-plugin/src/rules/no-throw-literal.ts b/packages/eslint-plugin/src/rules/no-throw-literal.ts index 178f1f39f9c..152e9348378 100644 --- a/packages/eslint-plugin/src/rules/no-throw-literal.ts +++ b/packages/eslint-plugin/src/rules/no-throw-literal.ts @@ -5,7 +5,16 @@ import { AST_NODE_TYPES, } from '@typescript-eslint/experimental-utils'; -export default util.createRule({ +type MessageIds = 'object' | 'undef'; + +type Options = [ + { + allowThrowingAny?: boolean; + allowThrowingUnknown?: boolean; + }, +]; + +export default util.createRule({ name: 'no-throw-literal', meta: { type: 'problem', @@ -15,14 +24,32 @@ export default util.createRule({ extendsBaseRule: true, requiresTypeChecking: true, }, - schema: [], + schema: [ + { + type: 'object', + properties: { + allowThrowingAny: { + type: 'boolean', + }, + allowThrowingUnknown: { + type: 'boolean', + }, + }, + additionalProperties: false, + }, + ], messages: { object: 'Expected an error object to be thrown.', undef: 'Do not throw undefined.', }, }, - defaultOptions: [], - create(context) { + defaultOptions: [ + { + allowThrowingAny: true, + allowThrowingUnknown: true, + }, + ], + create(context, [options]) { const parserServices = util.getParserServices(context); const program = parserServices.program; const checker = program.getTypeChecker(); @@ -77,11 +104,15 @@ export default util.createRule({ return; } - if ( - util.isTypeAnyType(type) || - util.isTypeUnknownType(type) || - isErrorLike(type) - ) { + if (options.allowThrowingAny && util.isTypeAnyType(type)) { + return; + } + + if (options.allowThrowingUnknown && util.isTypeUnknownType(type)) { + return; + } + + if (isErrorLike(type)) { return; } diff --git a/packages/eslint-plugin/tests/rules/no-throw-literal.test.ts b/packages/eslint-plugin/tests/rules/no-throw-literal.test.ts index c8ee6e3eb9c..30cd30c1f5a 100644 --- a/packages/eslint-plugin/tests/rules/no-throw-literal.test.ts +++ b/packages/eslint-plugin/tests/rules/no-throw-literal.test.ts @@ -123,6 +123,16 @@ throw nullishError || new Error(); declare const nullishError: Error | undefined; throw nullishError ? nullishError : new Error(); `, + ` +function fun(value: any) { + throw value; +} + `, + ` +function fun(value: unknown) { + throw value; +} + `, ], invalid: [ { @@ -423,5 +433,39 @@ throw foo as string; `, errors: [{ messageId: 'object' }], }, + { + code: ` +function fun(value: any) { + throw value; +} + `, + errors: [ + { + messageId: 'object', + }, + ], + options: [ + { + allowThrowingAny: false, + }, + ], + }, + { + code: ` +function fun(value: unknown) { + throw value; +} + `, + errors: [ + { + messageId: 'object', + }, + ], + options: [ + { + allowThrowingUnknown: false, + }, + ], + }, ], });