Skip to content

Parenthesized expressions#8025

Merged
jridgewell merged 2 commits intobabel:masterfrom
arv:parenthesized-expressions
Feb 23, 2019
Merged

Parenthesized expressions#8025
jridgewell merged 2 commits intobabel:masterfrom
arv:parenthesized-expressions

Conversation

@arv
Copy link
Contributor

@arv arv commented May 24, 2018

Q                       A
Fixed Issues? Fixes #7661, closes #7015
Patch: Bug Fix?
Major: Breaking Change?
Minor: New Feature? New feature
Tests Added + Pass? Yes
Documentation PR
Any Dependency Changes? No
License MIT

This adds a parser option called createParenthesizedExpressions, which when enabled creates ParenthesizedExpression nodes instead of setting extra.parenthesized.

@arv arv force-pushed the parenthesized-expressions branch 4 times, most recently from dfe4825 to eff42c6 Compare May 24, 2018 05:54
@nicolo-ribaudo
Copy link
Member

  1. I think that this shouldn't be a Babylon plugin (since plugins are meant to enable new syntax), but it should just be an option (which you already implemented).

  2. Can you add a test about (foo) => {} works with this new option enabled?

@nicolo-ribaudo nicolo-ribaudo added PR: New Feature 🚀 A type of pull request used for our changelog categories pkg: parser labels May 24, 2018
@arv arv force-pushed the parenthesized-expressions branch from eff42c6 to 8e9629a Compare May 25, 2018 19:47
@arv
Copy link
Contributor Author

arv commented May 25, 2018

All done!

  1. Removed the parser plugin
  2. Added more tests and fixed/improved issues related to lval.

@babel-bot
Copy link
Collaborator

babel-bot commented May 25, 2018

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

@babel-bot
Copy link
Collaborator

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

@@ -0,0 +1,3 @@
{
"parserOpts": {"createParenthesizedExpressions": true}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This works because I updated the runner in packages/babel-generator/test/index.js to spread the parserOpts into the parser options.

@arv arv force-pushed the parenthesized-expressions branch from 8e9629a to e1b1b23 Compare May 30, 2018 03:54
@arv
Copy link
Contributor Author

arv commented May 30, 2018

Rebased on top of 2a8ebbe

@arv arv force-pushed the parenthesized-expressions branch from e1b1b23 to f77818a Compare November 6, 2018 22:06
@arv
Copy link
Contributor Author

arv commented Nov 6, 2018

I'm still interested in getting this feature in. We are using Babel more and more in our tools and using a fork is getting old. Any chance of this getting another review?

@hzoo @nicolo-ribaudo

@arv
Copy link
Contributor Author

arv commented Feb 19, 2019

This is still blocking us. Any suggestions on how to proceed?

CC @rafaelweinstein

@nicolo-ribaudo nicolo-ribaudo self-requested a review February 19, 2019 19:55
@jridgewell
Copy link
Member

AMP Project also needed this. We solved it by running a custom plugin as the first step in our babel config:

// https://github.com/ampproject/amphtml/blob/2dc43d8df8a899f95348c2149aceddc9ca96b5a0/build-system/babel-plugins/babel-plugin-transform-parenthesize-expression/index.js
module.exports = function(babel) {
  const {types: t} = babel;
  return {
    visitor: {
      Expression: {
        exit(path) {
          const {node} = path;
          const {parenthesized} = node.extra || {};
          if (!parenthesized) {
            return;
          }

          path.replaceWith(t.parenthesizedExpression(node));
          path.skip();
        },
      },
    },
  };
};

A babel-parser config option would be welcome.

Copy link
Member

@nicolo-ribaudo nicolo-ribaudo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Apart from some minor comments, this PR looks pretty good.

return val;
const parenExpression = this.startNodeAt(startPos, startLoc);
parenExpression.expression = val;
this.finishNodeAt(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't using this.finishNode enough? The end position should be the correct one at this point I think.

@@ -0,0 +1 @@
({x}) = {x: 1};
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you also test ([ a ]) = []?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

@danez danez self-requested a review February 19, 2019 22:02
Copy link
Contributor Author

@arv arv left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • resolved the merge conflicts
  • Use finishNode instead of finishNodeAt
  • add test for ([a]) = []
  • remove dead code (parseParenExpression)

@@ -0,0 +1 @@
({x}) = {x: 1};
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

strictMode: ?boolean,
ranges: boolean,
tokens: boolean,
createParenthesizedExpressions: boolean,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This also needs to be added to babel-parser/typings/index.d.ts

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@arv
Copy link
Contributor Author

arv commented Feb 20, 2019

Thanks @danez

@nicolo-ribaudo nicolo-ribaudo added the PR: Ready to be Merged A pull request with already two approvals, but waiting for the next minor release label Feb 20, 2019
@nicolo-ribaudo nicolo-ribaudo added this to the v7.4.0 milestone Feb 20, 2019
Copy link
Member

@existentialism existentialism left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

arv added 2 commits February 23, 2019 02:28
When set to `true` we create `ParenthesizedExpression` nodes instead of
setting `extra.parenthesized`.
@jridgewell jridgewell force-pushed the parenthesized-expressions branch from 3f2e477 to 88ed72b Compare February 23, 2019 07:35
@jridgewell jridgewell merged commit dd8b700 into babel:master Feb 23, 2019
@nicolo-ribaudo
Copy link
Member

We were waiting to decide to release v7.4 😛

@jridgewell
Copy link
Member

Whoops, didn't see the milestone.

jridgewell added a commit that referenced this pull request Feb 23, 2019
nicolo-ribaudo pushed a commit that referenced this pull request Feb 25, 2019
nicolo-ribaudo pushed a commit to nicolo-ribaudo/babel that referenced this pull request Feb 25, 2019
* Add parser createParenthesizedExpressions option  …

When set to `true` we create `ParenthesizedExpression` nodes instead of
setting `extra.parenthesized`.

* Also update babel-parser.d.ts
@nicolo-ribaudo nicolo-ribaudo removed the PR: New Feature 🚀 A type of pull request used for our changelog categories label Feb 25, 2019
nicolo-ribaudo pushed a commit to nicolo-ribaudo/babel that referenced this pull request Mar 3, 2019
* Add parser createParenthesizedExpressions option  …

When set to `true` we create `ParenthesizedExpression` nodes instead of
setting `extra.parenthesized`.

* Also update babel-parser.d.ts
nicolo-ribaudo pushed a commit to nicolo-ribaudo/babel that referenced this pull request Mar 5, 2019
* Add parser createParenthesizedExpressions option  …

When set to `true` we create `ParenthesizedExpression` nodes instead of
setting `extra.parenthesized`.

* Also update babel-parser.d.ts
nicolo-ribaudo pushed a commit that referenced this pull request Mar 6, 2019
* Add parser createParenthesizedExpressions option  …

When set to `true` we create `ParenthesizedExpression` nodes instead of
setting `extra.parenthesized`.

* Also update babel-parser.d.ts
@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 pkg: parser PR: Ready to be Merged A pull request with already two approvals, but waiting for the next minor release

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Proposal: Add Closure Compiler Syntax Plugin

6 participants