-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
feat(eslint-plugin): [require-types-exports] add rule #10554
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
Closed
JoshuaKGoldberg
wants to merge
103
commits into
typescript-eslint:main
from
JoshuaKGoldberg:require-types-exports-ii-electric-boogaloo
Closed
Changes from all commits
Commits
Show all changes
103 commits
Select commit
Hold shift + click to select a range
9a0c28a
feat(eslint-plugin): [require-types-exports] add new rule
StyleShit 7778868
wip
StyleShit 12fce5b
wip
StyleShit d62f86c
lint
StyleShit 0ebebd2
wip
StyleShit 30e9aa9
Merge remote-tracking branch 'typescript-eslint/main' into feat/requi…
StyleShit bfee791
spelling...
StyleShit b309b51
wip
StyleShit 6aa6446
wip
StyleShit 892c368
wip
StyleShit 0e8e58f
tuple generic
StyleShit f4018a8
wip
StyleShit 89a8344
wip
StyleShit b2138e3
wip
StyleShit feedefd
Merge remote-tracking branch 'typescript-eslint/main' into feat/requi…
StyleShit 1161db0
wip
StyleShit d9875b3
refactor
StyleShit 6338202
make it shorter & more readable
StyleShit 428b2c1
Merge remote-tracking branch 'typescript-eslint/main' into feat/requi…
StyleShit 2cb3455
Merge remote-tracking branch 'typescript-eslint/main' into feat/requi…
StyleShit 1812e37
fix nested types in functions
StyleShit 4bee779
fix docs
StyleShit 26e7be7
add inferred return type test case
StyleShit e57985a
stupidly check for variable types
StyleShit cbb784c
support default exported variable
StyleShit 2f2dfa4
Merge remote-tracking branch 'typescript-eslint/main' into feat/requi…
StyleShit 37a0171
Merge remote-tracking branch 'typescript-eslint/main' into feat/requi…
StyleShit 6fb274a
update docs
StyleShit 4672fe1
wip
StyleShit c79b5cb
wip
StyleShit 279055a
wip
StyleShit 7897abf
Merge remote-tracking branch 'typescript-eslint/main' into feat/requi…
StyleShit 7082960
Merge remote-tracking branch 'typescript-eslint/main' into feat/requi…
StyleShit 2f81933
improve types
StyleShit 0f788d2
improve type reference search
StyleShit 6cec0f5
don't report types from default library
StyleShit 497957a
getTypeName
StyleShit 702d4d0
Merge remote-tracking branch 'typescript-eslint/main' into feat/requi…
StyleShit 700ff85
move utils out of the closure
StyleShit 9a155b3
support namespaced types
StyleShit 8d0d000
Merge remote-tracking branch 'typescript-eslint/main' into feat/requi…
StyleShit b65f9c4
fix namespaced imports
StyleShit 078e24a
WIP
StyleShit ed23162
wip
StyleShit ac224eb
fix propertykey tests
StyleShit 417cc91
ReturnType test
StyleShit 62f1466
Merge remote-tracking branch 'typescript-eslint/main' into feat/requi…
StyleShit ae1b87c
wip
StyleShit d227408
collect type references recursively
StyleShit dca52d0
lib types
StyleShit ed30856
Merge remote-tracking branch 'typescript-eslint/main' into feat/requi…
StyleShit 15fc51c
style
StyleShit 59eda58
wip
StyleShit fc0858a
wip
StyleShit 9d24c64
Merge remote-tracking branch 'typescript-eslint/main' into feat/requi…
StyleShit 94a98eb
configs
StyleShit dee0fe4
don't report generic params in call expression
StyleShit 0804b24
improve function types collection
StyleShit a0a4944
wip
StyleShit 66a0aff
wip
StyleShit b67e1f9
remove `getVariable`
StyleShit 9891e78
infer return type from return statements
StyleShit cb90d43
wip
StyleShit 479f593
wip
StyleShit 08f2ce2
Merge remote-tracking branch 'typescript-eslint/main' into feat/requi…
StyleShit e86427f
wip
StyleShit a61d49f
wip
StyleShit 121f475
wip
StyleShit f3f8518
wip
StyleShit ab837b4
custom traversal
StyleShit 1641272
some tests
StyleShit fd56a1c
add missing tests
StyleShit b0613d5
report default exported call expression
StyleShit b9f1148
report types used within exported types
StyleShit 0415b60
fix false positives due to ordering
StyleShit a0c236e
change message
StyleShit 3d5d695
wip
StyleShit 2e76ce6
fix some reports
StyleShit 88713cb
support keyof & typeof
StyleShit ff2c0a8
simplify tsconfig
StyleShit 2fd30aa
Merge remote-tracking branch 'typescript-eslint/main' into feat/requi…
StyleShit d977245
Merge branch 'main'
JoshuaKGoldberg a03713a
Revert unintentional changes
JoshuaKGoldberg 2f44044
Clean up docs
JoshuaKGoldberg 57181fd
Test cleanups
JoshuaKGoldberg 8563e09
Lint and tidy up source a bit
JoshuaKGoldberg e056cac
Auto updates
JoshuaKGoldberg 196d148
Trim down test empty lines
JoshuaKGoldberg c42d42f
More exports
JoshuaKGoldberg e11e05a
More exports
JoshuaKGoldberg cc90f7d
More exports
JoshuaKGoldberg 5458408
fix: don't report within call expressions
JoshuaKGoldberg 0061e98
Some more cleanups
JoshuaKGoldberg b629590
Better typeofs and externally declared globals
JoshuaKGoldberg 6de73eb
shared isNodeInside, and more internal progress
JoshuaKGoldberg 4188eb0
Handle already-exported ones, such as namespace children
JoshuaKGoldberg 55cca12
Also check typeof reports for being exported already
JoshuaKGoldberg fb0c4f3
Merge branch 'main' into require-types-exports-ii-electric-boogaloo
JoshuaKGoldberg dd7c454
requireTypeQueryExport message
JoshuaKGoldberg 6d04460
requireTypeQueryExport message tweak
JoshuaKGoldberg e9ada94
Switched report to be on export
JoshuaKGoldberg e09b7d9
update docs snapshot
JoshuaKGoldberg 8dc9607
Got up to requiring type info...
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
type ExportAndImportKind = 'type' | 'value'; | ||
export type ExportAndImportKind = 'type' | 'value'; | ||
|
||
export type ExportKind = ExportAndImportKind; | ||
export type ImportKind = ExportAndImportKind; |
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
87 changes: 87 additions & 0 deletions
87
packages/eslint-plugin/docs/rules/require-types-exports.mdx
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,87 @@ | ||
--- | ||
description: 'Require exporting types that are used in exported entities.' | ||
--- | ||
|
||
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/require-types-exports** for documentation. | ||
|
||
When exporting entities from a file, it is often useful to export also all the types that are used in their declarations. | ||
Doing so ensures consumers of the file can directly import and use those types when using those entities. | ||
|
||
Otherwise, consumers may have to use utility types like [`Parameters`](https://www.typescriptlang.org/docs/handbook/utility-types.html#parameterstype) or [`ReturnType`](https://www.typescriptlang.org/docs/handbook/utility-types.html#returntypetype) in order to extract the types from the entities. | ||
|
||
## Examples | ||
|
||
<Tabs> | ||
<TabItem value="❌ Incorrect"> | ||
|
||
```ts | ||
interface Fruit { | ||
name: string; | ||
color: string; | ||
} | ||
|
||
export const getFruitName = (fruit: Fruit) => fruit.name; | ||
``` | ||
|
||
```ts | ||
const fruits = { | ||
apple: '🍏', | ||
banana: '🍌', | ||
}; | ||
|
||
export const getFruit = (key: keyof typeof fruits) => fruits[key]; | ||
``` | ||
|
||
```ts | ||
enum Color { | ||
Red = 'red', | ||
Green = 'green', | ||
Blue = 'blue', | ||
} | ||
|
||
export declare function getRandomColor(): Color; | ||
``` | ||
|
||
</TabItem> | ||
<TabItem value="✅ Correct"> | ||
|
||
```ts | ||
export interface Fruit { | ||
name: string; | ||
color: string; | ||
} | ||
|
||
export const getFruitName = (fruit: Fruit) => fruit.name; | ||
``` | ||
|
||
```ts | ||
export const fruits = { | ||
apple: '🍏', | ||
banana: '🍌', | ||
}; | ||
|
||
export const getFruit = (key: keyof typeof fruits) => fruits[key]; | ||
``` | ||
|
||
```ts | ||
export enum Color { | ||
Red = 'red', | ||
Green = 'green', | ||
Blue = 'blue', | ||
} | ||
|
||
export declare function getRandomColor(): Color; | ||
``` | ||
|
||
</TabItem> | ||
</Tabs> | ||
|
||
## When Not To Use It | ||
|
||
If your files utilize many complex self-referential types that you don't want external consumers to reference, you may want to avoid this rule for those cases. | ||
You might consider using [ESLint disable comments](https://eslint.org/docs/latest/use/configure/rules#using-configuration-comments-1) for those specific situations instead of completely disabling this rule. |
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
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
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
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
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
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
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
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
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
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.
Side note: Next rule has I lined the options and does not have Config as separate type. Maybe for consistency this type could be inlined too, if you are doing another PR just for these export changes :) Or if the separate Config type is preferred, maybe there could be some follow up
task for unifying these.
I don’t feel like this would matter if they would stay as internal types. But after they are exposed to “public”, it would look better if they had unified names. :)