Skip to content

Proposal for enabling shorthand notation for object literal properties #767

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

Closed
wants to merge 2 commits into from

Conversation

mhegazy
Copy link
Contributor

@mhegazy mhegazy commented Sep 28, 2014

This allows ES6 shorthand object literal property definition. This addresses #418. e.g.:

var  a;
var x = { a }; // equivalent to {a : a }

Notes:

  • Only identifiers are allowed, keywords are disallows { var }
  • Qualified names are not allowed { a.b }
  • Any rewriting that would normally happen on the initializer should still occur. e.g.
module m {
    export var x;
}
module m {
   var y = { x }; // should emit { x : m.x }
}

parseExpected(SyntaxKind.ColonToken);
node.initializer = parseAssignmentExpression(false);
else if (token !== SyntaxKind.ColonToken && nameToken === SyntaxKind.Identifier) {
node.initializer = clone(node.name); } else { parseExpected(SyntaxKind.ColonToken); node.initializer = parseAssignmentExpression(false);
Copy link
Contributor

Choose a reason for hiding this comment

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

This seems highly undesirable. We now have a tree that has multiple tokens in it that have the exact same data (i.e. same position/width/etc.)

These trees break rules and lead to funky situations. For example, will the token show up multiple times when getting the children in the services layer? What should features like the formatter do here? Will formatting {a} end up with {a:a}?

We should ahve a new node that represents this type of property assignment.

Or, just dn't have an initializer.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fair. I was lazy. Will fix it.

Copy link
Contributor

Choose a reason for hiding this comment

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

Fix indentation

@CyrusNajmabadi
Copy link
Contributor

If we start taking on ES6 features, should we also add a ES6 as an acceptable target version? In ES6 emit mode these would stay as { a } while in ES5/ES3 emit it would be { a: a }

@mhegazy
Copy link
Contributor Author

mhegazy commented Sep 29, 2014

When we have an ES6 target we would emit the short hand. In ES3/ES5 we would emit the equivalent long form.

@mhegazy mhegazy closed this Oct 28, 2014
@mhegazy mhegazy deleted the propertiesShortHand branch October 28, 2014 16:16
@microsoft microsoft locked and limited conversation to collaborators Jun 18, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants