eslint-config-webpack 4.9.6 → 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 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
@@ -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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "eslint-config-webpack",
3
- "version": "4.9.6",
3
+ "version": "4.10.0",
4
4
  "description": "Provides Webpack's eslint rules as an extensible shared config",
5
5
  "keywords": [
6
6
  "eslint",
@@ -46,7 +46,7 @@
46
46
  "eslint-config-prettier": "^10.1.8",
47
47
  "eslint-plugin-import": "^2.32.0",
48
48
  "eslint-plugin-jest": "^29.15.2",
49
- "eslint-plugin-jsdoc": "^62.9.0",
49
+ "eslint-plugin-jsdoc": "^63.0.0",
50
50
  "eslint-plugin-n": "^18.0.1",
51
51
  "eslint-plugin-prettier": "^5.5.5",
52
52
  "eslint-plugin-react": "^7.37.5",
@@ -55,7 +55,7 @@
55
55
  "globals": "^17.6.0",
56
56
  "jsonc-eslint-parser": "^3.1.0",
57
57
  "semver": "^7.8.0",
58
- "sort-package-json": "^3.6.0",
58
+ "sort-package-json": "^4.0.0",
59
59
  "typescript-eslint": "^8.59.3"
60
60
  },
61
61
  "devDependencies": {
@@ -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
+ };