-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
fix(eslint-plugin): [explicit-function-return-type] support JSX attributes in allowTypedFunctionExpressions
#7553
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
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 |
---|---|---|
|
@@ -37,6 +37,53 @@ function isPropertyDefinitionWithTypeAnnotation( | |
); | ||
} | ||
|
||
/** | ||
* Checks if a node belongs to: | ||
* ``` | ||
* foo(() => 1) | ||
* ``` | ||
*/ | ||
function isFunctionArgument( | ||
parent: TSESTree.Node, | ||
callee?: FunctionExpression, | ||
): parent is TSESTree.CallExpression { | ||
return ( | ||
parent.type === AST_NODE_TYPES.CallExpression && | ||
// make sure this isn't an IIFE | ||
parent.callee !== callee | ||
); | ||
} | ||
|
||
/** | ||
* Checks if a node is type-constrained in JSX | ||
* ``` | ||
* <Foo x={() => {}} /> | ||
* <Bar>{() => {}}</Bar> | ||
* <Baz {...props} /> | ||
* ``` | ||
*/ | ||
function isTypedJSX( | ||
node: TSESTree.Node, | ||
): node is TSESTree.JSXExpressionContainer | TSESTree.JSXSpreadAttribute { | ||
bradzacher marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return ( | ||
node.type === AST_NODE_TYPES.JSXExpressionContainer || | ||
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. This is technically the relevant RHS type of 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. I think we'll want to check the parent because this is another valid location for a <>{() => {}}</> 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. @bradzacher Ah, I totally forgot about the render-props pattern, it's been a while since their heyday! Running this down actually led me across an existing issue in TS where fragment shorthands are not typed properly: microsoft/TypeScript#50429 I went around on this for a while, but I again think all
I eventually realized that while something like this is unsafe: <>{() => 'bug'}</> Having a lint error that turns it into this doesn't make things better as it's still not a type error but won't work as expected at runtime (especially if you return an object instead): <>{(): string => 'bug'}</> While this is a currently an issue with TypeScript, it doesn't seem like the lint rule can do anything better here in place of proper type-checking. But functions can be valid in children expression containers generally. |
||
node.type === AST_NODE_TYPES.JSXSpreadAttribute | ||
); | ||
} | ||
|
||
function isTypedParent( | ||
bradzacher marked this conversation as resolved.
Show resolved
Hide resolved
|
||
parent: TSESTree.Node, | ||
callee?: FunctionExpression, | ||
): boolean { | ||
return ( | ||
isTypeAssertion(parent) || | ||
isVariableDeclaratorWithTypeAnnotation(parent) || | ||
isPropertyDefinitionWithTypeAnnotation(parent) || | ||
isFunctionArgument(parent, callee) || | ||
isTypedJSX(parent) | ||
); | ||
} | ||
|
||
/** | ||
* Checks if a node belongs to: | ||
* ``` | ||
|
@@ -78,13 +125,7 @@ function isPropertyOfObjectWithType( | |
return false; | ||
} | ||
|
||
return ( | ||
isTypeAssertion(parent) || | ||
isPropertyDefinitionWithTypeAnnotation(parent) || | ||
isVariableDeclaratorWithTypeAnnotation(parent) || | ||
isFunctionArgument(parent) || | ||
isPropertyOfObjectWithType(parent) | ||
); | ||
return isTypedParent(parent) || isPropertyOfObjectWithType(parent); | ||
} | ||
|
||
/** | ||
|
@@ -127,23 +168,6 @@ function doesImmediatelyReturnFunctionExpression({ | |
); | ||
} | ||
|
||
/** | ||
* Checks if a node belongs to: | ||
* ``` | ||
* foo(() => 1) | ||
* ``` | ||
*/ | ||
function isFunctionArgument( | ||
parent: TSESTree.Node, | ||
callee?: FunctionExpression, | ||
): parent is TSESTree.CallExpression { | ||
return ( | ||
parent.type === AST_NODE_TYPES.CallExpression && | ||
// make sure this isn't an IIFE | ||
parent.callee !== callee | ||
); | ||
} | ||
|
||
/** | ||
* Checks if a function belongs to: | ||
* ``` | ||
|
@@ -194,11 +218,8 @@ function isTypedFunctionExpression( | |
} | ||
|
||
return ( | ||
isTypeAssertion(parent) || | ||
isVariableDeclaratorWithTypeAnnotation(parent) || | ||
isPropertyDefinitionWithTypeAnnotation(parent) || | ||
isTypedParent(parent, node) || | ||
isPropertyOfObjectWithType(parent) || | ||
isFunctionArgument(parent, node) || | ||
isConstructorArgument(parent) | ||
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. DRYing these out, it makes it more obvious that the recursive object-property case doesn't include this Happy to move it into |
||
); | ||
} | ||
|
Uh oh!
There was an error while loading. Please reload this page.