-
-
Notifications
You must be signed in to change notification settings - Fork 2.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 our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
skondrashov
wants to merge
43
commits into
typescript-eslint:main
Choose a base branch
from
skondrashov:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+1,329
−97
Open
Changes from all commits
Commits
Show all changes
43 commits
Select commit
Hold 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
skondrashov ae8f471
review changes
kirkwaiblinger 09b14f8
Merge branch 'main' into review/10182
kirkwaiblinger 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
74 changes: 74 additions & 0 deletions
74
packages/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,74 @@ | ||
--- | ||
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 has several idioms used to convert values to certain types. With TypeScript, it is possible to see at build time if the value is already of that type, making the conversion unnecessary. | ||
Performing unnecessary conversions increases visual clutter and harms code readability, so it's generally best practice to remove them if they don't change the type of an expression. | ||
This rule reports when a type conversion idiom is identified which does not change the runtime type of an expression. | ||
|
||
## 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) |
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.
It's about the buildtime type of the expression I think more than the runtime type. The runtime type isn't necessarily the same going in vs out of the expression, and if it is it's most likely because it matches the buildtime types, which is what we are scanning.