最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How can I make eslint detect rule violations in JSON files? - Stack Overflow

programmeradmin1浏览0评论

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

{
  "extends": "eslint:remended",
  "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.

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

{
  "extends": "eslint:remended",
  "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.

Share Improve this question edited Feb 15, 2019 at 14:24 Sth asked Feb 15, 2019 at 13:30 SthSth 5421 gold badge10 silver badges22 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 3

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 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
发布评论

评论列表(0)

  1. 暂无评论