UNPKG

4.76 kBMarkdownView Raw
1# eslint-plugin-json
2
3[![npm](https://img.shields.io/npm/v/eslint-plugin-json.svg)](https://www.npmjs.com/package/eslint-plugin-json)
4[![Build Status](https://travis-ci.org/azeemba/eslint-plugin-json.svg)](https://travis-ci.org/azeemba/eslint-plugin-json)
5[![codecov](https://codecov.io/gh/azeemba/eslint-plugin-json/branch/master/graph/badge.svg)](https://codecov.io/gh/azeemba/eslint-plugin-json)
6[![dependencies Status](https://david-dm.org/azeemba/eslint-plugin-json/master/status.svg)](https://david-dm.org/adrieankhisbe/eslint-plugin-json/master)
7[![Code Climate](https://codeclimate.com/github/azeemba/eslint-plugin-json/badges/gpa.svg)](https://codeclimate.com/github/azeemba/eslint-plugin-json)
8
9> Eslint plugin for JSON files
10
11:warning: Starting from **major 2.0**, rules **need to be explicitly activated**.
12[See **here** the minimal config to add](#basic-configuration) :rotating_light:
13
14## Installation
15
16Install `eslint-plugin-json` along [`eslint`](http://eslint.org):
17
18```shell
19$ npm install --save-dev eslint eslint-plugin-json
20# or
21$ yarn add --dev eslint eslint-plugin-json
22```
23
24**Note:** If you installed ESLint globally (using the `-g` flag) then you must also install `eslint-plugin-json` globally.
25
26## Usage
27
28### Basic configuration
29
30The `json` plugin ship with two recommended config you can use to easily activate it via the `extends` key.
31It comes in two flavor: one strict (`recommended`) and one allowing comments `recommended-with-comments`.
32
33
34```json
35{
36 "extends": ["plugin:json/recommended"]
37}
38```
39
40You can run ESLint on individual JSON files or you can use the `--ext` flag to add JSON files to the list.
41
42```
43eslint . --ext .json,.js
44eslint example.json
45```
46
47### Custom Configuration
48
49If you want more granular control over which rules, and wich severity you want
50
51Add `json` to the list of plugins (You can omit the `eslint-plugin-` prefix)
52Then pick your rules.
53
54If you want them all, add the `json/json` rule (or its alias `json/*`). (this is what the `recommended` config does)
55
56#### Global rules
57The global rules (`json/json` or its alias `json/*`) activate all the rules.
58Note it can be configured to ignore errors cause by comments.
59To do so, add option `'allowComments'` or `{allowComments: true}`
60
61For instance:
62```json
63{
64 "plugins": [
65 "json"
66 ],
67 "rules": {
68 "json/*": ["error", "allowComments"],
69 // or the equivalent:
70 "json/*": ["error", {"allowComments": true}]
71 }
72}
73```
74
75#### Individual Rules
76Here is the list of individual rules (with name in `kebab-case`)in case you want granular error/warning level:
77- `json/undefined`
78- `json/enum-value-mismatch`
79- `json/unexpected-end-of-comment`
80- `json/unexpected-end-of-string`
81- `json/unexpected-end-of-number`
82- `json/invalid-unicode`
83- `json/invalid-escape-character`
84- `json/invalid-character`
85- `json/property-expected`
86- `json/comma-expected`
87- `json/colon-expected`
88- `json/value-expected`
89- `json/comma-or-close-backet-expected`
90- `json/comma-or-close-brace-expected`
91- `json/trailing-comma`
92- `json/duplicate-key`
93- `json/comment-not-permitted`
94- `json/schema-resolve-error`
95- `json/unknown` (error that does not match previous ones)
96
97## FAQs
98
99
100#### How does eslint-plugin-json work?
101
102Starting from version 1.3, this plugin relies on what [VSCode](https://github.com/Microsoft/vscode-json-languageservice)
103uses for its implementation of JSON validation.
104
105Originaly this plugin used to use JSHint, however due to heavy dependencies, it was replaced.
106
107#### Why doesn't this plugin use `eslint` itself or just `JSON.parse`?
108
109`eslint`'s parser is a JavaScript parser. JSON is a stricter subset and things
110that are valid JavaScript are not valid JSON. This is why something more specific
111is more appropriate.
112
113While `JSON.parse` seems ideal, it is not designed to continue after the first error.
114So if you have a missing trailing comma in the start of the file, the rest of the file
115will go unlinted. A smarter parser that can self-correct after seeing errors is needed
116which the VSCode implementation provides by leveraging the
117[jsonc-parser](https://www.npmjs.com/package/jsonc-parser) module.
118
119
120#### Will this plugin provide more configuration?
121
122It is now possible as you can see in the [Configuration section](#custom-configuration)
123
124Additionally, support for autofixing common errors could be added in the feature.
125
126#### Is `eslint` really the best tool to lint my JSON?
127
128Not really. `eslint` plugin interface wasn't designed to lint a completely different language but
129its interface is flexible enough to allow it. So this plugin is certainly unusual.
130
131Ideally, your editor would natively supports linting JSON. If it doesn't though, then might as well
132use this plugin. Hacky linting is better than no linting :)