eslint-config-webpack 4.9.5 → 4.10.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 +49 -2
- package/configs/javascript.js +1 -1
- package/configs/node.js +48 -4
- package/configs/utils/get-es-version-from-node.js +83 -0
- package/configs/webpack-special.js +4 -0
- package/configs.js +49 -65
- package/package.json +16 -16
- package/plugins/webpack/index.js +21 -1
- package/plugins/webpack/rules/valid-schema.js +370 -0
package/README.md
CHANGED
|
@@ -33,11 +33,58 @@ import config from "eslint-config-webpack";
|
|
|
33
33
|
|
|
34
34
|
export default defineConfig([
|
|
35
35
|
{
|
|
36
|
-
extends: [config]
|
|
37
|
-
}
|
|
36
|
+
extends: [config],
|
|
37
|
+
},
|
|
38
38
|
]);
|
|
39
39
|
```
|
|
40
40
|
|
|
41
|
+
### Webpack-specific configs
|
|
42
|
+
|
|
43
|
+
Two opt-in configs cover conventions only webpack's own repositories need. They
|
|
44
|
+
are not part of `recommended` — extend them explicitly:
|
|
45
|
+
|
|
46
|
+
```js
|
|
47
|
+
import { defineConfig } from "eslint/config";
|
|
48
|
+
import config from "eslint-config-webpack";
|
|
49
|
+
import configs from "eslint-config-webpack/configs.js";
|
|
50
|
+
|
|
51
|
+
export default defineConfig([
|
|
52
|
+
{
|
|
53
|
+
extends: [config, configs["webpack/special"], configs["webpack/schemas"]],
|
|
54
|
+
},
|
|
55
|
+
]);
|
|
56
|
+
```
|
|
57
|
+
|
|
58
|
+
| Config | Files | What it checks |
|
|
59
|
+
| ----------------- | ---------------------- | ------------------------------------------------------------------------------------------------------------ |
|
|
60
|
+
| `webpack/special` | source files | `webpack/require-license-comment` — every file opens with the MIT license header. |
|
|
61
|
+
| `webpack/schemas` | `**/schemas/**/*.json` | `webpack/valid-schema` — the JSON schema conventions webpack's declaration and validator generators rely on. |
|
|
62
|
+
|
|
63
|
+
`webpack/valid-schema` accepts options for which keywords a schema may use.
|
|
64
|
+
`keywords` replaces the default set — webpack's own — outright, `allow` adds to
|
|
65
|
+
it and `disallow` removes from it:
|
|
66
|
+
|
|
67
|
+
```js
|
|
68
|
+
export default defineConfig([
|
|
69
|
+
{
|
|
70
|
+
extends: [configs["webpack/schemas"]],
|
|
71
|
+
rules: {
|
|
72
|
+
"webpack/valid-schema": [
|
|
73
|
+
"error",
|
|
74
|
+
{ allow: ["x-generator"], disallow: ["cli"] },
|
|
75
|
+
],
|
|
76
|
+
},
|
|
77
|
+
},
|
|
78
|
+
]);
|
|
79
|
+
```
|
|
80
|
+
|
|
81
|
+
Every other check is unconditional. The rule reports a schema that uses a
|
|
82
|
+
keyword outside that set, puts anything next to a `$ref`, gives `type` more than one
|
|
83
|
+
value, uses `instanceof` without `tsType`, `absolutePath` off a string or
|
|
84
|
+
`properties` off a non-object, describes `properties` without
|
|
85
|
+
`additionalProperties`, nests or mis-sizes `oneOf`/`anyOf`/`allOf`, or leaves a
|
|
86
|
+
property without a description starting in uppercase and ending in a single dot.
|
|
87
|
+
|
|
41
88
|
[npm]: https://img.shields.io/npm/v/eslint-config-webpack.svg
|
|
42
89
|
[npm-url]: https://npmjs.com/package/eslint-config-webpack
|
|
43
90
|
[test]: https://github.com/webpack/eslint-config-webpack/actions/workflows/test.yml/badge.svg
|
package/configs/javascript.js
CHANGED
package/configs/node.js
CHANGED
|
@@ -73,8 +73,7 @@ const commonRules = {
|
|
|
73
73
|
// No need
|
|
74
74
|
// "n/no-top-level-await": "error",
|
|
75
75
|
|
|
76
|
-
|
|
77
|
-
// "n/no-unpublished-bin": "error",
|
|
76
|
+
"n/no-unpublished-bin": "error",
|
|
78
77
|
|
|
79
78
|
// From recommended
|
|
80
79
|
// "n/no-unpublished-import": "error",
|
|
@@ -95,8 +94,53 @@ const commonRules = {
|
|
|
95
94
|
|
|
96
95
|
// "n/no-unsupported-features/es-syntax": "error",
|
|
97
96
|
|
|
98
|
-
// From recommended
|
|
99
|
-
|
|
97
|
+
// From recommended (override to ignore test module)
|
|
98
|
+
"n/no-unsupported-features/node-builtins": [
|
|
99
|
+
"error",
|
|
100
|
+
{
|
|
101
|
+
ignores: [
|
|
102
|
+
"test",
|
|
103
|
+
"test.after",
|
|
104
|
+
"test.afterEach",
|
|
105
|
+
"test.assert",
|
|
106
|
+
"test.assert.register",
|
|
107
|
+
"test.before",
|
|
108
|
+
"test.beforeEach",
|
|
109
|
+
"test.describe",
|
|
110
|
+
"test.describe.only",
|
|
111
|
+
"test.describe.skip",
|
|
112
|
+
"test.describe.todo",
|
|
113
|
+
"test.it",
|
|
114
|
+
"test.it.only",
|
|
115
|
+
"test.it.skip",
|
|
116
|
+
"test.it.todo",
|
|
117
|
+
"test.mock",
|
|
118
|
+
"test.mock.fn",
|
|
119
|
+
"test.mock.getter",
|
|
120
|
+
"test.mock.method",
|
|
121
|
+
"test.mock.module",
|
|
122
|
+
"test.mock.reset",
|
|
123
|
+
"test.mock.restoreAll",
|
|
124
|
+
"test.mock.setter",
|
|
125
|
+
"test.mock.timers",
|
|
126
|
+
"test.mock.timers.enable",
|
|
127
|
+
"test.mock.timers.reset",
|
|
128
|
+
"test.mock.timers.tick",
|
|
129
|
+
"test.only",
|
|
130
|
+
"test.run",
|
|
131
|
+
"test.snapshot",
|
|
132
|
+
"test.snapshot.setDefaultSnapshotSerializers",
|
|
133
|
+
"test.snapshot.setResolveSnapshotPath",
|
|
134
|
+
"test.skip",
|
|
135
|
+
"test.suite",
|
|
136
|
+
"test.test",
|
|
137
|
+
"test.test.only",
|
|
138
|
+
"test.test.skip",
|
|
139
|
+
"test.test.todo",
|
|
140
|
+
"test.todo",
|
|
141
|
+
],
|
|
142
|
+
},
|
|
143
|
+
],
|
|
100
144
|
|
|
101
145
|
"n/prefer-global/buffer": ["error", "always"],
|
|
102
146
|
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
import semver from "semver";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* @typedef {2016 | 2017 | 2018 | 2019 | 2020 | 2021 | 2022 | 2023 | 2024 | 2025} EsVersion
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Ordered table from Node version ranges to the highest ES version every
|
|
9
|
+
* release in the range fully supports.
|
|
10
|
+
*
|
|
11
|
+
* The table respects minor versions where they shift the supported feature
|
|
12
|
+
* set, e.g. async/await landed in Node 7.6 (ES2017), and `Object.hasOwn`
|
|
13
|
+
* landed in Node 16.11 (ES2022) — the last ES2022 feature to ship on the
|
|
14
|
+
* Node 16 line.
|
|
15
|
+
*
|
|
16
|
+
* Sources: https://node.green/ and the Node.js/V8 release notes. Entries
|
|
17
|
+
* are sorted newest-to-oldest; the first matching entry wins.
|
|
18
|
+
* @type {ReadonlyArray<{ range: string, esVersion: EsVersion }>}
|
|
19
|
+
*/
|
|
20
|
+
const ranges = [
|
|
21
|
+
// ES2025: Node 22.0 ships all ES2025 syntax — import attributes
|
|
22
|
+
// (`with { type: "json" }`), the RegExp `/v` flag, and duplicate named
|
|
23
|
+
// capturing groups. Stdlib gaps (e.g. Promise.try lands at 22.5) are
|
|
24
|
+
// caught by eslint-plugin-n's `no-unsupported-features/node-builtins`,
|
|
25
|
+
// so we don't need a separate ES2024 row here.
|
|
26
|
+
{ range: ">=22.0.0", esVersion: 2025 },
|
|
27
|
+
// ES2023: Array-by-copy (toSorted/toReversed/toSpliced/with), findLast/findLastIndex.
|
|
28
|
+
{ range: ">=20.0.0", esVersion: 2023 },
|
|
29
|
+
// ES2022: Object.hasOwn is the last ES2022 feature to land on the Node 16 line.
|
|
30
|
+
// Earlier 16.x has top-level await (>=14.8), `.at()` (>=16.6) and Error cause (>=16.9)
|
|
31
|
+
// but not Object.hasOwn, so it can't safely claim full ES2022.
|
|
32
|
+
{ range: ">=16.11.0", esVersion: 2022 },
|
|
33
|
+
// ES2021: logical assignment operators, String#replaceAll, Promise.any, WeakRef.
|
|
34
|
+
{ range: ">=15.0.0", esVersion: 2021 },
|
|
35
|
+
// ES2020: optional chaining, nullish coalescing, BigInt, Promise.allSettled, globalThis.
|
|
36
|
+
{ range: ">=14.0.0", esVersion: 2020 },
|
|
37
|
+
// ES2019: Object.fromEntries, Array#flat/flatMap, String#trimStart/trimEnd.
|
|
38
|
+
{ range: ">=12.0.0", esVersion: 2019 },
|
|
39
|
+
// ES2018: object rest/spread, async iteration, RegExp lookbehind, Promise#finally.
|
|
40
|
+
{ range: ">=10.0.0", esVersion: 2018 },
|
|
41
|
+
// ES2017: async/await is the gating feature — landed in 7.6, not 7.0.
|
|
42
|
+
{ range: ">=7.6.0", esVersion: 2017 },
|
|
43
|
+
// ES2016: ** exponentiation operator and Array#includes (both require >=7.0).
|
|
44
|
+
{ range: ">=7.0.0", esVersion: 2016 },
|
|
45
|
+
];
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* Resolves the highest ES version every Node release matched by
|
|
49
|
+
* `nodeRange` fully supports.
|
|
50
|
+
*
|
|
51
|
+
* Returns `undefined` when the lower bound is older than our oldest entry
|
|
52
|
+
* (Node 7.0), so callers can apply project-specific fallbacks — for example
|
|
53
|
+
* Node 6, which has `Array#includes` (from 6.5) but never gained the `**`
|
|
54
|
+
* operator and so doesn't map cleanly to a single ES year.
|
|
55
|
+
* @param {string} nodeRange a semver range, typically `package.json#engines.node`
|
|
56
|
+
* @returns {EsVersion | undefined} matching ES version, or `undefined`
|
|
57
|
+
*/
|
|
58
|
+
function getEsVersionFromNode(nodeRange) {
|
|
59
|
+
// `semver.minVersion` throws on unparseable input. Guard against a
|
|
60
|
+
// malformed `engines.node` so loading the ESLint config never crashes
|
|
61
|
+
// the consuming project — we just fall back to the caller's default.
|
|
62
|
+
let minVersion;
|
|
63
|
+
try {
|
|
64
|
+
minVersion = semver.minVersion(nodeRange);
|
|
65
|
+
// eslint-disable-next-line unicorn/prefer-optional-catch-binding
|
|
66
|
+
} catch (_err) {
|
|
67
|
+
return undefined;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
if (!minVersion) {
|
|
71
|
+
return undefined;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
for (const { range, esVersion } of ranges) {
|
|
75
|
+
if (semver.satisfies(minVersion, range)) {
|
|
76
|
+
return esVersion;
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
return undefined;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
export default getEsVersionFromNode;
|
|
@@ -3,6 +3,10 @@ import { configs } from "../plugins/webpack/index.js";
|
|
|
3
3
|
/** @type {import("eslint").Linter.Config} */
|
|
4
4
|
const recommendedWebpackSpecialConfig = configs.recommended;
|
|
5
5
|
|
|
6
|
+
/** @type {import("eslint").Linter.Config} */
|
|
7
|
+
const webpackSchemasConfig = configs.schemas;
|
|
8
|
+
|
|
6
9
|
export default {
|
|
7
10
|
"webpack/special": recommendedWebpackSpecialConfig,
|
|
11
|
+
"webpack/schemas": webpackSchemasConfig,
|
|
8
12
|
};
|
package/configs.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { globalIgnores } from "eslint/config";
|
|
2
2
|
import semver from "semver";
|
|
3
3
|
import configs from "./configs/index.js";
|
|
4
|
+
import getEsVersionFromNode from "./configs/utils/get-es-version-from-node.js";
|
|
4
5
|
import getJsonFile from "./configs/utils/get-json-file.js";
|
|
5
6
|
import ignorePaths from "./ignore-paths.js";
|
|
6
7
|
|
|
@@ -15,73 +16,56 @@ const isModule =
|
|
|
15
16
|
* @returns {import("eslint").Linter.Config} javascript configuration
|
|
16
17
|
*/
|
|
17
18
|
function getJavascriptConfig() {
|
|
18
|
-
if (packageJson.engines
|
|
19
|
-
|
|
20
|
-
const minMajorVersion = minVersion ? minVersion.major : undefined;
|
|
21
|
-
|
|
22
|
-
// https://node.green/
|
|
23
|
-
// https://github.com/microsoft/TypeScript/wiki/Node-Target-Mapping
|
|
24
|
-
switch (minMajorVersion) {
|
|
25
|
-
case 6: {
|
|
26
|
-
const config = {
|
|
27
|
-
...configs["javascript/es2016"],
|
|
28
|
-
rules: { ...configs["javascript/es2016"].rules },
|
|
29
|
-
};
|
|
30
|
-
|
|
31
|
-
config.rules["prefer-exponentiation-operator"] = "off";
|
|
32
|
-
|
|
33
|
-
return config;
|
|
34
|
-
}
|
|
35
|
-
case 7:
|
|
36
|
-
return configs["javascript/es2016"];
|
|
37
|
-
case 8:
|
|
38
|
-
case 9:
|
|
39
|
-
return configs["javascript/es2017"];
|
|
40
|
-
case 10:
|
|
41
|
-
case 11:
|
|
42
|
-
return configs["javascript/es2018"];
|
|
43
|
-
case 12:
|
|
44
|
-
case 13: {
|
|
45
|
-
/** @type {import("eslint").Linter.Config["languageOptions"]} */
|
|
46
|
-
const original = configs["javascript/es2019"].languageOptions;
|
|
47
|
-
/** @type {import("eslint").Linter.Config["languageOptions"]} */
|
|
48
|
-
const languageOptions = {
|
|
49
|
-
...original,
|
|
50
|
-
globals: {
|
|
51
|
-
// @ts-expect-error always exist
|
|
52
|
-
...original.globals,
|
|
53
|
-
Promise: false,
|
|
54
|
-
BigInt: false,
|
|
55
|
-
},
|
|
56
|
-
};
|
|
57
|
-
|
|
58
|
-
return { ...configs["javascript/es2019"], languageOptions };
|
|
59
|
-
}
|
|
60
|
-
case 14:
|
|
61
|
-
return configs["javascript/es2020"];
|
|
62
|
-
|
|
63
|
-
case 15:
|
|
64
|
-
return configs["javascript/es2021"];
|
|
65
|
-
case 16:
|
|
66
|
-
case 17:
|
|
67
|
-
case 18:
|
|
68
|
-
case 19:
|
|
69
|
-
return configs["javascript/es2022"];
|
|
70
|
-
case 20:
|
|
71
|
-
case 21:
|
|
72
|
-
return configs["javascript/es2023"];
|
|
73
|
-
case 22:
|
|
74
|
-
case 23:
|
|
75
|
-
return configs["javascript/es2024"];
|
|
76
|
-
case 24:
|
|
77
|
-
case 25:
|
|
78
|
-
return configs["javascript/es2025"];
|
|
79
|
-
default:
|
|
80
|
-
return configs["javascript/recommended"];
|
|
81
|
-
}
|
|
19
|
+
if (!packageJson.engines || !packageJson.engines.node) {
|
|
20
|
+
return configs["javascript/recommended"];
|
|
82
21
|
}
|
|
83
22
|
|
|
84
|
-
|
|
23
|
+
const nodeRange = packageJson.engines.node;
|
|
24
|
+
const minVersion = semver.minVersion(nodeRange);
|
|
25
|
+
|
|
26
|
+
// Node 6 has Array#includes (from 6.5) but never gained the `**` operator,
|
|
27
|
+
// so it doesn't map cleanly to a single ES year — handle it inline.
|
|
28
|
+
// https://node.green/
|
|
29
|
+
if (minVersion && minVersion.major === 6) {
|
|
30
|
+
const base = configs["javascript/es2016"];
|
|
31
|
+
return {
|
|
32
|
+
...base,
|
|
33
|
+
rules: {
|
|
34
|
+
...base.rules,
|
|
35
|
+
"prefer-exponentiation-operator": "off",
|
|
36
|
+
},
|
|
37
|
+
};
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
const esVersion = getEsVersionFromNode(nodeRange);
|
|
41
|
+
|
|
42
|
+
if (esVersion === undefined) {
|
|
43
|
+
return configs["javascript/recommended"];
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
const config = configs[`javascript/es${esVersion}`];
|
|
47
|
+
|
|
48
|
+
// The `globals.es2019` set from the `globals` package doesn't declare
|
|
49
|
+
// `Promise` or `BigInt`, but both are available at runtime on Node 12+.
|
|
50
|
+
// Re-add them as readonly globals so `no-undef` doesn't flag usage.
|
|
51
|
+
if (esVersion === 2019) {
|
|
52
|
+
/** @type {import("eslint").Linter.Config["languageOptions"]} */
|
|
53
|
+
const original = config.languageOptions;
|
|
54
|
+
/** @type {import("eslint").Linter.Config["languageOptions"]} */
|
|
55
|
+
const languageOptions = {
|
|
56
|
+
...original,
|
|
57
|
+
globals: {
|
|
58
|
+
// @ts-expect-error always exist
|
|
59
|
+
...original.globals,
|
|
60
|
+
Promise: false,
|
|
61
|
+
BigInt: false,
|
|
62
|
+
},
|
|
63
|
+
};
|
|
64
|
+
|
|
65
|
+
return { ...config, languageOptions };
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
return config;
|
|
85
69
|
}
|
|
86
70
|
|
|
87
71
|
const javascriptConfig = getJavascriptConfig();
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "eslint-config-webpack",
|
|
3
|
-
"version": "4.
|
|
3
|
+
"version": "4.10.0",
|
|
4
4
|
"description": "Provides Webpack's eslint rules as an extensible shared config",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"eslint",
|
|
@@ -45,31 +45,31 @@
|
|
|
45
45
|
"detect-indent": "^7.0.2",
|
|
46
46
|
"eslint-config-prettier": "^10.1.8",
|
|
47
47
|
"eslint-plugin-import": "^2.32.0",
|
|
48
|
-
"eslint-plugin-jest": "^29.15.
|
|
49
|
-
"eslint-plugin-jsdoc": "^
|
|
50
|
-
"eslint-plugin-n": "^
|
|
48
|
+
"eslint-plugin-jest": "^29.15.2",
|
|
49
|
+
"eslint-plugin-jsdoc": "^63.0.0",
|
|
50
|
+
"eslint-plugin-n": "^18.0.1",
|
|
51
51
|
"eslint-plugin-prettier": "^5.5.5",
|
|
52
52
|
"eslint-plugin-react": "^7.37.5",
|
|
53
|
-
"eslint-plugin-react-hooks": "^7.
|
|
53
|
+
"eslint-plugin-react-hooks": "^7.1.1",
|
|
54
54
|
"eslint-plugin-unicorn": "^64.0.0",
|
|
55
|
-
"globals": "^17.
|
|
55
|
+
"globals": "^17.6.0",
|
|
56
56
|
"jsonc-eslint-parser": "^3.1.0",
|
|
57
|
-
"semver": "^7.
|
|
58
|
-
"sort-package-json": "^
|
|
59
|
-
"typescript-eslint": "^8.
|
|
57
|
+
"semver": "^7.8.0",
|
|
58
|
+
"sort-package-json": "^4.0.0",
|
|
59
|
+
"typescript-eslint": "^8.59.3"
|
|
60
60
|
},
|
|
61
61
|
"devDependencies": {
|
|
62
|
-
"@changesets/cli": "^2.
|
|
62
|
+
"@changesets/cli": "^2.31.0",
|
|
63
63
|
"@changesets/get-github-info": "^0.8.0",
|
|
64
64
|
"@types/semver": "^7.7.1",
|
|
65
65
|
"eslint": "^9.39.2",
|
|
66
66
|
"eslint-find-rules": "^5.0.0",
|
|
67
|
-
"jest": "^30.
|
|
68
|
-
"prettier": "^3.8.
|
|
69
|
-
"react": "^19.2.
|
|
70
|
-
"react-dom": "^19.2.
|
|
71
|
-
"type-fest": "^5.
|
|
72
|
-
"typescript": "^6.0.
|
|
67
|
+
"jest": "^30.4.2",
|
|
68
|
+
"prettier": "^3.8.3",
|
|
69
|
+
"react": "^19.2.6",
|
|
70
|
+
"react-dom": "^19.2.6",
|
|
71
|
+
"type-fest": "^5.6.0",
|
|
72
|
+
"typescript": "^6.0.3"
|
|
73
73
|
},
|
|
74
74
|
"peerDependencies": {
|
|
75
75
|
"eslint": ">= 9.28.0",
|
package/plugins/webpack/index.js
CHANGED
|
@@ -1,6 +1,9 @@
|
|
|
1
1
|
import { createRequire } from "node:module";
|
|
2
|
+
// eslint-disable-next-line import/no-unresolved
|
|
3
|
+
import * as parserJsonc from "jsonc-eslint-parser";
|
|
2
4
|
import { allExtensions } from "../../configs/utils/extensions.js";
|
|
3
5
|
import { rule as requireLicenseComment } from "./rules/require-license-comment.js";
|
|
6
|
+
import { rule as validSchema } from "./rules/valid-schema.js";
|
|
4
7
|
|
|
5
8
|
const require = createRequire(import.meta.url);
|
|
6
9
|
|
|
@@ -8,6 +11,7 @@ const { version } = require("../../package.json");
|
|
|
8
11
|
|
|
9
12
|
const rules = {
|
|
10
13
|
"require-license-comment": requireLicenseComment,
|
|
14
|
+
"valid-schema": validSchema,
|
|
11
15
|
};
|
|
12
16
|
|
|
13
17
|
/** @type {import("eslint").Linter.Config["rules"]} */
|
|
@@ -19,7 +23,7 @@ const recommendedRules = {
|
|
|
19
23
|
),
|
|
20
24
|
};
|
|
21
25
|
|
|
22
|
-
/** @type {Record<"recommended", import("eslint").Linter.Config>} */
|
|
26
|
+
/** @type {Record<"recommended" | "schemas", import("eslint").Linter.Config>} */
|
|
23
27
|
const configs = {
|
|
24
28
|
recommended: {
|
|
25
29
|
name: "webpack/recommended",
|
|
@@ -32,6 +36,22 @@ const configs = {
|
|
|
32
36
|
},
|
|
33
37
|
rules: recommendedRules,
|
|
34
38
|
},
|
|
39
|
+
schemas: {
|
|
40
|
+
name: "webpack/schemas",
|
|
41
|
+
files: ["**/schemas/**/*.json"],
|
|
42
|
+
languageOptions: {
|
|
43
|
+
parser: parserJsonc,
|
|
44
|
+
},
|
|
45
|
+
plugins: {
|
|
46
|
+
get webpack() {
|
|
47
|
+
// eslint-disable-next-line no-use-before-define
|
|
48
|
+
return plugin;
|
|
49
|
+
},
|
|
50
|
+
},
|
|
51
|
+
rules: {
|
|
52
|
+
"webpack/valid-schema": "error",
|
|
53
|
+
},
|
|
54
|
+
},
|
|
35
55
|
};
|
|
36
56
|
|
|
37
57
|
const plugin = {
|
|
@@ -0,0 +1,370 @@
|
|
|
1
|
+
/** @typedef {import("jsonc-eslint-parser").AST.JSONExpression} JSONExpression */
|
|
2
|
+
/** @typedef {import("jsonc-eslint-parser").AST.JSONNode} JSONNode */
|
|
3
|
+
/** @typedef {import("jsonc-eslint-parser").AST.JSONObjectExpression} JSONObjectExpression */
|
|
4
|
+
/** @typedef {import("jsonc-eslint-parser").AST.JSONProgram} JSONProgram */
|
|
5
|
+
/** @typedef {import("jsonc-eslint-parser").AST.JSONProperty} JSONProperty */
|
|
6
|
+
|
|
7
|
+
// Keep in sync with the keywords `webpack/tooling` knows how to compile into
|
|
8
|
+
// declarations and validators - an unknown one is silently dropped there.
|
|
9
|
+
const DEFAULT_KEYWORDS = [
|
|
10
|
+
"definitions",
|
|
11
|
+
"$ref",
|
|
12
|
+
"$id",
|
|
13
|
+
"title",
|
|
14
|
+
"cli",
|
|
15
|
+
"items",
|
|
16
|
+
"implements",
|
|
17
|
+
"properties",
|
|
18
|
+
"additionalProperties",
|
|
19
|
+
"type",
|
|
20
|
+
"oneOf",
|
|
21
|
+
"anyOf",
|
|
22
|
+
"absolutePath",
|
|
23
|
+
"undefinedAsNull",
|
|
24
|
+
"description",
|
|
25
|
+
"enum",
|
|
26
|
+
"minLength",
|
|
27
|
+
"pattern",
|
|
28
|
+
"minimum",
|
|
29
|
+
"maximum",
|
|
30
|
+
"required",
|
|
31
|
+
"uniqueItems",
|
|
32
|
+
"minItems",
|
|
33
|
+
"minProperties",
|
|
34
|
+
"instanceof",
|
|
35
|
+
"tsType",
|
|
36
|
+
"not",
|
|
37
|
+
"link",
|
|
38
|
+
"deprecated",
|
|
39
|
+
"experimental",
|
|
40
|
+
"added",
|
|
41
|
+
];
|
|
42
|
+
|
|
43
|
+
const COMBINATOR_KEYWORDS = ["oneOf", "anyOf", "allOf"];
|
|
44
|
+
|
|
45
|
+
const DESCRIPTION_REGEXP = /^[A-Z`].*[^.]\.$/;
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* @param {JSONProperty} property property node
|
|
49
|
+
* @returns {string} name of the keyword the property declares
|
|
50
|
+
*/
|
|
51
|
+
function getKeywordName(property) {
|
|
52
|
+
const { key } = property;
|
|
53
|
+
|
|
54
|
+
return key.type === "JSONIdentifier" ? key.name : String(key.value);
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* @param {JSONExpression | null} node schema node
|
|
59
|
+
* @param {string} name keyword name
|
|
60
|
+
* @returns {JSONProperty | undefined} property declaring the keyword
|
|
61
|
+
*/
|
|
62
|
+
function getKeyword(node, name) {
|
|
63
|
+
if (!node || node.type !== "JSONObjectExpression") {
|
|
64
|
+
return undefined;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
return node.properties.find((property) => getKeywordName(property) === name);
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/**
|
|
71
|
+
* @param {JSONExpression | null | undefined} node schema node
|
|
72
|
+
* @param {string} expected expected string value
|
|
73
|
+
* @returns {boolean} whether the node is exactly that string literal
|
|
74
|
+
*/
|
|
75
|
+
function isStringLiteral(node, expected) {
|
|
76
|
+
return Boolean(
|
|
77
|
+
node && node.type === "JSONLiteral" && node.value === expected,
|
|
78
|
+
);
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
/**
|
|
82
|
+
* A schema that only points at another one carries its description there.
|
|
83
|
+
* @param {JSONExpression | null} node schema node
|
|
84
|
+
* @returns {boolean} whether the node is a plain reference
|
|
85
|
+
*/
|
|
86
|
+
function isReference(node) {
|
|
87
|
+
if (getKeyword(node, "$ref")) {
|
|
88
|
+
return true;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
const oneOf = getKeyword(node, "oneOf");
|
|
92
|
+
|
|
93
|
+
return Boolean(
|
|
94
|
+
oneOf &&
|
|
95
|
+
oneOf.value.type === "JSONArrayExpression" &&
|
|
96
|
+
oneOf.value.elements.length === 1 &&
|
|
97
|
+
getKeyword(oneOf.value.elements[0], "$ref"),
|
|
98
|
+
);
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
/**
|
|
102
|
+
* @type {import("eslint").Rule.RuleModule} rule rule
|
|
103
|
+
*/
|
|
104
|
+
export const rule = {
|
|
105
|
+
create(context) {
|
|
106
|
+
const {
|
|
107
|
+
allow = [],
|
|
108
|
+
disallow = [],
|
|
109
|
+
keywords = DEFAULT_KEYWORDS,
|
|
110
|
+
} = context.options[0] || {};
|
|
111
|
+
const allowedKeywords = new Set([...keywords, ...allow]);
|
|
112
|
+
|
|
113
|
+
for (const keyword of disallow) {
|
|
114
|
+
allowedKeywords.delete(keyword);
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
/**
|
|
118
|
+
* @param {JSONExpression | null} node schema node
|
|
119
|
+
* @param {JSONNode} target node the diagnostic points at
|
|
120
|
+
* @returns {void}
|
|
121
|
+
*/
|
|
122
|
+
function validateDescription(node, target) {
|
|
123
|
+
if (isReference(node)) {
|
|
124
|
+
return;
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
const description = getKeyword(node, "description");
|
|
128
|
+
const value = description && description.value;
|
|
129
|
+
|
|
130
|
+
if (
|
|
131
|
+
!value ||
|
|
132
|
+
value.type !== "JSONLiteral" ||
|
|
133
|
+
typeof value.value !== "string" ||
|
|
134
|
+
value.value.length === 0
|
|
135
|
+
) {
|
|
136
|
+
context.report({ loc: target.loc, messageId: "missingDescription" });
|
|
137
|
+
|
|
138
|
+
return;
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
if (!DESCRIPTION_REGEXP.test(value.value)) {
|
|
142
|
+
context.report({ loc: value.loc, messageId: "invalidDescription" });
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
/**
|
|
147
|
+
* @param {JSONExpression | null} node schema node
|
|
148
|
+
* @returns {void}
|
|
149
|
+
*/
|
|
150
|
+
function walk(node) {
|
|
151
|
+
if (!node || node.type !== "JSONObjectExpression") {
|
|
152
|
+
return;
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
const hasRef = Boolean(getKeyword(node, "$ref"));
|
|
156
|
+
|
|
157
|
+
for (const property of node.properties) {
|
|
158
|
+
const name = getKeywordName(property);
|
|
159
|
+
|
|
160
|
+
if (!allowedKeywords.has(name)) {
|
|
161
|
+
context.report({
|
|
162
|
+
data: { keyword: name },
|
|
163
|
+
loc: property.key.loc,
|
|
164
|
+
messageId: "unknownKeyword",
|
|
165
|
+
});
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
if (hasRef && name !== "$ref") {
|
|
169
|
+
context.report({
|
|
170
|
+
data: { keyword: name },
|
|
171
|
+
loc: property.key.loc,
|
|
172
|
+
messageId: "refWithOtherKeywords",
|
|
173
|
+
});
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
const type = getKeyword(node, "type");
|
|
178
|
+
|
|
179
|
+
if (
|
|
180
|
+
type &&
|
|
181
|
+
(type.value.type !== "JSONLiteral" ||
|
|
182
|
+
typeof type.value.value !== "string")
|
|
183
|
+
) {
|
|
184
|
+
context.report({ loc: type.value.loc, messageId: "multipleTypes" });
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
const instanceOf = getKeyword(node, "instanceof");
|
|
188
|
+
|
|
189
|
+
if (instanceOf && !getKeyword(node, "tsType")) {
|
|
190
|
+
context.report({
|
|
191
|
+
loc: instanceOf.key.loc,
|
|
192
|
+
messageId: "instanceofRequiresTsType",
|
|
193
|
+
});
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
const absolutePath = getKeyword(node, "absolutePath");
|
|
197
|
+
|
|
198
|
+
if (absolutePath && !isStringLiteral(type && type.value, "string")) {
|
|
199
|
+
context.report({
|
|
200
|
+
loc: absolutePath.key.loc,
|
|
201
|
+
messageId: "absolutePathRequiresStringType",
|
|
202
|
+
});
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
const properties = getKeyword(node, "properties");
|
|
206
|
+
const additionalProperties = getKeyword(node, "additionalProperties");
|
|
207
|
+
|
|
208
|
+
for (const keyword of [properties, additionalProperties]) {
|
|
209
|
+
if (keyword && !isStringLiteral(type && type.value, "object")) {
|
|
210
|
+
context.report({
|
|
211
|
+
data: { keyword: getKeywordName(keyword) },
|
|
212
|
+
loc: keyword.key.loc,
|
|
213
|
+
messageId: "objectKeywordRequiresObjectType",
|
|
214
|
+
});
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
for (const name of COMBINATOR_KEYWORDS) {
|
|
219
|
+
const keyword = getKeyword(node, name);
|
|
220
|
+
|
|
221
|
+
if (!keyword || keyword.value.type !== "JSONArrayExpression") {
|
|
222
|
+
continue;
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
const { elements } = keyword.value;
|
|
226
|
+
|
|
227
|
+
if (elements.length === 0) {
|
|
228
|
+
context.report({
|
|
229
|
+
data: { keyword: name },
|
|
230
|
+
loc: keyword.key.loc,
|
|
231
|
+
messageId: "emptyCombinator",
|
|
232
|
+
});
|
|
233
|
+
} else if ((elements.length === 1) !== (name === "oneOf")) {
|
|
234
|
+
context.report({
|
|
235
|
+
data: { keyword: name },
|
|
236
|
+
loc: keyword.key.loc,
|
|
237
|
+
messageId:
|
|
238
|
+
name === "oneOf" ? "oneOfSingleItem" : "combinatorMultipleItems",
|
|
239
|
+
});
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
for (const element of elements) {
|
|
243
|
+
if (!element) {
|
|
244
|
+
continue;
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
if (COMBINATOR_KEYWORDS.some((other) => getKeyword(element, other))) {
|
|
248
|
+
context.report({
|
|
249
|
+
loc: element.loc,
|
|
250
|
+
messageId: "nestedCombinator",
|
|
251
|
+
});
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
walk(element);
|
|
255
|
+
}
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
const items = getKeyword(node, "items");
|
|
259
|
+
|
|
260
|
+
if (items) {
|
|
261
|
+
validateDescription(items.value, items.key);
|
|
262
|
+
walk(items.value);
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
const definitions = getKeyword(node, "definitions");
|
|
266
|
+
|
|
267
|
+
if (definitions && definitions.value.type === "JSONObjectExpression") {
|
|
268
|
+
for (const definition of definitions.value.properties) {
|
|
269
|
+
validateDescription(definition.value, definition.key);
|
|
270
|
+
walk(definition.value);
|
|
271
|
+
}
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
if (properties) {
|
|
275
|
+
if (!additionalProperties) {
|
|
276
|
+
context.report({
|
|
277
|
+
loc: properties.key.loc,
|
|
278
|
+
messageId: "missingAdditionalProperties",
|
|
279
|
+
});
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
if (properties.value.type === "JSONObjectExpression") {
|
|
283
|
+
for (const property of properties.value.properties) {
|
|
284
|
+
validateDescription(property.value, property.key);
|
|
285
|
+
walk(property.value);
|
|
286
|
+
}
|
|
287
|
+
}
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
if (
|
|
291
|
+
additionalProperties &&
|
|
292
|
+
additionalProperties.value.type === "JSONObjectExpression"
|
|
293
|
+
) {
|
|
294
|
+
validateDescription(
|
|
295
|
+
additionalProperties.value,
|
|
296
|
+
additionalProperties.key,
|
|
297
|
+
);
|
|
298
|
+
walk(additionalProperties.value);
|
|
299
|
+
}
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
return {
|
|
303
|
+
"Program:exit"() {
|
|
304
|
+
const { ast } = context.sourceCode;
|
|
305
|
+
const program =
|
|
306
|
+
/** @type {JSONProgram} */
|
|
307
|
+
(/** @type {unknown} */ (ast));
|
|
308
|
+
const [statement] = program.body;
|
|
309
|
+
|
|
310
|
+
if (!statement || statement.type !== "JSONExpressionStatement") {
|
|
311
|
+
return;
|
|
312
|
+
}
|
|
313
|
+
|
|
314
|
+
walk(statement.expression);
|
|
315
|
+
},
|
|
316
|
+
};
|
|
317
|
+
},
|
|
318
|
+
meta: {
|
|
319
|
+
docs: {
|
|
320
|
+
category: "Possible Errors",
|
|
321
|
+
description: "Enforce webpack's conventions for JSON schema files",
|
|
322
|
+
recommended: false,
|
|
323
|
+
},
|
|
324
|
+
messages: {
|
|
325
|
+
absolutePathRequiresStringType:
|
|
326
|
+
'When using "absolutePath", "type" must be "string".',
|
|
327
|
+
combinatorMultipleItems: '"{{keyword}}" must have more than one item.',
|
|
328
|
+
emptyCombinator: '"{{keyword}}" must not be empty.',
|
|
329
|
+
instanceofRequiresTsType:
|
|
330
|
+
'When using "instanceof", "tsType" is required.',
|
|
331
|
+
invalidDescription:
|
|
332
|
+
"Description should start with an uppercase letter and end with a single dot.",
|
|
333
|
+
missingAdditionalProperties:
|
|
334
|
+
'Should have "additionalProperties" set to some value when describing "properties".',
|
|
335
|
+
missingDescription: "Should have a description set.",
|
|
336
|
+
multipleTypes: 'Should have a single "type".',
|
|
337
|
+
nestedCombinator: 'Should not double nest "oneOf"/"anyOf"/"allOf".',
|
|
338
|
+
objectKeywordRequiresObjectType:
|
|
339
|
+
'When using "{{keyword}}", "type" must be "object".',
|
|
340
|
+
oneOfSingleItem: '"oneOf" must have exactly one item.',
|
|
341
|
+
refWithOtherKeywords:
|
|
342
|
+
'When using "$ref" no other properties are possible, but found "{{keyword}}".',
|
|
343
|
+
unknownKeyword: 'Unexpected keyword "{{keyword}}".',
|
|
344
|
+
},
|
|
345
|
+
schema: [
|
|
346
|
+
{
|
|
347
|
+
additionalProperties: false,
|
|
348
|
+
properties: {
|
|
349
|
+
allow: {
|
|
350
|
+
items: { type: "string" },
|
|
351
|
+
type: "array",
|
|
352
|
+
uniqueItems: true,
|
|
353
|
+
},
|
|
354
|
+
disallow: {
|
|
355
|
+
items: { type: "string" },
|
|
356
|
+
type: "array",
|
|
357
|
+
uniqueItems: true,
|
|
358
|
+
},
|
|
359
|
+
keywords: {
|
|
360
|
+
items: { type: "string" },
|
|
361
|
+
type: "array",
|
|
362
|
+
uniqueItems: true,
|
|
363
|
+
},
|
|
364
|
+
},
|
|
365
|
+
type: "object",
|
|
366
|
+
},
|
|
367
|
+
],
|
|
368
|
+
type: "problem",
|
|
369
|
+
},
|
|
370
|
+
};
|