File size: 3,428 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# @automattic/eslint-plugin-json

This ESLint plugin is a collection of plugins, rules and configurations that help lint JSON files, with special support for `package.json`

## Usage

Add an override in your ESLint config:

```js
module.exports = {
	//...
	overrides: [
		{
			files: [ '*.json' ],
			extends: [ 'plugin:@automattic/eslint-plugin-json/wpcom' ],
			rules: {
				// Custom rules
			},
		},
	],
};
```

There are two configs to extend from:

- `plugin:@automattic/eslint-plugin-json/recommended`: default set of settings. Includes

  - [`eslint-plugin-json-es` recommended rules](https://github.com/zeitport/eslint-plugin-json-es#recommended)
  - Disable `comma-dangle` and forbid comments
  - [`npm-package-json-lint` default rules](https://github.com/tclindner/npm-package-json-lint-config-default)

- `plugin:@automattic/eslint-plugin-json/wpcom`: rules from Gutenberg. Includes:
  - All of the above
  - [`@wordpress/npm-package-json-lint-config` rules](https://www.npmjs.com/package/@wordpress/npm-package-json-lint-config)

## Linting JSON files

Internally, we use the plugin [`eslint-plugin-json-es`](https://www.npmjs.com/package/eslint-plugin-json-es) which transforms JSON into valid JavaScript so it can be linted with regular ESLint rules. It includes a few [recommended rules](https://github.com/zeitport/eslint-plugin-json-es#recommended), which are enabled in our `recommended` config.

Our `recommended` config also disables `comma-dangle` and `json-es/no-comments`, as they are not valid in strict JSON. If you want to allow comments in some JSON files, we recommend this config:

```js
module.exports = {
	//...
	overrides: [
		{
			files: [ '*.json' ],
			extends: [ 'plugin:@automattic/eslint-plugin-json/wpcom' ],
			overrides: [
				{
					// These files are parsed as jsonc (JSON with Comments)
					// Customize the files array as needed
					files: [ 'tsconfig.json' ],
					rules: {
						'json-es/no-comments': 'off',
					},
				},
			],
		},
	],
};
```

Using that config, ESLint won't allow comments in `.json` files, except for `tsconfig.json`.

## Linting package.json

It is recommended to add another overrides within the `.json` overrides to configure `package.json` specific rules:

```js
module.exports = {
	//...
	overrides: [
		{
			files: [ '*.json' ],
			extends: [ 'plugin:@automattic/eslint-plugin-json/wpcom' ],
			rules: {
				// General rules for all .json files
			},
			overrides: [
				{
					files: [ 'package.json' ],
					rules: {
						// Specific rules for package.json files
					},
				},
			],
		},
	],
};
```

We have an adapter to use all rules from [`npm-package-json-lint`](https://npmpackagejsonlint.org/docs/en/rules) as ESLint rules. For example, the rule [`require-author`](https://npmpackagejsonlint.org/docs/en/rules/required-node/require-author) is exposed as the ESLint rule `@automattic/json/require-author`. You can use this mapping to enable, disable and configure rules as needed. Example:

```js
module.exports = {
	//...
	overrides: [
		{
			files: [ 'package.json' ],
			rules: {
				// Require bugs URL
				'@automattic/json/require-bugs': 'error',

				// Do not require author
				'@automattic/json/require-author': 'off',

				// Configure description format
				'@automattic/json/description-format': [
					'error',
					{
						requireCapitalFirstLetter: true,
						requireEndingPeriod: true,
					},
				],
			},
		},
	],
};
```