1 | # eslint-plugin-json
|
2 |
|
3 | [![Build Status](https://travis-ci.org/azeemba/eslint-plugin-json.svg)](https://travis-ci.org/azeemba/eslint-plugin-json) [![Code Climate](https://codeclimate.com/github/azeemba/eslint-plugin-json/badges/gpa.svg)](https://codeclimate.com/github/azeemba/eslint-plugin-json)
|
4 |
|
5 | Lint JSON files
|
6 |
|
7 | ## Installation
|
8 |
|
9 | You'll first need to install [ESLint](http://eslint.org):
|
10 |
|
11 | ```
|
12 | $ npm i eslint --save-dev
|
13 | ```
|
14 |
|
15 | Next, install `eslint-plugin-json`:
|
16 |
|
17 | ```
|
18 | $ npm install eslint-plugin-json --save-dev
|
19 | ```
|
20 |
|
21 | **Note:** If you installed ESLint globally (using the `-g` flag) then you must also install `eslint-plugin-json` globally.
|
22 |
|
23 | ## Usage
|
24 |
|
25 | Add `json` to the plugins section of your `.eslintrc` configuration file. You can omit the `eslint-plugin-` prefix:
|
26 |
|
27 | ```json
|
28 | {
|
29 | "plugins": [
|
30 | "json"
|
31 | ]
|
32 | }
|
33 | ```
|
34 |
|
35 | You can run ESLint on individual JSON files or you can use the `--ext` flag to add JSON files to the list.
|
36 |
|
37 | ```
|
38 | eslint . --ext .json --ext .js
|
39 | eslint example.json
|
40 | ```
|
41 |
|
42 | ## FAQs
|
43 |
|
44 |
|
45 | #### How does eslint-plugin-json work?
|
46 |
|
47 | Starting from version 1.3, this plugin relies on what [VSCode](https://github.com/Microsoft/vscode-json-languageservice)
|
48 | uses for its implementation of JSON validation.
|
49 | This plugin used to use JSHint, however due to the large size of
|
50 | this dependency, it was replaced.
|
51 |
|
52 |
|
53 | #### Why doesn't this plugin use `eslint` itself or just `JSON.parse`?
|
54 |
|
55 | `eslint`'s parser is a JavaScript parser. JSON is a stricter subset and things
|
56 | that are valid JavaScript are not valid JSON. This is why something more specific
|
57 | is more appropriate.
|
58 |
|
59 | While `JSON.parse` seems ideal, it is not designed to continue after the first error.
|
60 | So if you have a missing trailing comma in the start of the file, the rest of the file
|
61 | will go unlinted. A smarter parser that can self-correct after seeing errors is needed
|
62 | which the VSCode implementation provides by leveraging the
|
63 | [jsonc-parser](https://www.npmjs.com/package/jsonc-parser) module.
|
64 |
|
65 |
|
66 | #### Will this plugin provide more configuration?
|
67 |
|
68 | Now that we have moved to a different implementation for our validation, a lot
|
69 | more things are possible. Optional support for JSON comments, trailing commas
|
70 | and schemas are possible.
|
71 |
|
72 | Additionally, support for autofixing common errors is also possible.
|
73 |
|
74 |
|
75 | #### Is `eslint` really the best tool to lint my JSON?
|
76 |
|
77 | Not really. `eslint` plugin interface wasn't designed to lint a completely different language but
|
78 | its interface is flexible enough to allow it. So this plugin is certainly unusual.
|
79 |
|
80 | Ideally, your editor would natively supports linting JSON. If it doesn't though, then might as well
|
81 | use this plugin. Hacky linting is better than no linting :) |
\ | No newline at end of file |