-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(eslint-plugin): [no-invalid-void-type] don't flag function overlo…
…ading with `void` in return type annotation (#10422) * initial implementation * add block test * refactor * refactor * use a recursive function to handle wrapped return types * report on method definitions that aren't overloads * handle export default declaration * handle named export declaration * refactor * fix test * use nullThrows instead of non-null assertions * add stricter parent types for various ast nodes and refactor accordingly * fix existing missing code coverage * refactor * move and rename hasOverloadSignatures * test void in non-implementation position * adjust hasOverloadSignatures to require a method definition with a body * expand comment a bit * Revert "adjust hasOverloadSignatures to require a method definition with a body" This reverts commit 8dd4707. * remove unnecessary comment * adjust hasOverloadSignatures to require a method definition with a body * Revert "adjust hasOverloadSignatures to require a method definition with a body" This reverts commit 350e82f. * refactor
- Loading branch information
Showing
4 changed files
with
357 additions
and
2 deletions.
There are no files selected for viewing
This file contains 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 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,72 @@ | ||
import type { TSESTree } from '@typescript-eslint/utils'; | ||
import type { RuleContext } from '@typescript-eslint/utils/ts-eslint'; | ||
|
||
import { AST_NODE_TYPES } from '@typescript-eslint/utils'; | ||
|
||
import { getStaticMemberAccessValue } from './misc'; | ||
|
||
/** | ||
* @return `true` if the function or method node has overload signatures. | ||
*/ | ||
export function hasOverloadSignatures( | ||
node: TSESTree.FunctionDeclaration | TSESTree.MethodDefinition, | ||
context: RuleContext<string, unknown[]>, | ||
): boolean { | ||
// `export default function () {}` | ||
if (node.parent.type === AST_NODE_TYPES.ExportDefaultDeclaration) { | ||
return node.parent.parent.body.some(member => { | ||
return ( | ||
member.type === AST_NODE_TYPES.ExportDefaultDeclaration && | ||
member.declaration.type === AST_NODE_TYPES.TSDeclareFunction | ||
); | ||
}); | ||
} | ||
|
||
// `export function f() {}` | ||
if (node.parent.type === AST_NODE_TYPES.ExportNamedDeclaration) { | ||
return node.parent.parent.body.some(member => { | ||
return ( | ||
member.type === AST_NODE_TYPES.ExportNamedDeclaration && | ||
member.declaration?.type === AST_NODE_TYPES.TSDeclareFunction && | ||
getFunctionDeclarationName(member.declaration, context) === | ||
getFunctionDeclarationName(node, context) | ||
); | ||
}); | ||
} | ||
|
||
// either: | ||
// - `function f() {}` | ||
// - `class T { foo() {} }` | ||
|
||
const nodeKey = getFunctionDeclarationName(node, context); | ||
|
||
return node.parent.body.some(member => { | ||
return ( | ||
(member.type === AST_NODE_TYPES.TSDeclareFunction || | ||
(member.type === AST_NODE_TYPES.MethodDefinition && | ||
member.value.body == null)) && | ||
nodeKey === getFunctionDeclarationName(member, context) | ||
); | ||
}); | ||
} | ||
|
||
function getFunctionDeclarationName( | ||
node: | ||
| TSESTree.FunctionDeclaration | ||
| TSESTree.MethodDefinition | ||
| TSESTree.TSDeclareFunction, | ||
context: RuleContext<string, unknown[]>, | ||
): string | symbol | undefined { | ||
if ( | ||
node.type === AST_NODE_TYPES.FunctionDeclaration || | ||
node.type === AST_NODE_TYPES.TSDeclareFunction | ||
) { | ||
// For a `FunctionDeclaration` or `TSDeclareFunction` this may be `null` if | ||
// and only if the parent is an `ExportDefaultDeclaration`. | ||
// | ||
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion | ||
return node.id!.name; | ||
} | ||
|
||
return getStaticMemberAccessValue(node, context); | ||
} |
This file contains 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.