Skip to content

Unexpected token errors during editing #130

@jerone

Description

@jerone

When editing the package.json file, I'm getting the following errors within VSCode:
image

Error
[Error - 17:00:33] ESLint stack trace:
[Error - 17:00:33] SyntaxError: Unexpected token } in JSON at position 1279
Occurred while linting C:\Sources\eslint-formatter-checklist\package.json:1
Rule: "json-files/sort-package-json"
    at JSON.parse (<anonymous>)
    at editStringJSON (C:\Sources\eslint-formatter-checklist\node_modules\sort-package-json\index.js:327:17)
    at sortPackageJson (C:\Sources\eslint-formatter-checklist\node_modules\sort-package-json\index.js:349:10)
    at AssignmentExpression (C:\Sources\eslint-formatter-checklist\node_modules\eslint-plugin-json-files\lib\rules\sort-package-json.js:28:26)
    at ruleErrorHandler (C:\Sources\eslint-formatter-checklist\node_modules\eslint\lib\linter\linter.js:1114:28)
    at C:\Sources\eslint-formatter-checklist\node_modules\eslint\lib\linter\safe-emitter.js:45:58
    at Array.forEach (<anonymous>)
    at Object.emit (C:\Sources\eslint-formatter-checklist\node_modules\eslint\lib\linter\safe-emitter.js:45:38)
    at NodeEventGenerator.applySelector (C:\Sources\eslint-formatter-checklist\node_modules\eslint\lib\linter\node-event-generator.js:297:26)
    at NodeEventGenerator.applySelectors (C:\Sources\eslint-formatter-checklist\node_modules\eslint\lib\linter\node-event-generator.js:326:22)

This happens in VSCode (v1.70.2) with ESLint extension (v2.2.6) installed, and the following configurations:

.vscode/settings.json
{
  "editor.codeActionsOnSave": {
    "source.fixAll": true
  },
  "eslint.validate": ["json", "jsonc"]
}
ESLint configuration
{
  "root": true,
  "overrides": [
    {
      "files": ["package.json"],
      "plugins": ["json-files"],
      "rules": {
        "json-files/sort-package-json": "error"
      }
    }
  ]
}

(Removed some unneeded configurations)

Current situation

The reason this happens is when I edit an package.json file (e.g. adding another property), the JSON files becomes in a temporary invalid state. This plugin tries to parse the invalid JSON for the sort-package-json-rule and throws an exception.

Expected situation

The solution should be to put the JSON parsing in an try-catch and swallow any exceptions until the JSON is valid.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions