commas.md

April 27, 2023 · View on GitHub

Leading and Trailing

Never use leading but trailing commas. As of 5 version 2.0.0 this has also been set as default configuration:

function snow(,) {} // SyntaxError: missing formal parameter
(,) => {}; // SyntaxError: expected expression, got ','
snow(,) // SyntaxError: expected expression, got ','
function snow(...flakes,) {} // SyntaxError: parameter after rest parameter
(...flakes,) => {} // SyntaxError: expected closing parenthesis, got ','

ESLint: comma-style and comma-dangle

Examples

Incorrect code for this rule:

const winter = [
    snow
  , frost
  , ice
];
const winter = {
    name: "winter"
  , elements: ["snow", "frost", "ice"]
  , temperature: -12
  , state: "frozen"
};
const winter = [
  snow,
  frost,
  ice,
];
const winter = {
  name: "winter",
  elements: ["snow", "frost", "ice"],
  temperature: -12,
  state: "frozen",
};

Correct code for this rule:

const winter = [
  snow,
  frost,
  ice
];
const winter = {
  name: "winter",
  elements: ["snow", "frost", "ice"],
  temperature: -12,
  state: "frozen",
};