From 744a2d570b7841e571f79727a8c8ba245a0555ee Mon Sep 17 00:00:00 2001 From: Josh Goldberg Date: Thu, 6 Jun 2024 11:13:24 -0400 Subject: [PATCH 1/3] docs: add minimal docs page for type-utils package --- docs/packages/Type_Utils.mdx | 17 ++++++++++ packages/type-utils/src/builtinSymbolLikes.ts | 33 ++++++++++++++----- packages/website/sidebars/sidebar.base.js | 1 + 3 files changed, 42 insertions(+), 9 deletions(-) create mode 100644 docs/packages/Type_Utils.mdx diff --git a/docs/packages/Type_Utils.mdx b/docs/packages/Type_Utils.mdx new file mode 100644 index 000000000000..24ea50f004e6 --- /dev/null +++ b/docs/packages/Type_Utils.mdx @@ -0,0 +1,17 @@ +--- +id: type-utils +sidebar_label: type-utils +--- + +# `@typescript-eslint/type-utils` + + + +> Type utilities for working with TypeScript types ✨ + +This package contains public utilities for working with TypeScript types. +Rules declared in [`@typescript-eslint/eslint-plugin`](./ESLint_Plugin.mdx) use these utility functions. + +The utilities in this package are separated from [`@typescript-eslint/utils`](./Utils.mdx) so that that package does not require a dependency on `typescript`. + +> See [Custom Rules](../developers/Custom_Rules.mdx) for documentation on creating your own custom ESLint rules for TypeScript code. diff --git a/packages/type-utils/src/builtinSymbolLikes.ts b/packages/type-utils/src/builtinSymbolLikes.ts index 3443a0d0382e..a2a03a692a26 100644 --- a/packages/type-utils/src/builtinSymbolLikes.ts +++ b/packages/type-utils/src/builtinSymbolLikes.ts @@ -3,18 +3,24 @@ import * as ts from 'typescript'; import { isSymbolFromDefaultLibrary } from './isSymbolFromDefaultLibrary'; /** - * class Foo extends Promise {} - * Foo.reject - * ^ PromiseLike + * @example + * ```ts + * class DerivedClass extends Promise {} + * DerivedClass.reject + * // ^ PromiseLike + * ``` */ export function isPromiseLike(program: ts.Program, type: ts.Type): boolean { return isBuiltinSymbolLike(program, type, 'Promise'); } /** - * const foo = Promise - * foo.reject - * ^ PromiseConstructorLike + * @example + * ```ts + * const value = Promise + * value.reject + * // ^ PromiseConstructorLike + * ``` */ export function isPromiseConstructorLike( program: ts.Program, @@ -24,17 +30,23 @@ export function isPromiseConstructorLike( } /** + * @example + * ```ts * class Foo extends Error {} * new Foo() - * ^ ErrorLike + * // ^ ErrorLike + * ``` */ export function isErrorLike(program: ts.Program, type: ts.Type): boolean { return isBuiltinSymbolLike(program, type, 'Error'); } /** + * @example + * ```ts * type T = Readonly - * ^ ReadonlyErrorLike + * // ^ ReadonlyErrorLike + * ``` */ export function isReadonlyErrorLike( program: ts.Program, @@ -50,8 +62,11 @@ export function isReadonlyErrorLike( } /** + * @example + * ```ts * type T = Readonly<{ foo: 'bar' }> - * ^ ReadonlyTypeLike + * // ^ ReadonlyTypeLike + * ``` */ export function isReadonlyTypeLike( program: ts.Program, diff --git a/packages/website/sidebars/sidebar.base.js b/packages/website/sidebars/sidebar.base.js index 4676e1e43612..02cbad567249 100644 --- a/packages/website/sidebars/sidebar.base.js +++ b/packages/website/sidebars/sidebar.base.js @@ -84,6 +84,7 @@ module.exports = { 'packages/parser', 'packages/rule-tester', 'packages/scope-manager', + 'packages/type-utils', 'packages/typescript-estree', 'packages/typescript-eslint', 'packages/utils', From 54bc492c1c979213bbd73b779fef8960297be221 Mon Sep 17 00:00:00 2001 From: Josh Goldberg Date: Fri, 7 Jun 2024 09:37:51 -0400 Subject: [PATCH 2/3] Updated mentions of ts-api-utils and utils --- docs/packages/Type_Utils.mdx | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/docs/packages/Type_Utils.mdx b/docs/packages/Type_Utils.mdx index 24ea50f004e6..3b43c3514be8 100644 --- a/docs/packages/Type_Utils.mdx +++ b/docs/packages/Type_Utils.mdx @@ -7,11 +7,14 @@ sidebar_label: type-utils -> Type utilities for working with TypeScript types ✨ +> Type utilities for working with TypeScript within ESLint rules. ✨ -This package contains public utilities for working with TypeScript types. +This package contains public utilities for working with TypeScript types that ESLint rules often use. Rules declared in [`@typescript-eslint/eslint-plugin`](./ESLint_Plugin.mdx) use these utility functions. -The utilities in this package are separated from [`@typescript-eslint/utils`](./Utils.mdx) so that that package does not require a dependency on `typescript`. +The utilities in this package are both: + +- Generally ESLint-focused than the broad TypeScript utilities in [`ts-api-utils`](https://npmjs.com/package/ts-api-utils) +- Separated from [`@typescript-eslint/utils`](./Utils.mdx) so that that package does not require a dependency on `typescript` > See [Custom Rules](../developers/Custom_Rules.mdx) for documentation on creating your own custom ESLint rules for TypeScript code. From eadcd77540eb136c0baf98852cc47c51cdb50c6a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Josh=20Goldberg=20=E2=9C=A8?= Date: Mon, 17 Jun 2024 15:18:51 -0400 Subject: [PATCH 3/3] Update docs/packages/Type_Utils.mdx Co-authored-by: Kirk Waiblinger --- docs/packages/Type_Utils.mdx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/packages/Type_Utils.mdx b/docs/packages/Type_Utils.mdx index 3b43c3514be8..fe5f382255ab 100644 --- a/docs/packages/Type_Utils.mdx +++ b/docs/packages/Type_Utils.mdx @@ -14,7 +14,7 @@ Rules declared in [`@typescript-eslint/eslint-plugin`](./ESLint_Plugin.mdx) use The utilities in this package are both: -- Generally ESLint-focused than the broad TypeScript utilities in [`ts-api-utils`](https://npmjs.com/package/ts-api-utils) +- More generally ESLint-focused than the broad TypeScript utilities in [`ts-api-utils`](https://npmjs.com/package/ts-api-utils) - Separated from [`@typescript-eslint/utils`](./Utils.mdx) so that that package does not require a dependency on `typescript` > See [Custom Rules](../developers/Custom_Rules.mdx) for documentation on creating your own custom ESLint rules for TypeScript code.