benjamn / benjamn/recast

Building MethodDefinition with an ArrowFunctionExpression crashes the printer

Open
#707 2 comments 0 reactions 0 assignees View on GitHub
Dominant language
TypeScript
Stars
5.3k
Forks
363
Avg merge
3d 8h
Merged PRs (30d)
3

Description

I am moderately new to recast. While it is a fantastic library, there appears to be no extended documentation, that would include rules and examples for all functionality. Therefore, i sometimes go by type annotation.

`recast.types.builders.methodDefinition` takes a `FunctionKind` as third parameter. While the "finished product" will contain a `FunctionExpression`, it's plausible to me, that it would just internally convert other related types. However, giving it an `ArrowFunctionExpression` creates an AST, which crashes the printer:

const recast = require("recast");

let ast = recast.parse("");

ast.program.body.push(recast.types.builders.classDeclaration(
recast.types.builders.identifier("myClass"),
recast.types.builders.classBody(
[recast.types.builders.methodDefinition(
"method",
recast.types.builders.identifier("myMethod"),
recast.types.builders.arrowFunctionExpression(
[],
recast.types.builders.blockStatement([])
)
)]
)
));

// printing the invalid (?) ast causes the exception.
console.log(recast.print(ast).code);

> TypeError: Cannot read property 'map' of undefined

Using recast from NPM in node, `"recast": "^0.18.10"`

I cannot determine, whether this is a bug (in recast or ast-types), or simply misuse on my side (probably a bit of both). Also, any additional documentation apart from the related github pages would be beneficial, or maybe a place to ask, when needed (discord, irc, ...). In my case, I simply manually built a `FunctionExpression` from the `ArrowFunctionExpression` i had as input, used that, and it worked fine.

Contributor guide

No contributing guide indexed for this repository

Research direction

Start by running the supplied reproducer through recast.types.builders.methodDefinition and recast.print. Trace how the ArrowFunctionExpression reaches the printer and compare it with the manually built FunctionExpression. Done means the supported input and expected behavior are clear, and the reproducer no longer crashes when the input is valid.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript
Domain
tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.