Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

recast.print modifies original source adding brackets around some statements #1191

Open
phoenisx opened this issue Sep 16, 2022 · 2 comments · May be fixed by #1257
Open

recast.print modifies original source adding brackets around some statements #1191

phoenisx opened this issue Sep 16, 2022 · 2 comments · May be fixed by #1257

Comments

@phoenisx
Copy link

phoenisx commented Sep 16, 2022

Ref: facebook/jscodeshift#176

For e.g. Let's consider the following source code as input passed to recast.print();

const code = [
  "async function add(a, b) {",
  "  const z = foo || await getFoo();",
  "  return z;",
  "}"
].join("\n");

const ast = recast.parse(code);
recast.print(ast);

Behaviour:

Prints a modified code, with await getFoo() surrounded by parentheses (normal brackets).

// PrintResult {
//   code: 'async function add(a, b) {\n' +
//     '  const z = foo || (await getFoo());\n' +
//     '  return z;\n' +
//     '}'
// }

Expected Behaviour:

Should not contain parenthesis if they were not present in the original code.

// PrintResult {
//   code: 'async function add(a, b) {\n' +
//     '  const z = foo || await getFoo();\n' +
//     '  return z;\n' +
//     '}'
// }

If there are any restrictions in supporting this, can it be possible to put this "adding auto parentheses" feature behind a recast options toggle?

Also if you need any support, I can work on this issue with some guidance 👍🏽
Thanks for all your efforts and creating this awesome library 🙇🏽 💯

@liukaigsx
Copy link

you can comment out this line at lib\printer.js
shouldAddParens = path.needsParens();

@phoenisx
Copy link
Author

Thanks @liukaigsx
I tried modifying the code and could remove the parens keepingshouldAddParens === false.

I will raise a PR in some time to add a new config so that users can avoid adding parens while printing.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants