Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork2.8k
feat(eslint-plugin): add rule no-unsafe-assignment#1694
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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Merged
Uh oh!
There was an error while loading.Please reload this page.
Merged
Changes fromall commits
Commits
Show all changes
8 commits Select commitHold shift + click to select a range
41b7b15
feat(eslint-plugin): add rule no-unsafe-assignment
bradzacher69ec840
feat: check array patterns
bradzacherb8c9250
test: assignment pattern
bradzacherfcbaf91
feat: check object pattern
bradzacherb1d1a92
feat: properties
bradzacherd6f08d2
feat: array spreads
bradzacher9f94296
feat: JSX attributes
bradzacher7b4d118
docs: docs
bradzacherFile filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
1 change: 1 addition & 0 deletionspackages/eslint-plugin/README.md
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
62 changes: 62 additions & 0 deletionspackages/eslint-plugin/docs/rules/no-unsafe-assignment.md
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,62 @@ | ||
# Disallows assigning any to variables and properties (`no-unsafe-assignment`) | ||
Despite your best intentions, the `any` type can sometimes leak into your codebase. | ||
Assigning an `any` typed value to a variable can be hard to pick up on, particularly if it leaks in from an external library. Operations on the variable will not checked at all by TypeScript, so it creates a potential safety hole, and source of bugs in your codebase. | ||
## Rule Details | ||
This rule disallows the assigning `any` to a variable, and assigning `any[]` to an array destructuring. | ||
This rule also compares the assigned type to the variable's declared/inferred return type to ensure you don't return an unsafe `any` in a generic position to a receiver that's expecting a specific type. For example, it will error if you return `Set<any>` from a function declared as returning `Set<string>`. | ||
Examples of **incorrect** code for this rule: | ||
```ts | ||
const x = 1 as any, | ||
y = 1 as any; | ||
const [x] = 1 as any; | ||
const [x] = [] as any[]; | ||
const [x] = [1 as any]; | ||
[x] = [1] as [any]; | ||
function foo(a = 1 as any) {} | ||
class Foo { | ||
constructor(private a = 1 as any) {} | ||
} | ||
class Foo { | ||
private a = 1 as any; | ||
} | ||
// generic position examples | ||
const x: Set<string> = new Set<any>(); | ||
const x: Map<string, string> = new Map<string, any>(); | ||
const x: Set<string[]> = new Set<any[]>(); | ||
const x: Set<Set<Set<string>>> = new Set<Set<Set<any>>>(); | ||
``` | ||
Examples of **correct** code for this rule: | ||
```ts | ||
const x = 1, | ||
y = 1; | ||
const [x] = [1]; | ||
[x] = [1] as [number]; | ||
function foo(a = 1) {} | ||
class Foo { | ||
constructor(private a = 1) {} | ||
} | ||
class Foo { | ||
private a = 1; | ||
} | ||
// generic position examples | ||
const x: Set<string> = new Set<string>(); | ||
const x: Map<string, string> = new Map<string, string>(); | ||
const x: Set<string[]> = new Set<string[]>(); | ||
const x: Set<Set<Set<string>>> = new Set<Set<Set<string>>>(); | ||
``` | ||
## Related to | ||
- [`no-explicit-any`](./no-explicit-any.md) | ||
- TSLint: [`no-unsafe-any`](https://palantir.github.io/tslint/rules/no-unsafe-any/) |
2 changes: 1 addition & 1 deletionpackages/eslint-plugin/docs/rules/no-unsafe-call.md
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
1 change: 1 addition & 0 deletionspackages/eslint-plugin/src/configs/all.json
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
2 changes: 2 additions & 0 deletionspackages/eslint-plugin/src/rules/index.ts
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.
Uh oh!
There was an error while loading.Please reload this page.
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.