I am using JSCodeshift to migrate some old JS files. One of the things I have to do is find all the functions (both normal and arrow functions) and then add some code to their body. To find both I just use the general Function constructor to search for all the possible functions. However, when I try to use the find nodes on the jscodeshift function I get a type error. So, this simple code can work as an example:
j(file.source).find(j.Function).filter((path) => {
j(path) // <- here is where the error happens
})
This is the full error:
Overload 2 of 2, '(source: ASTNode | ASTNode[] | ASTPath<ASTNode> | ASTPath<ASTNode>[]): Collection<any>', gave the following error.
Argument of type 'ASTPath<Function>' is not assignable to parameter of type 'ASTNode | ASTNode[] | ASTPath<ASTNode> | ASTPath<ASTNode>[]'.
Type 'NodePath<Function, Function>' is not assignable to type 'ASTPath<ASTNode>'.
Type 'Function' is not assignable to type 'ASTNode'.
Property 'key' is missing in type 'Function' but required in type 'ClassPrivateMethod'.ts(2769)
Looking at the definition of Error, it is extending ASTNode, so it should be compatible, no?
//ast-types/gen/namedTypes.d.ts
interface Function extends Node {
id?: K.IdentifierKind | null;
params: K.PatternKind[];
body: K.BlockStatementKind;
generator?: boolean;
async?: boolean;
expression?: boolean;
defaults?: (K.ExpressionKind | null)[];
rest?: K.IdentifierKind | null;
returnType?: K.TypeAnnotationKind | K.TSTypeAnnotationKind | null;
typeParameters?: K.TypeParameterDeclarationKind | K.TSTypeParameterDeclarationKind | null;
predicate?: K.FlowPredicateKind | null;
}
However if I use ArrowFunctionExpression as find query:
j(file.source).find(j.ArrowFunctionExpression).filter((path) => {
j(path)
})
it doesn't complain about the type, which is weird given that ArrowFunctionExpression just extends Function type
interface ArrowFunctionExpression extends Omit<Function, "type" | "id" | "body" | "generator">, Omit<Expression, "type"> {
type: "ArrowFunctionExpression";
id?: null;
body: K.BlockStatementKind | K.ExpressionKind;
generator?: false;
}