benjamn / benjamn/recast

Transforming IIFE results in extra parentheses

Open
#327 8 comments 0 reactions 1 assignee Claimed by @benjamn View on GitHub
Dominant language
TypeScript
Stars
5.3k
Forks
363
Avg merge
3d 8h
Merged PRs (30d)
3

Description

When transforming normal function to arrow function:

``` js
const recast = require('recast');

const ast = recast.parse('(function(){})()');
ast.program.body[0].expression.callee.type = 'ArrowFunctionExpression';

console.log(recast.print(ast).code);
```

the above outputs:

```
((() => {}))()
```

instead of the expected:

```
(() => {})()
```

Recast seems to correctly detect that this IIFE needs to be wrapped in parens, but fails to detect that there are already parenthesis present. When I just let Recast print a plain AST, it gets the parenthesis right:

``` js
console.log(recast.print({
type: 'ExpressionStatement',
expression: {
type: 'CallExpression',
callee: {
type: 'ArrowFunctionExpression',
params: [],
body: {
type: 'BlockStatement',
body: [],
}
},
arguments: [],
}
}).code);
```

Seems to be similar to an old issue #81

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.