ast-types icon indicating copy to clipboard operation
ast-types copied to clipboard

ArrowFunctionExpression fails to check arrow functions without body and the type is also wrong

Open danielo515 opened this issue 3 years ago • 3 comments

There are arrow functions whose body is a JSXExpression, like this:

const bla = (name: string) => <div>hello {name}</div>

That function will return false if you run ArrowFunctionExpression.check(path) and also the body type is wrongly defined here: https://github.com/benjamn/ast-types/blob/b99f9b3ef5000631c06d71d91ee7aa341c9a81fb/gen/namedTypes.ts#L339

Because the type of that body is JSXElement, which doesn't have a nested body, which the other two does. This can lead to uncaught bugs.

danielo515 avatar Dec 01 '21 11:12 danielo515

Ok, found an error on my code, I was passing down the wrapper path to the check function rather than the actual node, doing ArrowFunctionExpression.check(path.value) yields true. However te body type is still wrong

danielo515 avatar Dec 01 '21 12:12 danielo515

@danielo515 I think JSXElement should be a subtype of Expression? https://github.com/benjamn/ast-types/blob/b99f9b3ef5000631c06d71d91ee7aa341c9a81fb/gen/namedTypes.ts#L666

In your case, both JSXElement.check(arrowFunExpr.body) and Expression.check(arrowFunExpr.body) should return true, so I think the body type might be okay?

benjamn avatar Dec 03 '21 19:12 benjamn

@benjamn the thing is that JSXElement can be a the only body of an arrow function: image

All the other two types have a nested body but the JSXElement does not so you can mistakenly try to access it.

danielo515 avatar Dec 09 '21 10:12 danielo515