-
-
Notifications
You must be signed in to change notification settings - Fork 4.8k
feat: ignore overloaded function declarations in func-style rule #19755
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
Changes from all commits
2a23382
5449f62
61ea43d
9b088a4
ff26693
9dfcb5e
baf6230
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -65,6 +65,43 @@ module.exports = { | |
const { namedExports: exportFunctionStyle } = overrides; | ||
const stack = []; | ||
|
||
/** | ||
* Checks if a function declaration is part of an overloaded function | ||
* @param {ASTNode} node The function declaration node to check | ||
* @returns {boolean} True if the function is overloaded | ||
*/ | ||
function isOverloadedFunction(node) { | ||
const functionName = node.id.name; | ||
|
||
if (node.parent.type === "ExportNamedDeclaration") { | ||
return node.parent.parent.body.some( | ||
member => | ||
member.type === "ExportNamedDeclaration" && | ||
member.declaration?.type === "TSDeclareFunction" && | ||
member.declaration.id.name === functionName, | ||
); | ||
} | ||
|
||
if (node.parent.type === "SwitchCase") { | ||
return node.parent.parent.cases.some(switchCase => | ||
switchCase.consequent.some( | ||
member => | ||
member.type === "TSDeclareFunction" && | ||
member.id.name === functionName, | ||
), | ||
); | ||
} | ||
|
||
return ( | ||
Array.isArray(node.parent.body) && | ||
sethamus marked this conversation as resolved.
Show resolved
Hide resolved
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. There's still no test that would hit this. I think those tests should have the "expression"` option. 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. Aside from labeled function declarations, which was caught by CI (hence this check), I can't think of any other scenario where node.parent.body wouldn't be an array. 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. The tests you added in 9b088a4 would hit this, but with Another example of a function declaration where 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 right I misunderstood your previous comments, sorry about that. |
||
node.parent.body.some( | ||
member => | ||
member.type === "TSDeclareFunction" && | ||
member.id.name === functionName, | ||
sethamus marked this conversation as resolved.
Show resolved
Hide resolved
|
||
) | ||
); | ||
} | ||
|
||
const nodesToCheck = { | ||
FunctionDeclaration(node) { | ||
stack.push(false); | ||
|
@@ -73,14 +110,16 @@ module.exports = { | |
!enforceDeclarations && | ||
node.parent.type !== "ExportDefaultDeclaration" && | ||
(typeof exportFunctionStyle === "undefined" || | ||
node.parent.type !== "ExportNamedDeclaration") | ||
node.parent.type !== "ExportNamedDeclaration") && | ||
!isOverloadedFunction(node) | ||
) { | ||
context.report({ node, messageId: "expression" }); | ||
} | ||
|
||
if ( | ||
node.parent.type === "ExportNamedDeclaration" && | ||
exportFunctionStyle === "expression" | ||
exportFunctionStyle === "expression" && | ||
!isOverloadedFunction(node) | ||
) { | ||
context.report({ node, messageId: "expression" }); | ||
} | ||
|
Uh oh!
There was an error while loading. Please reload this page.