-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
feat(eslint-plugin): [no-misused-spread] add new rule #8509
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
StyleShit
wants to merge
57
commits into
typescript-eslint:main
from
StyleShit:feat/no-misused-spread
Closed
Changes from all commits
Commits
Show all changes
57 commits
Select commit
Hold shift + click to select a range
75812fc
feat(eslint-plugin): [no-misused-spread] add new rule
StyleShit f18acb8
fix docs
StyleShit 22a50e5
add tests
StyleShit 985795a
wip
StyleShit 3ff215f
fix functions
StyleShit 8e760e0
Merge remote-tracking branch 'typescript-eslint/main' into feat/no-mi…
StyleShit 5f9fae6
Merge remote-tracking branch 'typescript-eslint/main' into feat/no-mi…
StyleShit 990006f
change type name to `Iterable`
StyleShit 49692d2
wip
StyleShit 1f76659
Merge remote-tracking branch 'typescript-eslint/main' into feat/no-mi…
StyleShit c45038f
fix lint
StyleShit a12c9ff
Merge remote-tracking branch 'typescript-eslint/main' into feat/no-mi…
StyleShit 9f07bc1
wip
StyleShit ab20e73
Merge remote-tracking branch 'typescript-eslint/main' into feat/no-mi…
StyleShit 3ef5a45
Merge remote-tracking branch 'typescript-eslint/main' into feat/no-mi…
StyleShit b97a397
wip
StyleShit e02d7a9
don't flag strings spread in object
StyleShit 26434c3
Merge remote-tracking branch 'typescript-eslint/main' into feat/no-mi…
StyleShit 0921b80
wip
StyleShit b8d7e47
wip
StyleShit 3f9d545
configs
StyleShit b133698
snapshot
StyleShit c449dba
Merge remote-tracking branch 'typescript-eslint/main' into feat/no-mi…
StyleShit bb3031f
add options
StyleShit 99c67a0
better message
StyleShit ef12015
add map
StyleShit c3fa340
messages
StyleShit bc3bcf2
array
StyleShit 137c371
promise + iterable
StyleShit db72726
class declaration
StyleShit 92140f2
class instance
StyleShit 8a5e2dc
lint
StyleShit d2fe874
wip
StyleShit 0b0ac03
docs
StyleShit 8a1a309
Merge remote-tracking branch 'typescript-eslint/main' into feat/no-mi…
StyleShit dc1411f
Merge remote-tracking branch 'typescript-eslint/main' into feat/no-mi…
StyleShit 02d7fd3
wip
StyleShit e90385a
wip
StyleShit fc57f13
wip
StyleShit a95cd5f
wip?
StyleShit 094ae02
wip
StyleShit f9a1d18
FML
StyleShit e20e49b
wip
StyleShit acd146f
Merge remote-tracking branch 'typescript-eslint/main' into feat/no-mi…
StyleShit af82cbb
update snapshot
StyleShit 9ff1ba1
docs
StyleShit f4cf064
Merge remote-tracking branch 'typescript-eslint/main' into feat/no-mi…
StyleShit 3dea4cd
Merge branch 'main' into feat/no-misused-spread
bradzacher 235ca44
Merge remote-tracking branch 'typescript-eslint/main' into feat/no-mi…
StyleShit a808081
Merge branch 'feat/no-misused-spread' of https://github.com/StyleShit…
StyleShit 9405682
wip
StyleShit 2d60b29
Merge remote-tracking branch 'typescript-eslint/main' into feat/no-mi…
StyleShit a678af7
Merge branch 'main' into feat/no-misused-spread
JoshuaKGoldberg 283bbb7
Merge remote-tracking branch 'typescript-eslint/main' into feat/no-mi…
StyleShit 804fab4
Merge branch 'feat/no-misused-spread' of https://github.com/StyleShit…
StyleShit fc0e114
fix tests
StyleShit 07b69b8
Merge remote-tracking branch 'typescript-eslint/main' into feat/no-mi…
StyleShit 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
141 changes: 141 additions & 0 deletions
141
packages/eslint-plugin/docs/rules/no-misused-spread.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,141 @@ | ||
--- | ||
description: 'Disallow using the spread operator when it might cause unexpected behavior.' | ||
--- | ||
|
||
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/no-misused-spread** for documentation. | ||
|
||
The [spread operator](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Spread_syntax) (`...`) is a JavaScript | ||
feature that can be misused in ways not always reported by TypeScript. This rule disallows using the spread operator on types where | ||
spreading can lead to unexpected behavior. | ||
|
||
This rule disallows the spread operator in the following cases: | ||
JoshuaKGoldberg marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
- Spreading a string into an array | ||
JoshuaKGoldberg marked this conversation as resolved.
Show resolved
Hide resolved
|
||
- Spreading a `Promise` into an object | ||
- Spreading a function without properties into an object | ||
- Spreading a class instance or declaration into an object | ||
- Spreading an iterable (`Map`, `Array`, etc.) into an object | ||
JoshuaKGoldberg marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
## Examples | ||
|
||
<Tabs> | ||
<TabItem value="❌ Incorrect"> | ||
|
||
```ts | ||
declare const userName: string; | ||
const chars = [...userName]; | ||
|
||
declare const arr: number[]; | ||
const arrSpread = { ...arr }; | ||
|
||
declare const set: Set<number>; | ||
const setSpread = { ...set }; | ||
|
||
declare const map: Map<string, number>; | ||
const mapSpread = { ...map }; | ||
|
||
declare function getObj(): { a: 1; b: 2 }; | ||
const getObjSpread = { ...getObj }; | ||
``` | ||
|
||
</TabItem> | ||
<TabItem value="✅ Correct"> | ||
|
||
```ts | ||
declare const userName: string; | ||
const chars = userName.split(''); | ||
|
||
declare const arr: number[]; | ||
const arrSpread = [...arr]; | ||
|
||
declare const set: Set<number>; | ||
const setSpread = [...set]; | ||
|
||
declare const map: Map<string, number>; | ||
const mapObject = Object.fromEntries(map); | ||
|
||
declare function getObj(): { a: 1; b: 2 }; | ||
const getObjSpread = { ...getObj() }; | ||
``` | ||
|
||
</TabItem> | ||
</Tabs> | ||
|
||
## Options | ||
|
||
### `allow` | ||
|
||
This option allows marking specific types as "safe" to be spread. It takes an | ||
array of type specifiers to consider safe. | ||
|
||
This option takes the shared [`TypeOrValueSpecifier` format](/packages/type-utils/type-or-value-specifier). | ||
|
||
Examples of a configuration for this option: | ||
|
||
```json | ||
"@typescript-eslint/no-misused-spread": [ | ||
"error", | ||
{ | ||
"allowForKnownSafeIterables": [ | ||
"SafeType", | ||
{ "from": "file", "name": "SafeString", "path": "src/safe-string.ts" }, | ||
{ "from": "lib", "name": "BrandedArray" }, | ||
{ "from": "package", "name": "ThisIsSafe", "package": "safe-lib" } | ||
] | ||
} | ||
] | ||
``` | ||
|
||
<Tabs> | ||
<TabItem value="❌ Incorrect"> | ||
|
||
```ts | ||
type UnsafeIterable = Iterable<number>; | ||
|
||
declare const iterable: UnsafeIterable; | ||
|
||
const spreadIterable = { ...iterable }; | ||
|
||
type UnsafeBrandedString = string & { __brand: 'unsafe' }; | ||
|
||
declare const brandedString: UnsafeBrandedString; | ||
|
||
const spreadBrandedString = { ...brandedString }; | ||
``` | ||
|
||
</TabItem> | ||
<TabItem value="✅ Correct"> | ||
|
||
```ts option='{"allow":["SafeIterable", "BrandedString"]}' | ||
type SafeIterable = Iterable<number>; | ||
|
||
declare const iterable: SafeIterable; | ||
|
||
const spreadIterable = { ...iterable }; | ||
|
||
type BrandedString = string & { __brand: 'safe' }; | ||
|
||
declare const brandedString: BrandedString; | ||
|
||
const spreadBrandedString = { ...brandedString }; | ||
``` | ||
|
||
</TabItem> | ||
</Tabs> | ||
|
||
## When Not To Use It | ||
|
||
If you intentionally want to use the spread operator in those cases, and expect | ||
the specific behavior that comes with it, you might not want this rule. | ||
For example, when you want to spread an array into an object and expect the | ||
result to be an object with the array elements as values and the array indices | ||
as keys. | ||
JoshuaKGoldberg marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
If your use cases for unusual spreads only involve a few types, you might consider using | ||
[ESLint disable comments](https://eslint.org/docs/latest/use/configure/rules#using-configuration-comments-1) | ||
and/or the [`allow` option](#allow) 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
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.
Uh oh!
There was an error while loading. Please reload this page.