[JavaScript] Distinguish the comma operator from comma punctuation. #1551
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
(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 askeyword.operator.comma
, leaving all other commas aspunctuation.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:
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, and1, 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 aspunctuation
, the error is obscure. If the comma operator is markedkeyword.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.