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

[[FEAT]] Add new "enforcing" option: trailingcomma (updated) #3090

Merged
merged 2 commits into from
Apr 3, 2017

Conversation

jugglinmike
Copy link
Member

This changeset continues the work first submitted by @jacksonrayhamilton in
gh-2100. As discussed in that pull request, a few changes to master in the
years since he submitted have introduced conflicts in the patch. I've resolved
those with a merge commit because I believe that's the most transparent way to
document this collaboration.

jacksonrayhamilton and others added 2 commits January 15, 2017 17:36
Missing trailing commas in array and object literals can result in
undetectable visual ambiguities--the same visual ambiguities that lead
to bugs when a programmer relies on automatic semicolon insertion.
Therefore, it might be a good practice to always use them, so this
option provides warnings in their absence.

For example, this code might have worked last Tuesday:

    [
        b + c
    ].forEach(print);

But if one adds an element to the array and forgets to compensate for
the missing comma, no syntax error is thrown, and a linter cannot
determine if this was a mistake or an intentional function invocation:

    [
        b + c
        (d + e)
    ].forEach(print);

If one always appends a list item with a comma, this ambiguity cannot
occur:

    [
        b + c,
    ].forEach(print);

    [
        b + c,
        (d + e),
    ].forEach(print);

If this option is `true`, a warning will be thrown if a "," does not
occur before the closing "]" or "}" in an array or object literal. If
option `es3` is also `true`, this option does nothing, because otherwise
the options would contradict each other, and the `es3` mandate is more
pressing.

There is no inverse of this option, that would warn if an ES5 programmer
omitted a trailing comma. Omission of a trailing comma in ES5 provides
no benefit, unlike option `trailingcomma`, which actually helps prevent
bugs. Such an inverse feature would be considered "stylistic;" do not
confuse `trailingcomma` as a "style" option, as it is no more
"stylistic" than the default detection of semicolons is.
Resolving conflicts:

- The `inES5` method is now defined on the state object directly; update
  call sites accordingly
- Remove `true` parameter from invocation of `inES5` so that ES6+
  environments may also opt in to this functionality
- Update the identifier of the warning to accomodate warnings that have
  been introduced since the patch was submitted
- Enforce this restriction even in the absence of the configuration
  option for array initializers in ES3
@coveralls
Copy link

coveralls commented Jan 15, 2017

Coverage Status

Coverage increased (+0.002%) to 97.757% when pulling be28b71 on jugglinmike:2100-trailing-comma-updated into 0938dc7 on jshint:master.

@@ -2743,6 +2743,9 @@ var JSHINT = (function() {
break;
}
} else {
if (state.option.trailingcomma && state.inES5()) {

Choose a reason for hiding this comment

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

Do you think the ES5 check should be removed? (Continuing from here: #2100 (comment).)

From a language purist perspective, it should be. Although for practical reasons it is probably still useful for any sorry soul who still has to support old IE. I don't see an option specifically for detecting old IE bugs, otherwise I'd suggest that instead.

Copy link
Member Author

Choose a reason for hiding this comment

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

JSHint has a few IE-specific warnings (e.g. W070, "Extra comma. (it breaks
older versions of IE)"), and rather than referencing an option dedicated to
that condition, it applies these a little inconsistently--when the environment
does not support ES5. So this behavior respects precedence.

But like you, I'm not sure the precedent is a good one. I'm thinking that a new
"enforcing" option like oldie might be good, especially from a language
purist's perspective. I've written up a proposal here: gh-3094

/**
* This option warns when a comma is not placed after the last element in an
* array or object literal. Due to bugs in old versions of IE, trailing
* commas used to be discouraged, but since ES5 their semantics were

Choose a reason for hiding this comment

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

As you mentioned here, the syntax was actually specified before ES5. If we went the "practical" route I suggest here, we could reword this to, "but since IE implementations of ES5, their semantics became uniform across most JS environments."

@rwaldron
Copy link
Member

rwaldron commented Apr 3, 2017

This is great work :)

@rwaldron rwaldron merged commit 42dc572 into jshint:master Apr 3, 2017
jugglinmike added a commit to jugglinmike/jshint that referenced this pull request Apr 9, 2017
Missing trailing commas in array and object literals can result in
undetectable visual ambiguities--the same visual ambiguities that lead
to bugs when a programmer relies on automatic semicolon insertion.
Therefore, it might be a good practice to always use them, so this
option provides warnings in their absence.

For example, this code might have worked last Tuesday:

    [
        b + c
    ].forEach(print);

But if one adds an element to the array and forgets to compensate for
the missing comma, no syntax error is thrown, and a linter cannot
determine if this was a mistake or an intentional function invocation:

    [
        b + c
        (d + e)
    ].forEach(print);

If one always appends a list item with a comma, this ambiguity cannot
occur:

    [
        b + c,
    ].forEach(print);

    [
        b + c,
        (d + e),
    ].forEach(print);

If this option is `true`, a warning will be thrown if a "," does not
occur before the closing "]" or "}" in an array or object literal. If
option `es3` is also `true`, this option does nothing, because otherwise
the options would contradict each other, and the `es3` mandate is more
pressing.

There is no inverse of this option, that would warn if an ES5 programmer
omitted a trailing comma. Omission of a trailing comma in ES5 provides
no benefit, unlike option `trailingcomma`, which actually helps prevent
bugs. Such an inverse feature would be considered "stylistic;" do not
confuse `trailingcomma` as a "style" option, as it is no more
"stylistic" than the default detection of semicolons is.
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.

None yet

4 participants