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): [unified-signatures] exemptthis from optional parameter overload check#11005
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
Uh oh!
There was an error while loading.Please reload this page.
Changes fromall commits
e61d6cd1b00350f4689e458fef3a181c003676857b99e300ff6301ab44d6ac8File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -264,6 +264,14 @@ export default createRule<Options, MessageIds>({ | ||
| types1: readonly TSESTree.Parameter[], | ||
| types2: readonly TSESTree.Parameter[], | ||
| ): Unify | undefined { | ||
| const firstParam1 = types1[0]; | ||
| const firstParam2 = types2[0]; | ||
| // exempt signatures with `this: void` from the rule | ||
| if (isThisVoidParam(firstParam1) || isThisVoidParam(firstParam2)) { | ||
| return undefined; | ||
| } | ||
| const index = getIndexOfFirstDifference( | ||
| types1, | ||
| types2, | ||
| @@ -294,6 +302,22 @@ export default createRule<Options, MessageIds>({ | ||
| : undefined; | ||
| } | ||
| function isThisParam(param: TSESTree.Parameter | undefined): boolean { | ||
Member There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. FYI/PSA - I changed this not to be a type predicate since it's only valid in the ContributorAuthor There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. Oh I didn't think about TS doing a negative refinement when the function returns | ||
| return ( | ||
| param != null && | ||
| param.type === AST_NODE_TYPES.Identifier && | ||
| param.name === 'this' | ||
| ); | ||
| } | ||
| function isThisVoidParam(param: TSESTree.Parameter | undefined) { | ||
| return ( | ||
| isThisParam(param) && | ||
| (param as TSESTree.Identifier).typeAnnotation?.typeAnnotation.type === | ||
| AST_NODE_TYPES.TSVoidKeyword | ||
| ); | ||
| } | ||
| /** | ||
| * Detect `a(): void` and `a(x: number): void`. | ||
| * Returns the parameter declaration (`x: number` in this example) that should be optional/rest, and overload it's a part of. | ||
| @@ -310,6 +334,19 @@ export default createRule<Options, MessageIds>({ | ||
| const shorter = sig1.length < sig2.length ? sig1 : sig2; | ||
| const shorterSig = sig1.length < sig2.length ? a : b; | ||
| const firstParam1 = sig1.at(0); | ||
| const firstParam2 = sig2.at(0); | ||
| // If one signature has explicit this type and another doesn't, they can't | ||
| // be unified. | ||
| if (isThisParam(firstParam1) !== isThisParam(firstParam2)) { | ||
| return undefined; | ||
| } | ||
| // exempt signatures with `this: void` from the rule | ||
| if (isThisVoidParam(firstParam1) || isThisVoidParam(firstParam2)) { | ||
| return undefined; | ||
| } | ||
| // If one is has 2+ parameters more than the other, they must all be optional/rest. | ||
| // Differ by optional parameters: f() and f(x), f() and f(x, ?y, ...z) | ||
| // Not allowed: f() and f(x, y) | ||