comma-style - ESLint - Pluggable JavaScript Linter (original) (raw)

Enforce consistent comma style

🔧 Fixable

Some problems reported by this rule are automatically fixable by the --fix command line option

Table of Contents

  1. Rule Details
  2. Options
    1. last
    2. first
    3. exceptions
  3. When Not To Use It
  4. Related Rules
  5. Version
  6. Further Reading
  7. Resources

The Comma Style rule enforces styles for comma-separated lists. There are two comma styles primarily used in JavaScript:

One of the justifications for using Comma First style is that it can help track missing and trailing commas. These are problematic because missing commas in variable declarations can lead to the leakage of global variables and trailing commas can lead to errors in older versions of IE.

Rule Details

This rule enforce consistent comma style in array literals, object literals, and variable declarations.

This rule does not apply in either of the following cases:

Options

This rule has a string option:

This rule also accepts an additional exceptions object:

A way to determine the node types as defined by ESTree is to use AST Explorer with the espree parser.

last

Examples of incorrect code for this rule with the default "last" option:

Open in Playground

/*eslint comma-style: ["error", "last"]*/

var foo = 1
,
bar = 2;

var foo = 1
  , bar = 2;

var foo = ["apples"
           , "oranges"];

function baz() {
    return {
        "a": 1
        ,"b:": 2
    };
}

Examples of correct code for this rule with the default "last" option:

Open in Playground

/*eslint comma-style: ["error", "last"]*/

var foo = 1, bar = 2;

var foo = 1,
    bar = 2;

var foo = ["apples",
           "oranges"];

function baz() {
    return {
        "a": 1,
        "b:": 2
    };
}

first

Examples of incorrect code for this rule with the "first" option:

Open in Playground

/*eslint comma-style: ["error", "first"]*/

var foo = 1,
    bar = 2;

var foo = ["apples",
           "oranges"];

function baz() {
    return {
        "a": 1,
        "b:": 2
    };
}

Examples of correct code for this rule with the "first" option:

Open in Playground

/*eslint comma-style: ["error", "first"]*/

var foo = 1, bar = 2;

var foo = 1
    ,bar = 2;

var foo = ["apples"
          ,"oranges"];

function baz() {
    return {
        "a": 1
        ,"b:": 2
    };
}

exceptions

An example use case is to enforce comma style only in var statements.

Examples of incorrect code for this rule with sample "first", { "exceptions": { … } } options:

Open in Playground

/*eslint comma-style: ["error", "first", { "exceptions": { "ArrayExpression": true, "ObjectExpression": true } }]*/

var o = {},
    a = [];

Examples of correct code for this rule with sample "first", { "exceptions": { … } } options:

Open in Playground

/*eslint comma-style: ["error", "first", { "exceptions": { "ArrayExpression": true, "ObjectExpression": true } }]*/

var o = {fst:1,
         snd: [1,
               2]}
  , a = [];

When Not To Use It

This rule can safely be turned off if your project does not care about enforcing a consistent comma style.

Version

This rule was introduced in ESLint v0.9.0.

Further Reading

Avatar image for gist.github.com

Resources