From 9deeed32999a8076b0bfa3479c96b3ec87d46199 Mon Sep 17 00:00:00 2001 From: Josh Goldberg Date: Sun, 17 Mar 2024 13:16:24 -0400 Subject: [PATCH 1/4] feat(eslint-plugin): deprecate no-throw-literal and add a renamed only-throw-error --- .../docs/rules/no-throw-literal.mdx | 104 +--- .../docs/rules/only-throw-error.mdx | 115 +++++ packages/eslint-plugin/src/configs/all.ts | 4 +- .../src/configs/disable-type-checked.ts | 1 + .../src/configs/strict-type-checked-only.ts | 4 +- .../src/configs/strict-type-checked.ts | 4 +- packages/eslint-plugin/src/rules/index.ts | 2 + .../src/rules/no-throw-literal.ts | 3 +- .../src/rules/only-throw-error.ts | 98 ++++ .../tests/rules/only-throw-error.test.ts | 473 ++++++++++++++++++ packages/typescript-eslint/src/configs/all.ts | 4 +- .../src/configs/disable-type-checked.ts | 1 + .../src/configs/strict-type-checked-only.ts | 4 +- .../src/configs/strict-type-checked.ts | 4 +- 14 files changed, 711 insertions(+), 110 deletions(-) create mode 100644 packages/eslint-plugin/docs/rules/only-throw-error.mdx create mode 100644 packages/eslint-plugin/src/rules/only-throw-error.ts create mode 100644 packages/eslint-plugin/tests/rules/only-throw-error.test.ts diff --git a/packages/eslint-plugin/docs/rules/no-throw-literal.mdx b/packages/eslint-plugin/docs/rules/no-throw-literal.mdx index b5d57c5533f5..09d572e6a722 100644 --- a/packages/eslint-plugin/docs/rules/no-throw-literal.mdx +++ b/packages/eslint-plugin/docs/rules/no-throw-literal.mdx @@ -12,104 +12,14 @@ import TabItem from '@theme/TabItem'; 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. With the `allowThrowingAny` and `allowThrowingUnknown`, it can be configured to only allow throwing values which are guaranteed to be an instance of `Error`. +This rule restricts what can be thrown as an exception. -## Examples +:::warn +This rule is being renamed to [`only-throw-error`](./only-throw-error.mdx). +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` options, it can be configured to only allow throwing values which are guaranteed to be an instance of `Error`. -This rule is aimed at maintaining consistency when throwing exception by disallowing to throw literals and other expressions which cannot possibly be an `Error` object. - - - - -```ts -throw 'error'; - -throw 0; - -throw undefined; - -throw null; - -const err = new Error(); -throw 'an ' + err; - -const err = new Error(); -throw `${err}`; - -const err = ''; -throw err; - -function err() { - return ''; -} -throw err(); - -const foo = { - bar: '', -}; -throw foo.bar; -``` - - - - -```ts -throw new Error(); - -throw new Error('error'); - -const e = new Error('error'); -throw e; - -try { - throw new Error('error'); -} catch (e) { - throw e; -} - -const err = new Error(); -throw err; - -function err() { - return new Error(); -} -throw err(); - -const foo = { - bar: new Error(), -}; -throw foo.bar; - -class CustomError extends Error { - // ... -} -throw new CustomError(); -``` - - - - -## Options - -This rule adds the following options: - -```ts -interface Options { - /** - * Whether to always allow throwing values typed as `any`. - */ - allowThrowingAny?: boolean; - - /** - * Whether to always allow throwing values typed as `unknown`. - */ - allowThrowingUnknown?: boolean; -} - -const defaultOptions: Options = { - allowThrowingAny: false, - allowThrowingUnknown: false, -}; -``` +The current name `no-throw-literal` will be removed in a future major version of typescript-eSLint. +::: {/* Intentionally Omitted: When Not To Use It */} diff --git a/packages/eslint-plugin/docs/rules/only-throw-error.mdx b/packages/eslint-plugin/docs/rules/only-throw-error.mdx new file mode 100644 index 000000000000..fd9d8f2c4d25 --- /dev/null +++ b/packages/eslint-plugin/docs/rules/only-throw-error.mdx @@ -0,0 +1,115 @@ +--- +description: 'Disallow throwing non-`Error` values as exceptions.' +--- + +import Tabs from '@theme/Tabs'; +import TabItem from '@theme/TabItem'; + +> 🛑 This file is source code, not the primary documentation location! 🛑 +> +> See **https://typescript-eslint.io/rules/only-throw-error** for documentation. + +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. + +## Examples + +This rule is aimed at maintaining consistency when throwing exception by disallowing to throw literals and other expressions which cannot possibly be an `Error` object. + + + + +```ts +throw 'error'; + +throw 0; + +throw undefined; + +throw null; + +const err = new Error(); +throw 'an ' + err; + +const err = new Error(); +throw `${err}`; + +const err = ''; +throw err; + +function err() { + return ''; +} +throw err(); + +const foo = { + bar: '', +}; +throw foo.bar; +``` + + + + +```ts +throw new Error(); + +throw new Error('error'); + +const e = new Error('error'); +throw e; + +try { + throw new Error('error'); +} catch (e) { + throw e; +} + +const err = new Error(); +throw err; + +function err() { + return new Error(); +} +throw err(); + +const foo = { + bar: new Error(), +}; +throw foo.bar; + +class CustomError extends Error { + // ... +} +throw new CustomError(); +``` + + + + +## Options + +This rule adds the following options: + +```ts +interface Options { + /** + * Whether to always allow throwing values typed as `any`. + */ + allowThrowingAny?: boolean; + + /** + * Whether to always allow throwing values typed as `unknown`. + */ + allowThrowingUnknown?: boolean; +} + +const defaultOptions: Options = { + allowThrowingAny: false, + allowThrowingUnknown: false, +}; +``` + +{/* Intentionally Omitted: When Not To Use It */} diff --git a/packages/eslint-plugin/src/configs/all.ts b/packages/eslint-plugin/src/configs/all.ts index b8711f0cc6a7..1e7304e45a36 100644 --- a/packages/eslint-plugin/src/configs/all.ts +++ b/packages/eslint-plugin/src/configs/all.ts @@ -90,8 +90,6 @@ export = { 'no-shadow': 'off', '@typescript-eslint/no-shadow': 'error', '@typescript-eslint/no-this-alias': 'error', - 'no-throw-literal': 'off', - '@typescript-eslint/no-throw-literal': 'error', '@typescript-eslint/no-unnecessary-boolean-literal-compare': 'error', '@typescript-eslint/no-unnecessary-condition': 'error', '@typescript-eslint/no-unnecessary-qualifier': 'error', @@ -118,6 +116,8 @@ export = { '@typescript-eslint/no-useless-template-literals': 'error', '@typescript-eslint/no-var-requires': 'error', '@typescript-eslint/non-nullable-type-assertion-style': 'error', + 'no-throw-literal': 'off', + '@typescript-eslint/only-throw-error': 'error', '@typescript-eslint/parameter-properties': 'error', '@typescript-eslint/prefer-as-const': 'error', 'prefer-destructuring': 'off', diff --git a/packages/eslint-plugin/src/configs/disable-type-checked.ts b/packages/eslint-plugin/src/configs/disable-type-checked.ts index 392e01ce304a..fd3a8c2d7cbd 100644 --- a/packages/eslint-plugin/src/configs/disable-type-checked.ts +++ b/packages/eslint-plugin/src/configs/disable-type-checked.ts @@ -41,6 +41,7 @@ export = { '@typescript-eslint/no-unsafe-unary-minus': 'off', '@typescript-eslint/no-useless-template-literals': 'off', '@typescript-eslint/non-nullable-type-assertion-style': 'off', + '@typescript-eslint/only-throw-error': 'off', '@typescript-eslint/prefer-destructuring': 'off', '@typescript-eslint/prefer-find': 'off', '@typescript-eslint/prefer-includes': 'off', diff --git a/packages/eslint-plugin/src/configs/strict-type-checked-only.ts b/packages/eslint-plugin/src/configs/strict-type-checked-only.ts index cbfaa5b73b80..444f8e5aab31 100644 --- a/packages/eslint-plugin/src/configs/strict-type-checked-only.ts +++ b/packages/eslint-plugin/src/configs/strict-type-checked-only.ts @@ -23,8 +23,6 @@ export = { '@typescript-eslint/no-misused-promises': 'error', '@typescript-eslint/no-mixed-enums': 'error', '@typescript-eslint/no-redundant-type-constituents': 'error', - 'no-throw-literal': 'off', - '@typescript-eslint/no-throw-literal': 'error', '@typescript-eslint/no-unnecessary-boolean-literal-compare': 'error', '@typescript-eslint/no-unnecessary-condition': 'error', '@typescript-eslint/no-unnecessary-type-arguments': 'error', @@ -36,6 +34,8 @@ export = { '@typescript-eslint/no-unsafe-member-access': 'error', '@typescript-eslint/no-unsafe-return': 'error', '@typescript-eslint/no-useless-template-literals': 'error', + 'no-throw-literal': 'off', + '@typescript-eslint/only-throw-error': 'error', '@typescript-eslint/prefer-includes': 'error', 'prefer-promise-reject-errors': 'off', '@typescript-eslint/prefer-promise-reject-errors': 'error', diff --git a/packages/eslint-plugin/src/configs/strict-type-checked.ts b/packages/eslint-plugin/src/configs/strict-type-checked.ts index 6d838db31566..82c676812992 100644 --- a/packages/eslint-plugin/src/configs/strict-type-checked.ts +++ b/packages/eslint-plugin/src/configs/strict-type-checked.ts @@ -44,8 +44,6 @@ export = { '@typescript-eslint/no-non-null-assertion': 'error', '@typescript-eslint/no-redundant-type-constituents': 'error', '@typescript-eslint/no-this-alias': 'error', - 'no-throw-literal': 'off', - '@typescript-eslint/no-throw-literal': 'error', '@typescript-eslint/no-unnecessary-boolean-literal-compare': 'error', '@typescript-eslint/no-unnecessary-condition': 'error', '@typescript-eslint/no-unnecessary-type-arguments': 'error', @@ -64,6 +62,8 @@ export = { '@typescript-eslint/no-useless-constructor': 'error', '@typescript-eslint/no-useless-template-literals': 'error', '@typescript-eslint/no-var-requires': 'error', + 'no-throw-literal': 'off', + '@typescript-eslint/only-throw-error': 'error', '@typescript-eslint/prefer-as-const': 'error', '@typescript-eslint/prefer-includes': 'error', '@typescript-eslint/prefer-literal-enum-member': 'error', diff --git a/packages/eslint-plugin/src/rules/index.ts b/packages/eslint-plugin/src/rules/index.ts index 3534b6780c2c..64c6a872ed69 100644 --- a/packages/eslint-plugin/src/rules/index.ts +++ b/packages/eslint-plugin/src/rules/index.ts @@ -101,6 +101,7 @@ import noUselessTemplateLiterals from './no-useless-template-literals'; import noVarRequires from './no-var-requires'; import nonNullableTypeAssertionStyle from './non-nullable-type-assertion-style'; import objectCurlySpacing from './object-curly-spacing'; +import onlyThrowError from './only-throw-error'; import paddingLineBetweenStatements from './padding-line-between-statements'; import parameterProperties from './parameter-properties'; import preferAsConst from './prefer-as-const'; @@ -245,6 +246,7 @@ export default { 'no-var-requires': noVarRequires, 'non-nullable-type-assertion-style': nonNullableTypeAssertionStyle, 'object-curly-spacing': objectCurlySpacing, + 'only-throw-error': onlyThrowError, 'padding-line-between-statements': paddingLineBetweenStatements, 'parameter-properties': parameterProperties, 'prefer-as-const': preferAsConst, diff --git a/packages/eslint-plugin/src/rules/no-throw-literal.ts b/packages/eslint-plugin/src/rules/no-throw-literal.ts index f3f5937c7379..d893dbc164a5 100644 --- a/packages/eslint-plugin/src/rules/no-throw-literal.ts +++ b/packages/eslint-plugin/src/rules/no-throw-literal.ts @@ -23,9 +23,10 @@ export default createRule({ name: 'no-throw-literal', meta: { type: 'problem', + deprecated: true, + replacedBy: ['@typescript-eslint/only-throw-error'], docs: { description: 'Disallow throwing literals as exceptions', - recommended: 'strict', extendsBaseRule: true, requiresTypeChecking: true, }, diff --git a/packages/eslint-plugin/src/rules/only-throw-error.ts b/packages/eslint-plugin/src/rules/only-throw-error.ts new file mode 100644 index 000000000000..62ce268fc700 --- /dev/null +++ b/packages/eslint-plugin/src/rules/only-throw-error.ts @@ -0,0 +1,98 @@ +import type { TSESTree } from '@typescript-eslint/utils'; +import { AST_NODE_TYPES } from '@typescript-eslint/utils'; +import * as ts from 'typescript'; + +import { + createRule, + getParserServices, + isErrorLike, + isTypeAnyType, + isTypeUnknownType, +} from '../util'; + +type MessageIds = 'object' | 'undef'; + +type Options = [ + { + allowThrowingAny?: boolean; + allowThrowingUnknown?: boolean; + }, +]; + +export default createRule({ + name: 'only-throw-error', + meta: { + type: 'problem', + docs: { + description: 'Disallow throwing non-`Error` values as exceptions', + recommended: 'strict', + extendsBaseRule: 'no-throw-literal', + requiresTypeChecking: true, + }, + 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: [ + { + allowThrowingAny: true, + allowThrowingUnknown: true, + }, + ], + create(context, [options]) { + const services = getParserServices(context); + + function checkThrowArgument(node: TSESTree.Node): void { + if ( + node.type === AST_NODE_TYPES.AwaitExpression || + node.type === AST_NODE_TYPES.YieldExpression + ) { + return; + } + + const type = services.getTypeAtLocation(node); + + if (type.flags & ts.TypeFlags.Undefined) { + context.report({ node, messageId: 'undef' }); + return; + } + + if (options.allowThrowingAny && isTypeAnyType(type)) { + return; + } + + if (options.allowThrowingUnknown && isTypeUnknownType(type)) { + return; + } + + if (isErrorLike(services.program, type)) { + return; + } + + context.report({ node, messageId: 'object' }); + } + + return { + ThrowStatement(node): void { + if (node.argument) { + checkThrowArgument(node.argument); + } + }, + }; + }, +}); diff --git a/packages/eslint-plugin/tests/rules/only-throw-error.test.ts b/packages/eslint-plugin/tests/rules/only-throw-error.test.ts new file mode 100644 index 000000000000..57fa09d86b55 --- /dev/null +++ b/packages/eslint-plugin/tests/rules/only-throw-error.test.ts @@ -0,0 +1,473 @@ +import { RuleTester } from '@typescript-eslint/rule-tester'; + +import rule from '../../src/rules/only-throw-error'; +import { getFixturesRootDir } from '../RuleTester'; + +const ruleTester = new RuleTester({ + parserOptions: { + ecmaVersion: 2018, + sourceType: 'module', + tsconfigRootDir: getFixturesRootDir(), + project: './tsconfig.json', + }, + parser: '@typescript-eslint/parser', +}); + +ruleTester.run('only-throw-error', rule, { + valid: [ + 'throw new Error();', + "throw new Error('error');", + "throw Error('error');", + ` +const e = new Error(); +throw e; + `, + ` +try { + throw new Error(); +} catch (e) { + throw e; +} + `, + ` +function foo() { + return new Error(); +} +throw foo(); + `, + ` +const foo = { + bar: new Error(), +}; +throw foo.bar; + `, + ` +const foo = { + bar: new Error(), +}; + +throw foo['bar']; + `, + ` +const foo = { + bar: new Error(), +}; + +const bar = 'bar'; +throw foo[bar]; + `, + ` +class CustomError extends Error {} +throw new CustomError(); + `, + ` +class CustomError1 extends Error {} +class CustomError2 extends CustomError1 {} +throw new CustomError2(); + `, + 'throw (foo = new Error());', + 'throw (1, 2, new Error());', + "throw 'literal' && new Error();", + "throw new Error() || 'literal';", + 'throw foo ? new Error() : new Error();', + ` +function* foo() { + let index = 0; + throw yield index++; +} + `, + ` +async function foo() { + throw await bar; +} + `, + ` +import { Error } from './missing'; +throw Error; + `, + ` +class CustomError extends Error {} +throw new CustomError(); + `, + ` +class CustomError extends Error {} +throw new CustomError(); + `, + ` +class CustomError extends Error {} +throw new CustomError(); + `, + ` +function foo() { + throw Object.assign(new Error('message'), { foo: 'bar' }); +} + `, + ` +const foo: Error | SyntaxError = bar(); +function bar() { + throw foo; +} + `, + ` +declare const foo: Error | string; +throw foo as Error; + `, + 'throw new Error() as Error;', + ` +declare const nullishError: Error | undefined; +throw nullishError ?? new Error(); + `, + ` +declare const nullishError: Error | undefined; +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: [ + { + code: 'throw undefined;', + errors: [ + { + messageId: 'undef', + }, + ], + }, + { + code: "throw new String('');", + errors: [ + { + messageId: 'object', + }, + ], + }, + { + code: "throw 'error';", + errors: [ + { + messageId: 'object', + }, + ], + }, + { + code: 'throw 0;', + errors: [ + { + messageId: 'object', + }, + ], + }, + { + code: 'throw false;', + errors: [ + { + messageId: 'object', + }, + ], + }, + { + code: 'throw null;', + errors: [ + { + messageId: 'object', + }, + ], + }, + { + code: 'throw {};', + errors: [ + { + messageId: 'object', + }, + ], + }, + { + code: "throw 'a' + 'b';", + errors: [ + { + messageId: 'object', + }, + ], + }, + { + code: ` +const a = ''; +throw a + 'b'; + `, + errors: [ + { + messageId: 'object', + }, + ], + }, + { + code: "throw (foo = 'error');", + errors: [ + { + messageId: 'object', + }, + ], + }, + { + code: 'throw (new Error(), 1, 2, 3);', + errors: [ + { + messageId: 'object', + }, + ], + }, + { + code: "throw 'literal' && 'not an Error';", + errors: [{ messageId: 'object' }], + }, + { + code: "throw 'literal' || new Error();", + errors: [{ messageId: 'object' }], + }, + { + code: "throw new Error() && 'literal';", + errors: [{ messageId: 'object' }], + }, + { + code: "throw 'literal' ?? new Error();", + errors: [{ messageId: 'object' }], + }, + { + code: "throw foo ? 'not an Error' : 'literal';", + errors: [{ messageId: 'object' }], + }, + { + code: "throw foo ? new Error() : 'literal';", + errors: [{ messageId: 'object' }], + }, + { + code: "throw foo ? 'literal' : new Error();", + errors: [{ messageId: 'object' }], + }, + { + code: 'throw `${err}`;', + errors: [ + { + messageId: 'object', + }, + ], + }, + { + code: ` +const err = 'error'; +throw err; + `, + errors: [ + { + messageId: 'object', + }, + ], + }, + { + code: ` +function foo(msg) {} +throw foo('error'); + `, + errors: [ + { + messageId: 'object', + }, + ], + }, + { + code: ` +const foo = { + msg: 'error', +}; +throw foo.msg; + `, + errors: [ + { + messageId: 'object', + }, + ], + }, + { + code: ` +const foo = { + msg: undefined, +}; +throw foo.msg; + `, + errors: [ + { + messageId: 'undef', + }, + ], + }, + { + code: ` +class CustomError {} +throw new CustomError(); + `, + errors: [ + { + messageId: 'object', + }, + ], + }, + { + code: ` +class Foo {} +class CustomError extends Foo {} +throw new CustomError(); + `, + errors: [ + { + messageId: 'object', + }, + ], + }, + { + code: ` +const Error = null; +throw Error; + `, + errors: [ + { + messageId: 'object', + }, + ], + }, + { + code: ` +import { Error } from './class'; +throw new Error(); + `, + errors: [ + { + messageId: 'object', + line: 3, + column: 7, + }, + ], + }, + { + code: ` +class CustomError extends Foo {} +throw new CustomError(); + `, + errors: [ + { + messageId: 'object', + line: 3, + column: 7, + }, + ], + }, + { + code: ` +function foo() { + const res: T; + throw res; +} + `, + errors: [ + { + messageId: 'object', + line: 4, + column: 9, + }, + ], + }, + { + code: ` +function foo(fn: () => Promise) { + const promise = fn(); + const res = promise.then(() => {}).catch(() => {}); + throw res; +} + `, + errors: [ + { + messageId: 'object', + line: 5, + column: 9, + }, + ], + }, + { + code: ` +function foo() { + throw Object.assign({ foo: 'foo' }, { bar: 'bar' }); +} + `, + errors: [ + { + messageId: 'object', + }, + ], + }, + { + code: ` +const foo: Error | { bar: string } = bar(); +function bar() { + throw foo; +} + `, + errors: [ + { + messageId: 'object', + }, + ], + }, + { + code: ` +declare const foo: Error | string; +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, + }, + ], + }, + ], +}); diff --git a/packages/typescript-eslint/src/configs/all.ts b/packages/typescript-eslint/src/configs/all.ts index ad4a85cd524c..893a6364f56b 100644 --- a/packages/typescript-eslint/src/configs/all.ts +++ b/packages/typescript-eslint/src/configs/all.ts @@ -98,8 +98,6 @@ export default ( 'no-shadow': 'off', '@typescript-eslint/no-shadow': 'error', '@typescript-eslint/no-this-alias': 'error', - 'no-throw-literal': 'off', - '@typescript-eslint/no-throw-literal': 'error', '@typescript-eslint/no-unnecessary-boolean-literal-compare': 'error', '@typescript-eslint/no-unnecessary-condition': 'error', '@typescript-eslint/no-unnecessary-qualifier': 'error', @@ -126,6 +124,8 @@ export default ( '@typescript-eslint/no-useless-template-literals': 'error', '@typescript-eslint/no-var-requires': 'error', '@typescript-eslint/non-nullable-type-assertion-style': 'error', + 'no-throw-literal': 'off', + '@typescript-eslint/only-throw-error': 'error', '@typescript-eslint/parameter-properties': 'error', '@typescript-eslint/prefer-as-const': 'error', 'prefer-destructuring': 'off', diff --git a/packages/typescript-eslint/src/configs/disable-type-checked.ts b/packages/typescript-eslint/src/configs/disable-type-checked.ts index cf6107d6c531..30187dd7f20a 100644 --- a/packages/typescript-eslint/src/configs/disable-type-checked.ts +++ b/packages/typescript-eslint/src/configs/disable-type-checked.ts @@ -43,6 +43,7 @@ export default ( '@typescript-eslint/no-unsafe-unary-minus': 'off', '@typescript-eslint/no-useless-template-literals': 'off', '@typescript-eslint/non-nullable-type-assertion-style': 'off', + '@typescript-eslint/only-throw-error': 'off', '@typescript-eslint/prefer-destructuring': 'off', '@typescript-eslint/prefer-find': 'off', '@typescript-eslint/prefer-includes': 'off', diff --git a/packages/typescript-eslint/src/configs/strict-type-checked-only.ts b/packages/typescript-eslint/src/configs/strict-type-checked-only.ts index 4c52c09b7376..373db581ec14 100644 --- a/packages/typescript-eslint/src/configs/strict-type-checked-only.ts +++ b/packages/typescript-eslint/src/configs/strict-type-checked-only.ts @@ -34,8 +34,6 @@ export default ( '@typescript-eslint/no-misused-promises': 'error', '@typescript-eslint/no-mixed-enums': 'error', '@typescript-eslint/no-redundant-type-constituents': 'error', - 'no-throw-literal': 'off', - '@typescript-eslint/no-throw-literal': 'error', '@typescript-eslint/no-unnecessary-boolean-literal-compare': 'error', '@typescript-eslint/no-unnecessary-condition': 'error', '@typescript-eslint/no-unnecessary-type-arguments': 'error', @@ -47,6 +45,8 @@ export default ( '@typescript-eslint/no-unsafe-member-access': 'error', '@typescript-eslint/no-unsafe-return': 'error', '@typescript-eslint/no-useless-template-literals': 'error', + 'no-throw-literal': 'off', + '@typescript-eslint/only-throw-error': 'error', '@typescript-eslint/prefer-includes': 'error', 'prefer-promise-reject-errors': 'off', '@typescript-eslint/prefer-promise-reject-errors': 'error', diff --git a/packages/typescript-eslint/src/configs/strict-type-checked.ts b/packages/typescript-eslint/src/configs/strict-type-checked.ts index e53f57934cea..de7d46d153e3 100644 --- a/packages/typescript-eslint/src/configs/strict-type-checked.ts +++ b/packages/typescript-eslint/src/configs/strict-type-checked.ts @@ -55,8 +55,6 @@ export default ( '@typescript-eslint/no-non-null-assertion': 'error', '@typescript-eslint/no-redundant-type-constituents': 'error', '@typescript-eslint/no-this-alias': 'error', - 'no-throw-literal': 'off', - '@typescript-eslint/no-throw-literal': 'error', '@typescript-eslint/no-unnecessary-boolean-literal-compare': 'error', '@typescript-eslint/no-unnecessary-condition': 'error', '@typescript-eslint/no-unnecessary-type-arguments': 'error', @@ -75,6 +73,8 @@ export default ( '@typescript-eslint/no-useless-constructor': 'error', '@typescript-eslint/no-useless-template-literals': 'error', '@typescript-eslint/no-var-requires': 'error', + 'no-throw-literal': 'off', + '@typescript-eslint/only-throw-error': 'error', '@typescript-eslint/prefer-as-const': 'error', '@typescript-eslint/prefer-includes': 'error', '@typescript-eslint/prefer-literal-enum-member': 'error', From fcea975b2a50fffa828ae2fcf14517b801fcb679 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Josh=20Goldberg=20=E2=9C=A8?= Date: Sat, 23 Mar 2024 13:46:00 -0400 Subject: [PATCH 2/4] Update packages/eslint-plugin/docs/rules/no-throw-literal.mdx Co-authored-by: YeonJuan --- packages/eslint-plugin/docs/rules/no-throw-literal.mdx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/eslint-plugin/docs/rules/no-throw-literal.mdx b/packages/eslint-plugin/docs/rules/no-throw-literal.mdx index 09d572e6a722..0317ef4bbd7e 100644 --- a/packages/eslint-plugin/docs/rules/no-throw-literal.mdx +++ b/packages/eslint-plugin/docs/rules/no-throw-literal.mdx @@ -19,7 +19,7 @@ This rule is being renamed to [`only-throw-error`](./only-throw-error.mdx). 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` options, it can be configured to only allow throwing values which are guaranteed to be an instance of `Error`. -The current name `no-throw-literal` will be removed in a future major version of typescript-eSLint. +The current name `no-throw-literal` will be removed in a future major version of typescript-eslint. ::: {/* Intentionally Omitted: When Not To Use It */} From 3f124f130276ad3021b70bdd51d8646b2553516d Mon Sep 17 00:00:00 2001 From: Josh Goldberg Date: Sat, 23 Mar 2024 14:23:32 -0400 Subject: [PATCH 3/4] Updated options snapshot --- .../schema-snapshots/only-throw-error.shot | 32 +++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 packages/eslint-plugin/tests/schema-snapshots/only-throw-error.shot diff --git a/packages/eslint-plugin/tests/schema-snapshots/only-throw-error.shot b/packages/eslint-plugin/tests/schema-snapshots/only-throw-error.shot new file mode 100644 index 000000000000..370beb0d1bd8 --- /dev/null +++ b/packages/eslint-plugin/tests/schema-snapshots/only-throw-error.shot @@ -0,0 +1,32 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`Rule schemas should be convertible to TS types for documentation purposes only-throw-error 1`] = ` +" +# SCHEMA: + +[ + { + "additionalProperties": false, + "properties": { + "allowThrowingAny": { + "type": "boolean" + }, + "allowThrowingUnknown": { + "type": "boolean" + } + }, + "type": "object" + } +] + + +# TYPES: + +type Options = [ + { + allowThrowingAny?: boolean; + allowThrowingUnknown?: boolean; + }, +]; +" +`; From a4c19effd9f98ade975fc6268e9c4baa706dad04 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Josh=20Goldberg=20=E2=9C=A8?= Date: Mon, 25 Mar 2024 13:01:26 -0400 Subject: [PATCH 4/4] Update packages/eslint-plugin/docs/rules/no-throw-literal.mdx Co-authored-by: auvred <61150013+auvred@users.noreply.github.com> --- packages/eslint-plugin/docs/rules/no-throw-literal.mdx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/eslint-plugin/docs/rules/no-throw-literal.mdx b/packages/eslint-plugin/docs/rules/no-throw-literal.mdx index 0317ef4bbd7e..d5919b3bdc63 100644 --- a/packages/eslint-plugin/docs/rules/no-throw-literal.mdx +++ b/packages/eslint-plugin/docs/rules/no-throw-literal.mdx @@ -14,7 +14,7 @@ The fundamental benefit of `Error` objects is that they automatically keep track This rule restricts what can be thrown as an exception. -:::warn +:::warning This rule is being renamed to [`only-throw-error`](./only-throw-error.mdx). 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` options, it can be configured to only allow throwing values which are guaranteed to be an instance of `Error`.