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

Proposal: ParenthesizedType #193

Open
k2snowman69 opened this issue Dec 26, 2018 · 3 comments
Open

Proposal: ParenthesizedType #193

k2snowman69 opened this issue Dec 26, 2018 · 3 comments

Comments

@k2snowman69
Copy link

k2snowman69 commented Dec 26, 2018

This proposal is to add a parenthesis type for expressions. This was slightly mentioned in the CST issue but I think this is much more basic, hence splitting it out into it's own issue.

Issue

Note: yes this is very contrived and will always result in the same value but it's trimmed down for this example:

let value = (2) | 1;

The trimmed down output of this without a parenthesized type is:

{
    type: "BinaryExpression",
    range: [12, 19],
    operator: "|",
    left: {
      type: "Literal",
      range: [13, 14],
      value: 2,
      raw: "2"
    },
    right: {
      type: "Literal",
      range: [18, 19],
      value: 1,
      raw: "1"
    }
  };

which means the context of the parenthesis is missing. This means if you were to reprint this document, you have no context as to where a user may or may not have added parenthesis to break up expressions.

For me, I'm writing a cli that helps order code so losing the context of the parenthesis means that my code now reorders this binary expression like this

let value = (1) | 2;

Which isn't what the developer initially intended

Proposal

Adding a ParenthesizedType will allow for the context of the parenthesis to be conveyed in the AST

{
    type: "BinaryExpression",
    range: [12, 19],
    operator: "|",
    left: {
      type: "ParenthesisExpression",
      range: [12, 15],
      raw: "(2)",
      value: {
        type: "Literal",
        range: [13, 14],
        value: 2,
        raw: "2"
      }
    },
    right: {
      type: "Literal",
      range: [18, 19],
      value: 1,
      raw: "1"
    }
  }

Credit to where credit is due, I got this idea from TSParenthesizedType from typescript-estree

@Meir017
Copy link

Meir017 commented May 11, 2022

is this related to the following scenario?

(fn1) = function(){}
fn2 = function(){}

image
viewing this in astexplorer.net shows identical trees for both
image

@k2snowman69
Copy link
Author

Yep, exactly. These two trees should not be identical and instead for the left identifier there should be a ParenthesizedType

@Meir017
Copy link

Meir017 commented May 15, 2022

related babel issue recently fixed - babel/babel#14514
babel has an extra property on node objects where this metadata is saved
from the PR - image

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

No branches or pull requests

2 participants