简体   繁体   中英

How can I make eslint detect rule violations in JSON files?

Below you see my .eslintrc file, which contains the rule that needs to be applied to a .json file.

{
  "extends": "eslint:recommended",
  "parser": "babel-eslint",
  "env": {
    "browser": true,
    "node": true,
    "jquery": true,
    "jasmine": true
  },
  "plugins": [
    "json"
  ],
  "rules": {
    "camelcase": 2
  }

The rule works literally everywhere in the project, except the .json files.

I have tried to use the "eslint-plugin-json" and couple of other modules, but they simply just don't work.

Please don't give solutions such as "use jsonlint instead, or etc.", the module for linting in the project is eslint.

If I understand correctly, JSON files have not valid JavaScript syntax in most cases: outer curly brackets {} are parsed as a block with statements and parsing error Unexpected token : happens on the first key: value pair. So I am not sure if this can be achieved with ESLint (ESLint checks JavaScript per definition).

You can try something like echo "(`cat test.json`)" | eslint --stdin echo "(`cat test.json`)" | eslint --stdin though.

However, camelcase particularly cannot be applied here: ESlint seems to ignore quoted keys and you cannot use unquoted keys in JSON.

I made an alternative to eslint-plugin-json since it didn't work for me and didn't have auto-fixing - eslint-plugin-json-format

It will also (optionally) sort your package.json for you

eslint --ext .json,.js . --fix

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