Skip to content

Disallow trailing commas in object literals entirely when targeting ES3#284

Closed
DanielRosenwasser wants to merge 4 commits intomasterfrom
disallowTrailingObjectLiteralCommasInES3
Closed

Disallow trailing commas in object literals entirely when targeting ES3#284
DanielRosenwasser wants to merge 4 commits intomasterfrom
disallowTrailingObjectLiteralCommasInES3

Conversation

@DanielRosenwasser
Copy link
Copy Markdown
Member

The idea is that since the ECMAScript 3 spec does not permit trailing commas in an object literal, we should not permit the construct unless we have a revised grammar for TypeScript that says otherwise.

This is a breaking change. I would personally rather amend the specification to permit trailing commas and simply not preserve them.

This is an extension of the work on #273.

This was done because trailing commas in object literals are not accepted by ES3.

Fixes #271.
Also removed trailing commas from object literals in the compiler and certain tests.
@DanielRosenwasser DanielRosenwasser changed the title Disallow Trailing Commas in Object Literals Entirely for ES3 Targets Disallow trailing commas in object literals entirely when targeting ES3 Jul 28, 2014
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

if (i > 0) {
result += ",\r\n";
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

or .join(",\r\n")

@CyrusNajmabadi
Copy link
Copy Markdown
Contributor

👍

@mhegazy
Copy link
Copy Markdown
Contributor

mhegazy commented Jul 29, 2014

I do not think this is the right fix. we do not need to error on this. it is trivial enough to remove. please hold off on this change until we discuss it.

@DanielRosenwasser
Copy link
Copy Markdown
Member Author

I know, I'm just putting this change out there for the sake of discussion. I believe that the original fix (#273) supplies the right user experience, but in that case we need to amend the specification to reflect this.

@CyrusNajmabadi
Copy link
Copy Markdown
Contributor

I believe the "correct" thing to do is error. That said, being correct is not always the "right" thing to do (esp. as we've shipped).

I would simply document that TypeScript augments the ES3 grammar to add hte comma.

@sophiajt
Copy link
Copy Markdown
Contributor

If it was common practice in browsers to allow this for ES3, than it seems an unnecessarily strict check on our part. We would need to show that this isn't the case.

Perhaps as a meta point, new restrictions should be done judiciously and should be giving tangible user benefit. I'm unclear on this point for this patch.

@ahejlsberg
Copy link
Copy Markdown
Member

👎 I don't like this fix. It actually breaks running the compiler with cscript.exe because cscript.exe only supports ES3. Also, I don't see why it is so important to preserve a trailing comma in an object literal as it has no semantic meaning. There are lots of other things we don't preserve in the emitter. For example, we always emit semicolons even if they weren't present in the input.

@DanielRosenwasser
Copy link
Copy Markdown
Member Author

Actually, this commit should be fine for cscript, but it's bad for the end user (and us, since we use trailing commas everywhere. I agree that it should not be merged.

The problem is that if we don't do this, we no longer conform with the spec. What would be appropriate is if in the spec, we said we use ES5 parsing rules for trailing commas in object literals.

@mhegazy
Copy link
Copy Markdown
Contributor

mhegazy commented Aug 2, 2014

Just to clear the confusion, this was an alternative fix for #271 that Daniel wanted to bring forward. I believe everyone agrees that this is a restriction that does not add value to the user, and omitting the trailing comma, though not inline with the sprit of staying close to the input is the right thing to do here.

Daniel has already checked in a different change in #273. so closing this request.

@mhegazy mhegazy closed this Aug 2, 2014
@DanielRosenwasser DanielRosenwasser deleted the disallowTrailingObjectLiteralCommasInES3 branch August 3, 2014 01:09
@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

Breaking Change Would introduce errors in existing code

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants