babel-literal-to-ast icon indicating copy to clipboard operation
babel-literal-to-ast copied to clipboard

Function not valid when nested as ObjectMember

Open hypervillain opened this issue 4 years ago • 0 comments

Hey 👋,

I found your repo while looking for ways to transform literal functions. But my code broke when trying to transform functions like:

toAst({
    myFunction: function myFunction(prop) { return props }
})

Which results in an error (approx.) objectProperty expects Expression or PatternLike. The reason for this to break is because running the Babylon parser on the stringified function returns a node of type File.

I tried to handle this case in my own toAST method, here: https://github.com/hypervillain/babel-transform-config/blob/master/plugin/toAst.js#L35

It's probably not the most effective solution yet but it returns the right data type in my case. Unfortunately, it does not handle anonymous function either.

Anyway, I thought you might have an opinion on this subject. I'd be happy to collaborate on something less naive than my drafted solution 👍

hypervillain avatar Mar 19 '20 13:03 hypervillain