UNPKG

2.66 kBMarkdownView Raw
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
5Lint JSON files
6
7## Installation
8
9You'll first need to install [ESLint](http://eslint.org):
10
11```
12$ npm i eslint --save-dev
13```
14
15Next, 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
25Add `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
35You can run ESLint on individual JSON files or you can use the `--ext` flag to add JSON files to the list.
36
37```
38eslint . --ext .json --ext .js
39eslint example.json
40```
41
42## FAQs
43
44
45#### How does eslint-plugin-json work?
46
47Starting from version 1.3, this plugin relies on what [VSCode](https://github.com/Microsoft/vscode-json-languageservice)
48uses for its implementation of JSON validation.
49This plugin used to use JSHint, however due to the large size of
50this 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
56that are valid JavaScript are not valid JSON. This is why something more specific
57is more appropriate.
58
59While `JSON.parse` seems ideal, it is not designed to continue after the first error.
60So if you have a missing trailing comma in the start of the file, the rest of the file
61will go unlinted. A smarter parser that can self-correct after seeing errors is needed
62which 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
68Now that we have moved to a different implementation for our validation, a lot
69more things are possible. Optional support for JSON comments, trailing commas
70and schemas are possible.
71
72Additionally, support for autofixing common errors is also possible.
73
74
75#### Is `eslint` really the best tool to lint my JSON?
76
77Not really. `eslint` plugin interface wasn't designed to lint a completely different language but
78its interface is flexible enough to allow it. So this plugin is certainly unusual.
79
80Ideally, your editor would natively supports linting JSON. If it doesn't though, then might as well
81use this plugin. Hacky linting is better than no linting :)
\No newline at end of file