ast-types
ast-types copied to clipboard
ArrowFunctionExpression fails to check arrow functions without body and the type is also wrong
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 the body type is wrongly defined here:
https://github.com/benjamn/ast-types/blob/b99f9b3ef5000631c06d71d91ee7aa341c9a81fb/gen/namedTypes.ts#L339ArrowFunctionExpression.check(path) and also
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.
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 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 the thing is that JSXElement can be a the only body of an arrow function:

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