简体   繁体   中英

Eslint parse error on comma dangle in functions

I couldn't find an answer to this question on SO (it might be there, I just couldn't find it), so I'm posting the question and the answer I finally found. I hope it helps.

When using a dangling comma on function calls, ESLint throws this error: Parsing error: Unexpected token ) . The "unexpected token" is the function's closing paren.

I have comma-dangle set to 'comma-dangle': ['error', 'always-multiline'] in my eslintrc file.

Why would this error get thrown?

Just to clarify, this is what a dangling comma would look like in a function call:

const result = parseInput(
  input,
  true,
  paramNames, // this is the line with the dangling comma
)

Dangling commas in function calls were added in ECMAScript 2017 (yay!). In order for ESLint to recognize this new feature, you have to specify the ecmaVersion in .eslintrc . ECMAScript 2017 corresponds to ecmaVersion 8. So the setting would look something like this:

"parserOptions": {
  "ecmaVersion": 8
}

FYI, you can use any ecmaVersion greater than 8 as well.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM