From 342935d9632334b10941df43560a108af45caf15 Mon Sep 17 00:00:00 2001 From: Daniel Playfair Cal Date: Tue, 23 Nov 2021 14:38:24 +1100 Subject: [PATCH 1/2] feat: add options to no-throw-literal to disallow any/unknown --- .../docs/rules/no-throw-literal.md | 16 +++++- .../src/rules/no-throw-literal.ts | 49 +++++++++++++++---- .../tests/rules/no-throw-literal.test.ts | 44 +++++++++++++++++ 3 files changed, 99 insertions(+), 10 deletions(-) diff --git a/packages/eslint-plugin/docs/rules/no-throw-literal.md b/packages/eslint-plugin/docs/rules/no-throw-literal.md index 043dc8c3de5..b4b120864c5 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..b60ff0420da 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, + }, + ], + }, ], }); From c8e8941d54ae6a5d90d66a33dff10b3977a69317 Mon Sep 17 00:00:00 2001 From: Daniel Playfair Cal Date: Tue, 23 Nov 2021 15:24:57 +1100 Subject: [PATCH 2/2] fix: eslint errors --- packages/eslint-plugin/tests/rules/no-throw-literal.test.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) 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 b60ff0420da..30cd30c1f5a 100644 --- a/packages/eslint-plugin/tests/rules/no-throw-literal.test.ts +++ b/packages/eslint-plugin/tests/rules/no-throw-literal.test.ts @@ -438,7 +438,7 @@ throw foo as string; function fun(value: any) { throw value; } - `, + `, errors: [ { messageId: 'object', @@ -455,7 +455,7 @@ function fun(value: any) { function fun(value: unknown) { throw value; } - `, + `, errors: [ { messageId: 'object',