Skip to content

Add retainExtraParens option into babel-generator#7015

Closed
koba04 wants to merge 6 commits intobabel:masterfrom
koba04:add-retain-parens-option
Closed

Add retainExtraParens option into babel-generator#7015
koba04 wants to merge 6 commits intobabel:masterfrom
koba04:add-retain-parens-option

Conversation

@koba04
Copy link
Contributor

@koba04 koba04 commented Dec 12, 2017

Q                       A
Fixed Issues?
Patch: Bug Fix?
Major: Breaking Change?
Minor: New Feature? Yes
Tests Added + Pass? Yes
Documentation PR
Any Dependency Changes?
License MIT

This PR is to add a new option retainParents, which is to retain parens even if they are meaningless for parsers.
I know this feature is useless for many cases but it's useful for some cases.
(Parens are in there for a reason)

I think this option shouldn't enable by default, but it makes sense to able to enable it by optional.

Thanks!

@koba04
Copy link
Contributor Author

koba04 commented Dec 12, 2017

This is the blocker to use Babel in our project.
I want to avoid to use forked Babel because it's a very painful so please consider this PR🙏

@babel-bot
Copy link
Collaborator

babel-bot commented Dec 12, 2017

Build successful! You can test your changes in the REPL here: https://babeljs.io/repl/build/6314/

@loganfsmyth
Copy link
Member

My concern with this any time is comes up is that to define an API that we can confidently maintain, we need to know what we are guaranteeing. If a random plugin decides to clear node.extra and the parens vanish, how are users supposed to know what happened, for example? Or if we replace the node being cast with another node, none of our current logic preserves .extra. Moreover, any existing plugin could introduce a new piece of code to cause that in the future, and I wouldn't consider it a breaking change currently, but with this flag enabled, any change like that could qualify as a breaking change.

For example if a user does

import foo from "bar";

var other = /** ... */(foo);

and we transform that to

var _bar = require("bar");

var other = _bar.default;

the foo AST node is just gone, and the member expression has no .extra.

@koba04
Copy link
Contributor Author

koba04 commented Dec 13, 2017

@loganfsmyth Thank you for your feedback!
I've added a commit to reach out your concern.

I thought comments also have the same problem but comments are passed from an oldNode to a newNode so I've applied it to parens as well as comments.
Does it make sense?

@loganfsmyth
Copy link
Member

The issue with emulating comments is that we already don't make any guarantees about comments. Babel is only defined to make a best effort at preserving comments, because comments have no functional affect on runtime code, and people already run into issues with that. They'd likely have the same issues with this, so we'd be introducing a new thing for people to trip on.

@loganfsmyth
Copy link
Member

To do this "right" given our current structure, we'd have to have an actual AST node for parenthesized expressions, and then every single transform we and anyone else writes would have to make sure to handle it, which would be a mountain of work for everyone involved.

@koba04
Copy link
Contributor Author

koba04 commented Dec 15, 2017

@loganfsmyth
Thank you!
From my perspective, this implementation could cover many cases even though there are no guarantees as you've pointed out.
Do you have any issues or examples for that?
(BTW, extraParens might be a better name than retainParens as an option name)

I think it's hard to guarantee to work this well against any AST operations.
But I could cover more cases.
Doesn't it make sense to fix the problems at the same time with comments in later?

Is having an actual AST node for Parens(ParenthesizedExpression?) the only way to add this feature?
If so, I'll work on this.

@koba04 koba04 changed the title Add retain parens option Add retainExtraParens option into babel-generator Dec 22, 2017
@koba04 koba04 force-pushed the add-retain-parens-option branch from 2d2750e to b7c7353 Compare December 22, 2017 17:32
@koba04 koba04 force-pushed the add-retain-parens-option branch from b7c7353 to 9af3ce7 Compare December 22, 2017 17:37
@koba04
Copy link
Contributor Author

koba04 commented Dec 22, 2017

I've updated this to remove the option from babel-core and rename the option name to retainExtraParens.

@arv
Copy link
Contributor

arv commented Mar 20, 2018

I'm running into this as well. Removing the parens should really be up to babel/minify.

@arv
Copy link
Contributor

arv commented Apr 10, 2018

I believe retaining the parens through the transformation passes is always the right thing to do.

Maybe the option should be called printAllParens or printExtraParens.

@arv
Copy link
Contributor

arv commented May 7, 2018

I want us to make progress on this. Here is my proposal:

  • Create a new PR that contains the code to inheritParens so that the parens are not lost in transformations. (This should be non controversial)
  • After that we can go a few ways:
    • Add a flag to print extra parens?
    • Always print extra parens after a /** */ comment (when printing comments)?
    • Add a closure compiler flag?

@koba04 @loganfsmyth WDYT?

arv added a commit to arv/babel that referenced this pull request May 11, 2018
If the original expression had parens then add parens around the new
expression too,

Based on  babel#7015

Co-authored-by: Toru Kobayashi <koba0004@gmail.com>
arv added a commit to arv/babel that referenced this pull request May 11, 2018
If the original expression had parens then add parens around the new
expression too,

Based on  babel#7015

Co-authored-by: Toru Kobayashi <koba0004@gmail.com>
@arv arv mentioned this pull request Feb 20, 2019
@lock lock bot added the outdated A closed issue/PR that is archived due to age. Recommended to make a new issue label Oct 4, 2019
@lock lock bot locked as resolved and limited conversation to collaborators Oct 4, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

outdated A closed issue/PR that is archived due to age. Recommended to make a new issue

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants