-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
docs: add dedicated TypeOrValueSpecifier docs page #9875
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
JoshuaKGoldberg
merged 6 commits into
typescript-eslint:main
from
JoshuaKGoldberg:docs-type-or-value-specifier
Aug 30, 2024
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
932a18f
docs: add dedicated TypeOrValueSpecifier docs page
JoshuaKGoldberg f7fdeab
spelling
JoshuaKGoldberg 3ec6989
Unnecessary new test
JoshuaKGoldberg 946beea
path/package copypasta
JoshuaKGoldberg 7bfb333
push updated test snapshots
JoshuaKGoldberg 42bc6da
Merge branch 'main'
JoshuaKGoldberg File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,135 @@ | ||
--- | ||
id: type-or-value-specifier | ||
title: TypeOrValueSpecifier | ||
--- | ||
|
||
Some lint rules include options to describe specific _types_ and/or _values_. | ||
These options use a standardized format exported from the `type-utils` package, **`TypeOrValueSpecifier`**. | ||
|
||
`TypeOrValueSpecifier` allows three object forms of specifiers: | ||
|
||
- [`FileSpecifier`](#filespecifier): for types or values declared in local files | ||
- [`LibSpecifier`](#libspecifier): for types or values declared in TypeScript's built-in lib definitions | ||
- [`PackageSpecifier`](#packagespecifier): for types or values imported from packages | ||
|
||
For example, the following configuration of [`@typescript-eslint/no-floating-promises` > `allowForKnownSafeCalls`](/rules/no-floating-promises#allowforknownsafecalls) marks `node:test`'s `it` as safe using a package specifier: | ||
|
||
```json | ||
{ | ||
"@typescript-eslint/no-floating-promises": [ | ||
"error", | ||
{ | ||
"allowForKnownSafeCalls": [ | ||
{ "from": "package", "name": "it", "package": "node:test" } | ||
] | ||
} | ||
] | ||
} | ||
``` | ||
|
||
Each object format requires at least: | ||
|
||
- `from`: which specifier to use, as `'file' | 'lib' | 'package'` | ||
- `name`: a `string` or `string[]` for type or value name(s) to match on | ||
|
||
## FileSpecifier | ||
|
||
```ts | ||
interface FileSpecifier { | ||
from: 'file'; | ||
name: string[] | string; | ||
path?: string; | ||
} | ||
``` | ||
|
||
Describes specific types or values declared in local files. | ||
|
||
`path` may be used to specify a file the types or values must be declared in. | ||
If omitted, all files will be matched. | ||
|
||
### FileSpecifier Examples | ||
|
||
Matching all types and values named `Props`: | ||
|
||
```json | ||
{ "from": "file", "name": "Props" } | ||
``` | ||
|
||
Matching all types and values named `Props` in `file.tsx`: | ||
|
||
```json | ||
{ "from": "file", "name": "Props", "path": "file.tsx" } | ||
``` | ||
|
||
## LibSpecifier | ||
|
||
```ts | ||
interface LibSpecifier { | ||
from: 'lib'; | ||
name: string[] | string; | ||
} | ||
``` | ||
|
||
Describes specific types or values declared in TypeScript's built-in `lib.*.d.ts` ("lib") types. | ||
|
||
Lib types include `lib.dom.d.ts` globals such as `Window` and `lib.es*.ts` globals such as `Array`. | ||
|
||
### LibSpecifier Examples | ||
|
||
Matching all array-typed values: | ||
|
||
```json | ||
{ "from": "lib", "name": "Array" } | ||
``` | ||
|
||
Matching all `Promise` and `PromiseLike`-typed values: | ||
|
||
```json | ||
{ "from": "lib", "name": ["Promise", "PromiseLike"] } | ||
``` | ||
|
||
## PackageSpecifier | ||
|
||
```ts | ||
interface PackageSpecifier { | ||
from: 'package'; | ||
name: string[] | string; | ||
package: string; | ||
} | ||
``` | ||
|
||
Describes specific types or values imported from packages. | ||
|
||
`package` must be used to specify the package name. | ||
|
||
### PackageSpecifier Examples | ||
|
||
Matching the `SafePromise` type from `@reduxjs/toolkit`: | ||
|
||
```json | ||
{ "from": "package", "name": "SafePromise", "package": "@reduxjs/toolkit" } | ||
``` | ||
|
||
Matching the `describe`, `it`, and `test` values from `vitest`: | ||
|
||
```json | ||
{ "from": "package", "name": ["describe", "it", "test"], "package": "vitest" } | ||
``` | ||
|
||
## Universal String Specifiers | ||
|
||
`TypeOrValueSpecifier` also allows providing a plain string specifier to match all names regardless of declaration source. | ||
For example, providing `"RegExp"` matches _all_ types and values named `RegExp`. | ||
|
||
:::danger | ||
We strongly recommend not using universal string specifiers. | ||
Matching _all_ names without specifying a source file, library, or package can accidentally match other types or values with a coincidentally similar name. | ||
|
||
Universal string specifiers will be removed in a future major version of typescript-eslint. | ||
::: | ||
|
||
## Rule Options Using This Format | ||
|
||
- [`@typescript-eslint/no-floating-promises` > `allowForKnownSafeCalls`](/rules/no-floating-promises#allowforknownsafecalls) | ||
- [`@typescript-eslint/no-floating-promises` > `allowForKnownSafePromises`](/rules/no-floating-promises#allowforknownsafepromises) | ||
- [`@typescript-eslint/prefer-readonly-parameter-types` > `allow`](/rules/prefer-readonly-parameter-types/#allow) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10 changes: 5 additions & 5 deletions
10
...ges/eslint-plugin/tests/docs-eslint-output-snapshots/prefer-readonly-parameter-types.shot
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fyi @abrahamguo & @marekdedic & @RebeccaStevens 🙂