Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does this trigger comma-dangle rule in eslint?

Tags:

eslint

This looks correct to me, but why does eslint show a rule violation, missing trailing comma comma-dangle at the end of the last property "credentials"?

  dispatch({
    type: LOGIN_USER,
    payload: credentials
  });

.eslintrc

{
  "extends": "airbnb",
  "globals": {
    "__DEV__": true
  },
  "rules": {
    "react/jsx-quotes": 0,
    "jsx-quotes": [2, "prefer-double"]
  }
}
like image 915
MonkeyBonkey Avatar asked Oct 27 '15 15:10

MonkeyBonkey


People also ask

What is comma dangle?

Require or disallow trailing commas. 🛠 Some problems reported by this rule are automatically fixable by the --fix command line option.

What is trailing comma in JSON?

Trailing commas in JSON As JSON is based on a very restricted subset of JavaScript syntax, trailing commas are not allowed in JSON. Both lines will throw a SyntaxError : JSON. parse("[1, 2, 3, 4, ]"); JSON. parse('{"foo" : 1, }'); // SyntaxError JSON.parse: unexpected character // at line 1 column 14 of the JSON data.

What is trailing comma in Python?

Description: In Python, a tuple is actually created by the comma symbol, not by the parentheses. Unfortunately, one can actually create a tuple by misplacing a trailing comma, which can lead to potential weird bugs in your code.

How do you bypass Eslint?

To temporarily turn off ESLint, you should add a block comment /* eslint-disable */ before the lines that you're interested in: /* eslint-disable */ console.


2 Answers

I use the following combination (this works for me)

[1] .eslintrc.json

"rules": {        
        "comma-dangle": [2, "always-multiline"]         
    }

[2] JsPrettier | Prettier

"trailingComma": "all"

Results

dispatch({
  type: LOGIN_USER,
  payload: credentials,
});
like image 53
OnlyZero Avatar answered Oct 11 '22 03:10

OnlyZero


Based on the airbnb config the rule is setup like this comma-dangle: [2, "always-multiline"].

Acoording to this, The expected code is

  dispatch({
    type: LOGIN_USER,
    payload: credentials,
  });

It expects a , at the end.

More info on rule: http://eslint.org/docs/rules/comma-dangle

like image 39
Gyandeep Avatar answered Oct 11 '22 02:10

Gyandeep