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): [no-unnecessary-type-conversion] add rule#10182
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
46 commits Select commitHold shift + click to select a range
762cd47 add no-unnecessary-coercion rule
3060bff resolve no-unnecessary-coercion rule problems in repository
393fb2f rename to no-unnecessary-type-conversion
00476f9 make perfectionist happy
89ecd74 rename isString to matchesType to accurately reflect that it works wi…
879dd41 don't pass node to context.report if a loc is already being passed
1638cb3 improve loc generation for !!, +, and ~~ operators
0112f0a don't use messageId constant in tests
bfd8c5b retrieve types only when we need to in order to be more performant
05f4c6e Merge branch 'real_main'
e125bb9 PR feedback
0a1257b implement satisfies suggestions
0dd5d52 don't trigger rule if type constructor calls are overriden
b054d60 put parentheses around satisfies suggestion autofix when parent is a …
aac5d40 resolve bad autofix for !(!false)
1029658 Merge branch 'real_main'
988f383 show satisfies suggestion for member expressions passed to type const…
606556e different autofixes for the case where str += '' is used as a stateme…
6691fc7 Merge branch 'real_main'
6033707 always provide a satisfies suggestion
d5b9f2f fix minor grammar mistakes in getWrappingFixer
7568840 make satisfies suggestion fixes use getWrappingFixer
251bb05 resolve issue where satisfies suggestion for unary operators gives li…
0b12c0e make autofixes use getWrappingFixer
c85bf9a add suggestions to all tests
2877d5f basic implementation of optional wrap parameter to getWrappingFixer
178b961 remove code path that doesn't seem to be possible to hit
a89e6ca Merge branch 'real_main'
74bf8a1 add tests based on feedback
17dfbc9 change all autofixes to suggestions
6afafe9 change getType to getTypeLazy
c0976d1 alias node callee to avoid type assertion
973234f fix type in message
e8956c0 use unionTypeParts helper function
ad88ade make handleUnaryOperator cleaner
36cf822 Merge branch 'real_main'
8539069 fix test after main merge
2a1471f Merge branch 'real_main'
4a56a42 Merge branch 'real_main'
94ebd1e update snapshots
0d16798 Merge branch 'main' into main
skondrashovae8f471 review changes
kirkwaiblinger09b14f8 Merge branch 'main' into review/10182
kirkwaiblinger6f88f00 revert bad suggestion
kirkwaiblingere2c037a Merge branch 'real_main'
6822a69 josh feedback
File 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 deletionseslint.config.mjs
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
79 changes: 79 additions & 0 deletionspackages/eslint-plugin/docs/rules/no-unnecessary-type-conversion.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,79 @@ | ||
| --- | ||
| description: 'Disallow conversion idioms when they do not change the type or value of the expression.' | ||
| --- | ||
| 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-unnecessary-type-conversion** for documentation. | ||
| JavaScript provides several commonly used idioms to convert values to a specific type: | ||
| - Primitive coercion (e.g. `Boolean(value)`, `String(value)`): using a built-in primitive function | ||
| - String concatenation (e.g. `value + ''`): turning a value into a string | ||
| - Unary coercion (e.g. `+value`, `!!value`): using a built-in operator | ||
| - The `.toString()` method defined on many types | ||
| These conversions are unnecessary if the value is already of that type. | ||
| ## Examples | ||
| <Tabs> | ||
| <TabItem value="❌ Incorrect"> | ||
| ```ts | ||
| String('123'); | ||
| '123'.toString(); | ||
| '' + '123'; | ||
| '123' + ''; | ||
| Number(123); | ||
| +123; | ||
| ~~123; | ||
| Boolean(true); | ||
| !!true; | ||
| BigInt(BigInt(1)); | ||
| let str = '123'; | ||
| str += ''; | ||
| ``` | ||
| </TabItem> | ||
| <TabItem value="✅ Correct"> | ||
| ```ts | ||
| function foo(bar: string | number) { | ||
| String(bar); | ||
| bar.toString(); | ||
| '' + bar; | ||
| bar + ''; | ||
| Number(bar); | ||
| +bar; | ||
| ~~bar; | ||
| Boolean(bar); | ||
| !!bar; | ||
| BigInt(1); | ||
| bar += ''; | ||
| } | ||
| ``` | ||
| </TabItem> | ||
| </Tabs> | ||
| ## When Not To Use It | ||
| If you don't care about having no-op type conversions in your code, then you can turn off this rule. | ||
| If you have types which are not accurate, then this rule might cause you to remove conversions that you actually do need. | ||
| ## Related To | ||
| - [no-unnecessary-type-assertion](./no-unnecessary-type-assertion.mdx) | ||
| - [no-useless-template-literals](./no-useless-template-literals.mdx) |
1 change: 1 addition & 0 deletionspackages/eslint-plugin/src/configs/eslintrc/all.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
1 change: 1 addition & 0 deletionspackages/eslint-plugin/src/configs/eslintrc/disable-type-checked.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
1 change: 1 addition & 0 deletionspackages/eslint-plugin/src/configs/flat/all.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
1 change: 1 addition & 0 deletionspackages/eslint-plugin/src/configs/flat/disable-type-checked.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
2 changes: 1 addition & 1 deletionpackages/eslint-plugin/src/rules/adjacent-overload-signatures.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
4 changes: 2 additions & 2 deletionspackages/eslint-plugin/src/rules/explicit-function-return-type.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
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
2 changes: 1 addition & 1 deletionpackages/eslint-plugin/src/rules/member-ordering.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
4 changes: 2 additions & 2 deletionspackages/eslint-plugin/src/rules/no-duplicate-enum-values.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
5 changes: 2 additions & 3 deletionspackages/eslint-plugin/src/rules/no-empty-interface.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
2 changes: 1 addition & 1 deletionpackages/eslint-plugin/src/rules/no-redundant-type-constituents.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
9 changes: 2 additions & 7 deletionspackages/eslint-plugin/src/rules/no-unnecessary-template-expression.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.
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.