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

[JavaScript] Distinguish the comma operator from comma punctuation. #1551

Merged
merged 1 commit into from
Jun 18, 2018

Conversation

Thom1729
Copy link
Collaborator

@Thom1729 Thom1729 commented May 9, 2018

(See also #831.)

JavaScript uses the comma in two distinct ways. The most common is as an item separator, such as in function parameters and array or object literals. In these cases, it is and ought to be scoped punctuation.separator.

There is also the relatively obscure comma operator. This is a binary operator that evaluates both of its operands and returns the right operand. It is a true operator syntactically and semantically, just like + or -. It is rarely used deliberately, although a developer who omits semicolons may accidentally use it, thereby producing a bug.

The formal ECMAScript grammar is carefully written to maintain the distinction between the comma operator and comma punctuation. Sublime's JavaScript syntax is also capable of making this distinction (since #1009 and others).

Currently, the comma is always scoped punctuation.separator, even when the comma is an operator. This PR highlights the comma operator as keyword.operator.comma, leaving all other commas as punctuation.separator.comma. This is a one-line change: because the comma operator is syntactically a very different thing from comma punctuation, they are matched in different places.

In addition to probably being more correct, this change has an important practical purpose. If a developer forgets a semicolon, the resulting code may be a valid expression with a very different meaning. In particular, commas that would have been punctuation can become operators. If the JavaScript syntax respects this distinction, the effects of the typo will become immediately visible. Example:

someExpression()
[1, 2, 3].forEach(/*...*/);

In this example, the hapless programmer has forgotten the semicolon after someExpression(). The subsequent brackets, which were intended to be an array, are in fact a property access, and 1, 2, 3 is not a list of array items but an expression with two comma operators. The whole two-line expression is equivalent to (someExpression()[3]).forEach(/*...*/). If the comma operator is marked as punctuation, the error is obscure. If the comma operator is marked keyword.operator, the error is obvious.

While I do think that this PR is useful, I mostly think that it's more correct. Now that we have the ability to reliably make the distinction, I think that we should.

@wbond wbond merged commit e56c181 into sublimehq:master Jun 18, 2018
@Thom1729 Thom1729 deleted the javascript-comma-operator branch June 18, 2018 16:43
charlievieth pushed a commit to charlievieth/Packages that referenced this pull request Jul 25, 2018
deathaxe pushed a commit to deathaxe/sublime-packages that referenced this pull request Jun 9, 2019
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

Successfully merging this pull request may close these issues.

2 participants