Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unexpected token errors during editing #130

Open
jerone opened this issue Aug 19, 2022 · 0 comments
Open

Unexpected token errors during editing #130

jerone opened this issue Aug 19, 2022 · 0 comments

Comments

@jerone
Copy link

jerone commented Aug 19, 2022

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant