@@ -37,6 +37,53 @@ function isPropertyDefinitionWithTypeAnnotation(
3737 ) ;
3838}
3939
40+ /**
41+ * Checks if a node belongs to:
42+ * ```
43+ * foo(() => 1)
44+ * ```
45+ */
46+ function isFunctionArgument (
47+ parent : TSESTree . Node ,
48+ callee ?: FunctionExpression ,
49+ ) : parent is TSESTree . CallExpression {
50+ return (
51+ parent . type === AST_NODE_TYPES . CallExpression &&
52+ // make sure this isn't an IIFE
53+ parent . callee !== callee
54+ ) ;
55+ }
56+
57+ /**
58+ * Checks if a node is type-constrained in JSX
59+ * ```
60+ * <Foo x={() => {}} />
61+ * <Bar>{() => {}}</Bar>
62+ * <Baz {...props} />
63+ * ```
64+ */
65+ function isTypedJSX (
66+ node : TSESTree . Node ,
67+ ) : node is TSESTree . JSXExpressionContainer | TSESTree . JSXSpreadAttribute {
68+ return (
69+ node . type === AST_NODE_TYPES . JSXExpressionContainer ||
70+ node . type === AST_NODE_TYPES . JSXSpreadAttribute
71+ ) ;
72+ }
73+
74+ function isTypedParent (
75+ parent : TSESTree . Node ,
76+ callee ?: FunctionExpression ,
77+ ) : boolean {
78+ return (
79+ isTypeAssertion ( parent ) ||
80+ isVariableDeclaratorWithTypeAnnotation ( parent ) ||
81+ isPropertyDefinitionWithTypeAnnotation ( parent ) ||
82+ isFunctionArgument ( parent , callee ) ||
83+ isTypedJSX ( parent )
84+ ) ;
85+ }
86+
4087/**
4188 * Checks if a node belongs to:
4289 * ```
@@ -78,13 +125,7 @@ function isPropertyOfObjectWithType(
78125 return false ;
79126 }
80127
81- return (
82- isTypeAssertion ( parent ) ||
83- isPropertyDefinitionWithTypeAnnotation ( parent ) ||
84- isVariableDeclaratorWithTypeAnnotation ( parent ) ||
85- isFunctionArgument ( parent ) ||
86- isPropertyOfObjectWithType ( parent )
87- ) ;
128+ return isTypedParent ( parent ) || isPropertyOfObjectWithType ( parent ) ;
88129}
89130
90131/**
@@ -127,23 +168,6 @@ function doesImmediatelyReturnFunctionExpression({
127168 ) ;
128169}
129170
130- /**
131- * Checks if a node belongs to:
132- * ```
133- * foo(() => 1)
134- * ```
135- */
136- function isFunctionArgument (
137- parent : TSESTree . Node ,
138- callee ?: FunctionExpression ,
139- ) : parent is TSESTree . CallExpression {
140- return (
141- parent . type === AST_NODE_TYPES . CallExpression &&
142- // make sure this isn't an IIFE
143- parent . callee !== callee
144- ) ;
145- }
146-
147171/**
148172 * Checks if a function belongs to:
149173 * ```
@@ -194,11 +218,8 @@ function isTypedFunctionExpression(
194218 }
195219
196220 return (
197- isTypeAssertion ( parent ) ||
198- isVariableDeclaratorWithTypeAnnotation ( parent ) ||
199- isPropertyDefinitionWithTypeAnnotation ( parent ) ||
221+ isTypedParent ( parent , node ) ||
200222 isPropertyOfObjectWithType ( parent ) ||
201- isFunctionArgument ( parent , node ) ||
202223 isConstructorArgument ( parent )
203224 ) ;
204225}
0 commit comments