Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork2.8k
fix(eslint-plugin): add getConstraintInfo to handle generic constraints better#10496
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
JoshuaKGoldberg merged 17 commits intotypescript-eslint:mainfromkirkwaiblinger:get-constraint-type-info-at-locationDec 30, 2024
Uh oh!
There was an error while loading.Please reload this page.
Merged
Changes fromall commits
Commits
Show all changes
17 commits Select commitHold shift + click to select a range
d7bcdc6
add getConstraintTypeInfoAtLocation
typescript-eslint[bot]cd97692
revisit #10314
kirkwaiblinger3626dcb
handle #10443
kirkwaiblingerbc12e04
tweak tests
kirkwaiblingerf0ebffa
Apply suggestions from code review
kirkwaiblinger2fa6426
code review
kirkwaiblinger2e0bd42
Merge branch 'main' into get-constraint-type-info-at-location
kirkwaiblingere753c02
deal with deprecated API lint failures
kirkwaiblinger11d3a0a
space
kirkwaiblinger5d7cab6
remove vfs, derp
kirkwaiblingerf383280
typo
kirkwaiblingerc8c985c
Merge branch 'main' into get-constraint-type-info-at-location
kirkwaiblinger2aecb98
simplify
kirkwaiblinger2a5c248
more simplify
kirkwaiblingerc646ffb
jsdoc
kirkwaiblinger943dc75
more better
kirkwaiblinger4c9da08
better
kirkwaiblingerFile 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
15 changes: 11 additions & 4 deletionspackages/eslint-plugin/src/rules/await-thenable.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
17 changes: 11 additions & 6 deletionspackages/eslint-plugin/src/rules/no-unnecessary-boolean-literal-compare.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
23 changes: 10 additions & 13 deletionspackages/eslint-plugin/src/rules/no-unnecessary-condition.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
60 changes: 60 additions & 0 deletionspackages/eslint-plugin/src/util/getConstraintInfo.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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
import type * as ts from 'typescript'; | ||
import * as tsutils from 'ts-api-utils'; | ||
export interface ConstraintTypeInfoUnconstrained { | ||
constraintType: undefined; | ||
isTypeParameter: true; | ||
} | ||
export interface ConstraintTypeInfoConstrained { | ||
constraintType: ts.Type; | ||
isTypeParameter: true; | ||
} | ||
export interface ConstraintTypeInfoNonGeneric { | ||
constraintType: ts.Type; | ||
isTypeParameter: false; | ||
} | ||
export type ConstraintTypeInfo = | ||
| ConstraintTypeInfoConstrained | ||
| ConstraintTypeInfoNonGeneric | ||
| ConstraintTypeInfoUnconstrained; | ||
/** | ||
* Returns whether the type is a generic and what its constraint is. | ||
* | ||
* If the type is not a generic, `isTypeParameter` will be `false`, and | ||
* `constraintType` will be the same as the input type. | ||
* | ||
* If the type is a generic, and it is constrained, `isTypeParameter` will be | ||
* `true`, and `constraintType` will be the constraint type. | ||
* | ||
* If the type is a generic, but it is not constrained, `constraintType` will be | ||
* `undefined` (rather than an `unknown` type), due to https://github.com/microsoft/TypeScript/issues/60475 | ||
* | ||
* Successor to {@link getConstrainedTypeAtLocation} due to https://github.com/typescript-eslint/typescript-eslint/issues/10438 | ||
* | ||
* This is considered internal since it is unstable for now and may have breaking changes at any time. | ||
* Use at your own risk. | ||
* | ||
* @internal | ||
* | ||
*/ | ||
export function getConstraintInfo( | ||
checker: ts.TypeChecker, | ||
type: ts.Type, | ||
): ConstraintTypeInfo { | ||
if (tsutils.isTypeParameter(type)) { | ||
const constraintType = checker.getBaseConstraintOfType(type); | ||
return { | ||
constraintType, | ||
isTypeParameter: true, | ||
}; | ||
} | ||
return { | ||
constraintType: type, | ||
isTypeParameter: false, | ||
}; | ||
} |
2 changes: 2 additions & 0 deletionspackages/eslint-plugin/src/util/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
14 changes: 6 additions & 8 deletionspackages/eslint-plugin/src/util/needsToBeAwaited.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
14 changes: 14 additions & 0 deletionspackages/eslint-plugin/tests/rules/no-unnecessary-boolean-literal-compare.test.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.