@eslint/json 0.12.0 → 0.13.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +70 -32
- package/dist/cjs/index.cjs +247 -104
- package/dist/cjs/index.d.cts +62 -41
- package/dist/esm/index.d.ts +62 -41
- package/dist/esm/index.js +247 -104
- package/package.json +22 -11
package/README.md
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
## Overview
|
|
4
4
|
|
|
5
|
-
This package contains a plugin that allows you to natively lint JSON and
|
|
5
|
+
This package contains a plugin that allows you to natively lint JSON, JSONC, and JSON5 files using ESLint.
|
|
6
6
|
|
|
7
7
|
**Important:** This plugin requires ESLint v9.6.0 or higher and you must be using the [new configuration system](https://eslint.org/docs/latest/use/configure/configuration-files).
|
|
8
8
|
|
|
@@ -17,7 +17,7 @@ yarn add @eslint/json -D
|
|
|
17
17
|
# or
|
|
18
18
|
pnpm install @eslint/json -D
|
|
19
19
|
# or
|
|
20
|
-
bun
|
|
20
|
+
bun add @eslint/json -D
|
|
21
21
|
```
|
|
22
22
|
|
|
23
23
|
For Deno:
|
|
@@ -31,15 +31,17 @@ deno add @eslint/json
|
|
|
31
31
|
This package exports these languages:
|
|
32
32
|
|
|
33
33
|
- `"json/json"` is for regular JSON files
|
|
34
|
-
- `"json/jsonc"` is for JSON files that support comments ([JSONC](https://github.com/microsoft/node-jsonc-parser)) such as those used for Visual Studio Code configuration files
|
|
34
|
+
- `"json/jsonc"` is for JSON files that support comments ([JSONC](https://github.com/microsoft/node-jsonc-parser)) such as those used for [Visual Studio Code](https://code.visualstudio.com/) configuration files
|
|
35
35
|
- `"json/json5"` is for [JSON5](https://json5.org) files
|
|
36
36
|
|
|
37
37
|
Depending on which types of JSON files you'd like to lint, you can set up your `eslint.config.js` file to include just the files you'd like. Here's an example that lints JSON, JSONC, and JSON5 files:
|
|
38
38
|
|
|
39
39
|
```js
|
|
40
|
+
// eslint.config.js
|
|
41
|
+
import { defineConfig } from "eslint/config";
|
|
40
42
|
import json from "@eslint/json";
|
|
41
43
|
|
|
42
|
-
export default [
|
|
44
|
+
export default defineConfig([
|
|
43
45
|
{
|
|
44
46
|
plugins: {
|
|
45
47
|
json,
|
|
@@ -72,15 +74,17 @@ export default [
|
|
|
72
74
|
"json/no-duplicate-keys": "error",
|
|
73
75
|
},
|
|
74
76
|
},
|
|
75
|
-
];
|
|
77
|
+
]);
|
|
76
78
|
```
|
|
77
79
|
|
|
78
80
|
In CommonJS format:
|
|
79
81
|
|
|
80
82
|
```js
|
|
83
|
+
// eslint.config.js
|
|
84
|
+
const { defineConfig } = require("eslint/config");
|
|
81
85
|
const json = require("@eslint/json").default;
|
|
82
86
|
|
|
83
|
-
module.exports = [
|
|
87
|
+
module.exports = defineConfig([
|
|
84
88
|
{
|
|
85
89
|
plugins: {
|
|
86
90
|
json,
|
|
@@ -113,56 +117,64 @@ module.exports = [
|
|
|
113
117
|
"json/no-duplicate-keys": "error",
|
|
114
118
|
},
|
|
115
119
|
},
|
|
116
|
-
];
|
|
120
|
+
]);
|
|
117
121
|
```
|
|
118
122
|
|
|
119
123
|
## Recommended Configuration
|
|
120
124
|
|
|
121
|
-
To use the recommended configuration for this plugin, specify your matching `files` and then use the `json
|
|
125
|
+
To use the recommended configuration for this plugin, specify your matching `files` and then use the `extends: ["json/recommended"]` property, like this:
|
|
122
126
|
|
|
123
127
|
```js
|
|
128
|
+
// eslint.config.js
|
|
129
|
+
import { defineConfig } from "eslint/config";
|
|
124
130
|
import json from "@eslint/json";
|
|
125
131
|
|
|
126
|
-
export default [
|
|
132
|
+
export default defineConfig([
|
|
127
133
|
// lint JSON files
|
|
128
134
|
{
|
|
129
135
|
files: ["**/*.json"],
|
|
130
136
|
ignores: ["package-lock.json"],
|
|
137
|
+
plugins: { json },
|
|
131
138
|
language: "json/json",
|
|
132
|
-
|
|
139
|
+
extends: ["json/recommended"],
|
|
133
140
|
},
|
|
134
141
|
|
|
135
142
|
// lint JSONC files
|
|
136
143
|
{
|
|
137
144
|
files: ["**/*.jsonc"],
|
|
145
|
+
plugins: { json },
|
|
138
146
|
language: "json/jsonc",
|
|
139
|
-
|
|
147
|
+
extends: ["json/recommended"],
|
|
140
148
|
},
|
|
141
149
|
|
|
142
150
|
// lint JSON5 files
|
|
143
151
|
{
|
|
144
152
|
files: ["**/*.json5"],
|
|
153
|
+
plugins: { json },
|
|
145
154
|
language: "json/json5",
|
|
146
|
-
|
|
155
|
+
extends: ["json/recommended"],
|
|
147
156
|
},
|
|
148
|
-
];
|
|
157
|
+
]);
|
|
149
158
|
```
|
|
150
159
|
|
|
151
160
|
**Note:** You generally want to ignore `package-lock.json` because it is auto-generated and you typically will not want to manually make changes to it.
|
|
152
161
|
|
|
153
162
|
## Rules
|
|
154
163
|
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
164
|
+
<!-- NOTE: The following table is autogenerated. Do not manually edit. -->
|
|
165
|
+
|
|
166
|
+
<!-- Rule Table Start -->
|
|
167
|
+
|
|
168
|
+
| **Rule Name** | **Description** | **Recommended** |
|
|
169
|
+
| :------------------------------------------------------------- | :-------------------------------------------------------- | :-------------: |
|
|
170
|
+
| [`no-duplicate-keys`](./docs/rules/no-duplicate-keys.md) | Disallow duplicate keys in JSON objects | yes |
|
|
171
|
+
| [`no-empty-keys`](./docs/rules/no-empty-keys.md) | Disallow empty keys in JSON objects | yes |
|
|
172
|
+
| [`no-unnormalized-keys`](./docs/rules/no-unnormalized-keys.md) | Disallow JSON keys that are not normalized | yes |
|
|
173
|
+
| [`no-unsafe-values`](./docs/rules/no-unsafe-values.md) | Disallow JSON values that are unsafe for interchange | yes |
|
|
174
|
+
| [`sort-keys`](./docs/rules/sort-keys.md) | Require JSON object keys to be sorted | no |
|
|
175
|
+
| [`top-level-interop`](./docs/rules/top-level-interop.md) | Require the JSON top-level value to be an array or object | no |
|
|
176
|
+
|
|
177
|
+
<!-- Rule Table End -->
|
|
166
178
|
|
|
167
179
|
## Configuration Comments
|
|
168
180
|
|
|
@@ -199,26 +211,30 @@ Both line and block comments can be used for all kinds of configuration comments
|
|
|
199
211
|
The Microsoft implementation of JSONC optionally allows for trailing commas in objects and arrays (files like `tsconfig.json` have this option enabled by default in Visual Studio Code). To enable trailing commas in JSONC files, use the `allowTrailingCommas` language option, as in this example:
|
|
200
212
|
|
|
201
213
|
```js
|
|
214
|
+
// eslint.config.js
|
|
215
|
+
import { defineConfig } from "eslint/config";
|
|
202
216
|
import json from "@eslint/json";
|
|
203
217
|
|
|
204
|
-
export default [
|
|
218
|
+
export default defineConfig([
|
|
205
219
|
// lint JSONC files
|
|
206
220
|
{
|
|
207
221
|
files: ["**/*.jsonc"],
|
|
222
|
+
plugins: { json },
|
|
208
223
|
language: "json/jsonc",
|
|
209
|
-
|
|
224
|
+
extends: ["json/recommended"],
|
|
210
225
|
},
|
|
211
226
|
|
|
212
227
|
// lint JSONC files and allow trailing commas
|
|
213
228
|
{
|
|
214
229
|
files: ["**/tsconfig.json", ".vscode/*.json"],
|
|
230
|
+
plugins: { json },
|
|
215
231
|
language: "json/jsonc",
|
|
216
232
|
languageOptions: {
|
|
217
233
|
allowTrailingCommas: true,
|
|
218
234
|
},
|
|
219
|
-
|
|
235
|
+
extends: ["json/recommended"],
|
|
220
236
|
},
|
|
221
|
-
];
|
|
237
|
+
]);
|
|
222
238
|
```
|
|
223
239
|
|
|
224
240
|
**Note:** The `allowTrailingCommas` option is only valid for the `json/jsonc` language.
|
|
@@ -229,8 +245,8 @@ export default [
|
|
|
229
245
|
|
|
230
246
|
This plugin implements JSON parsing for ESLint using the language plugins API, which is the official way of supporting non-JavaScript languages in ESLint. This differs from the other plugins:
|
|
231
247
|
|
|
232
|
-
- `eslint-plugin-json` uses a processor to parse the JSON, meaning it doesn't create an AST and you can't write custom rules for it.
|
|
233
|
-
- `eslint-plugin-jsonc` uses a parser that still goes through the JavaScript linting functionality and requires several rules to disallow valid JavaScript syntax that is invalid in JSON.
|
|
248
|
+
- [`eslint-plugin-json`](https://github.com/azeemba/eslint-plugin-json) uses a processor to parse the JSON, meaning it doesn't create an AST and you can't write custom rules for it.
|
|
249
|
+
- [`eslint-plugin-jsonc`](https://github.com/ota-meshi/eslint-plugin-jsonc) uses a parser that still goes through the JavaScript linting functionality and requires several rules to disallow valid JavaScript syntax that is invalid in JSON.
|
|
234
250
|
|
|
235
251
|
As such, this plugin is more robust and faster than the others. You can write your own custom rules when using the languages in this plugin, too.
|
|
236
252
|
|
|
@@ -242,6 +258,28 @@ Similarly, many of the rules in `eslint-plugin-jsonc` specifically disallow vali
|
|
|
242
258
|
|
|
243
259
|
Any other rules that catch potential problems in JSON are welcome to be implemented. You can [open an issue](https://github.com/eslint/json/issues/new/choose) to propose a new rule.
|
|
244
260
|
|
|
261
|
+
## Editor and IDE Setup
|
|
262
|
+
|
|
263
|
+
### Visual Studio Code
|
|
264
|
+
|
|
265
|
+
First, ensure you have the [ESLint plugin](https://marketplace.visualstudio.com/items?itemName=dbaeumer.vscode-eslint) installed.
|
|
266
|
+
|
|
267
|
+
Then, edit `eslint.validate` in your `settings.json` file to include `json`, `jsonc`, and `json5`:
|
|
268
|
+
|
|
269
|
+
```json
|
|
270
|
+
{
|
|
271
|
+
"eslint.validate": ["json", "jsonc", "json5"]
|
|
272
|
+
}
|
|
273
|
+
```
|
|
274
|
+
|
|
275
|
+
### JetBrains WebStorm
|
|
276
|
+
|
|
277
|
+
For any [JetBrains WebStorm](https://www.jetbrains.com/webstorm/), configure the [ESLint scope](https://www.jetbrains.com/help/webstorm/eslint.html#ws_eslint_configure_scope) to include `json`, `jsonc`, and `json5`, such as:
|
|
278
|
+
|
|
279
|
+
```text
|
|
280
|
+
**/*.{js,ts,jsx,tsx,cjs,cts,mjs,mts,html,vue,json,jsonc,json5}
|
|
281
|
+
```
|
|
282
|
+
|
|
245
283
|
## License
|
|
246
284
|
|
|
247
285
|
Apache 2.0
|
|
@@ -255,11 +293,11 @@ The following companies, organizations, and individuals support ESLint's ongoing
|
|
|
255
293
|
to get your logo on our READMEs and [website](https://eslint.org/sponsors).
|
|
256
294
|
|
|
257
295
|
<h3>Diamond Sponsors</h3>
|
|
258
|
-
<p><a href="https://www.ag-grid.com/"><img src="https://images.opencollective.com/ag-grid/
|
|
296
|
+
<p><a href="https://www.ag-grid.com/"><img src="https://images.opencollective.com/ag-grid/bec0580/logo.png" alt="AG Grid" height="128"></a></p><h3>Platinum Sponsors</h3>
|
|
259
297
|
<p><a href="https://automattic.com"><img src="https://images.opencollective.com/automattic/d0ef3e1/logo.png" alt="Automattic" height="128"></a> <a href="https://www.airbnb.com/"><img src="https://images.opencollective.com/airbnb/d327d66/logo.png" alt="Airbnb" height="128"></a></p><h3>Gold Sponsors</h3>
|
|
260
298
|
<p><a href="https://qlty.sh/"><img src="https://images.opencollective.com/qltysh/33d157d/logo.png" alt="Qlty Software" height="96"></a> <a href="https://trunk.io/"><img src="https://images.opencollective.com/trunkio/fb92d60/avatar.png" alt="trunk.io" height="96"></a> <a href="https://shopify.engineering/"><img src="https://avatars.githubusercontent.com/u/8085" alt="Shopify" height="96"></a></p><h3>Silver Sponsors</h3>
|
|
261
299
|
<p><a href="https://vite.dev/"><img src="https://images.opencollective.com/vite/e6d15e1/logo.png" alt="Vite" height="64"></a> <a href="https://liftoff.io/"><img src="https://images.opencollective.com/liftoff/5c4fa84/logo.png" alt="Liftoff" height="64"></a> <a href="https://americanexpress.io"><img src="https://avatars.githubusercontent.com/u/3853301" alt="American Express" height="64"></a> <a href="https://stackblitz.com"><img src="https://avatars.githubusercontent.com/u/28635252" alt="StackBlitz" height="64"></a></p><h3>Bronze Sponsors</h3>
|
|
262
|
-
<p><a href="https://cybozu.co.jp/"><img src="https://images.opencollective.com/cybozu/933e46d/logo.png" alt="Cybozu" height="32"></a> <a href="https://
|
|
300
|
+
<p><a href="https://cybozu.co.jp/"><img src="https://images.opencollective.com/cybozu/933e46d/logo.png" alt="Cybozu" height="32"></a> <a href="https://sentry.io"><img src="https://github.com/getsentry.png" alt="Sentry" height="32"></a> <a href="https://www.crosswordsolver.org/anagram-solver/"><img src="https://images.opencollective.com/anagram-solver/2666271/logo.png" alt="Anagram Solver" height="32"></a> <a href="https://icons8.com/"><img src="https://images.opencollective.com/icons8/7fa1641/logo.png" alt="Icons8" height="32"></a> <a href="https://discord.com"><img src="https://images.opencollective.com/discordapp/f9645d9/logo.png" alt="Discord" height="32"></a> <a href="https://www.gitbook.com"><img src="https://avatars.githubusercontent.com/u/7111340" alt="GitBook" height="32"></a> <a href="https://nx.dev"><img src="https://avatars.githubusercontent.com/u/23692104" alt="Nx" height="32"></a> <a href="https://opensource.mercedes-benz.com/"><img src="https://avatars.githubusercontent.com/u/34240465" alt="Mercedes-Benz Group" height="32"></a> <a href="https://herocoders.com"><img src="https://avatars.githubusercontent.com/u/37549774" alt="HeroCoders" height="32"></a> <a href="https://www.lambdatest.com"><img src="https://avatars.githubusercontent.com/u/171592363" alt="LambdaTest" height="32"></a></p>
|
|
263
301
|
<h3>Technology Sponsors</h3>
|
|
264
302
|
Technology sponsors allow us to use their products and services for free as part of a contribution to the open source ecosystem and our work.
|
|
265
303
|
<p><a href="https://netlify.com"><img src="https://raw.githubusercontent.com/eslint/eslint.org/main/src/assets/images/techsponsors/netlify-icon.svg" alt="Netlify" height="32"></a> <a href="https://algolia.com"><img src="https://raw.githubusercontent.com/eslint/eslint.org/main/src/assets/images/techsponsors/algolia-icon.svg" alt="Algolia" height="32"></a> <a href="https://1password.com"><img src="https://raw.githubusercontent.com/eslint/eslint.org/main/src/assets/images/techsponsors/1password-icon.svg" alt="1Password" height="32"></a></p>
|