eslint / json

JSON language plugin for ESLint
Apache License 2.0
44 stars 4 forks source link

Bug: Trailing comma reported as invalid token #58

Closed susnux closed 1 week ago

susnux commented 1 week ago

Environment

ESLint version: 9.14.0 @eslint/json version: 0.6.0 Node version: 22 npm version: 10 Operating System: Linux

Which language are you using?

json

What did you do?

Configuration ``` { files: '**/*.json', language: 'json/json', plugins: { json: jsonPlugin as ESLint.Plugin, }, rules: { 'json/no-duplicate-keys': 'error', 'json/no-empty-keys': 'error', }, }, ```
{
  "$schema": "https://json.schemastore.org/tsconfig",
  "compilerOptions": {
    "allowImportingTsExtensions": true,
    "rootDir": "lib/",
  }
}

What did you expect to happen?

An error that points you to the invalid trailing comma.

What actually happened?

Parsing error: Unexpected token RBrace found.

Link to Minimal Reproducible Example

stackblitz does not work

Participation

Additional comments

No response

fasttime commented 1 week ago

Thanks for issue @susnux. Note that the files property in a config object should an array rather than a string, so I replaced

files: '**/*.json',

with

files: ['**/*.json'],

With that change, when I run ESLint with your config, I'm getting a syntax error on the closing brace (}) that follows the line with "rootDir": "lib/",:

.../tsconfig.json
  6:3  error  Parsing error: Unexpected token RBrace found

✖ 1 problem (1 error, 0 warnings)

I believe this is expected: the trailing comma per se would be fine if another property were declared after that line. The problem is actually the closing brace which makes the input invalid JSON.

Certain files like tsconfig.json are not pure JSON but rather JSONC, or JSON with comments (and possibly trailing commas). If you need to lint those files, be sure to set the language to json/jsonc and to enable the language option allowTrailingCommas:

{
    files: ['**/*.json'],
    language: 'json/jsonc',
    languageOptions: {
        allowTrailingCommas: true,
    },
    // ...
},

This is described in the documentation for this plugin, see: https://github.com/eslint/json?tab=readme-ov-file#allowing-trailing-commas-in-jsonc.

susnux commented 1 week ago

Makes sense, thank you for the feedback!