eslint-config-webpack 4.13.0 → 4.15.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
@@ -38,6 +38,52 @@ export default defineConfig([
38
38
  ]);
39
39
  ```
40
40
 
41
+ ### Prettier
42
+
43
+ The recommended config does not run Prettier: formatting every file again
44
+ inside ESLint is slow, and an editor already formats on save. Run Prettier
45
+ on its own, with the shared options:
46
+
47
+ ```bash
48
+ npm i -D prettier
49
+ ```
50
+
51
+ ```js
52
+ // prettier.config.mjs
53
+ export { default } from "eslint-config-webpack/prettier-config.js";
54
+ ```
55
+
56
+ Use `eslint-config-webpack/prettier-config-es5.js` instead for code that must
57
+ stay ES5 (trailing commas only where ES5 allows them).
58
+
59
+ ```json
60
+ {
61
+ "scripts": {
62
+ "lint": "eslint --cache . && prettier --cache --check .",
63
+ "fmt": "prettier --cache --write ."
64
+ }
65
+ }
66
+ ```
67
+
68
+ Run `lint` in CI, so unformatted code fails the build, and set your editor to
69
+ format on save with the Prettier extension. Files Prettier should skip go in
70
+ `.prettierignore`.
71
+
72
+ To report formatting as ESLint errors instead, as before, add the opt-in
73
+ `stylistic/prettier` config:
74
+
75
+ ```js
76
+ import { defineConfig } from "eslint/config";
77
+ import config from "eslint-config-webpack";
78
+ import configs from "eslint-config-webpack/configs.js";
79
+
80
+ export default defineConfig([
81
+ {
82
+ extends: [config, configs["stylistic/prettier"]],
83
+ },
84
+ ]);
85
+ ```
86
+
41
87
  ### Webpack-specific configs
42
88
 
43
89
  Three opt-in configs cover conventions only webpack's own repositories need.
@@ -60,11 +106,12 @@ export default defineConfig([
60
106
  ]);
61
107
  ```
62
108
 
63
- | Config | Files | What it checks |
64
- | ----------------- | ---------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
65
- | `webpack/special` | source files | `webpack/require-license-comment` — every file opens with the MIT license header. |
66
- | `webpack/schemas` | `**/schemas/**/*.json` | `webpack/valid-schema` — the JSON schema conventions webpack's declaration and validator generators rely on. `webpack/format-schema` — key order, and definitions kept in sync with the base schema (autofixable). |
67
- | `webpack/types` | `**/lib/**/*.{js,mjs,cjs}` | `webpack/inherit-jsdoc` — an overriding method carries the JSDoc of the method it overrides (autofixable). |
109
+ | Config | Files | What it checks |
110
+ | ------------------ | -------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
111
+ | `webpack/special` | source files | `webpack/require-license-comment` — every file opens with the MIT license header. |
112
+ | `webpack/schemas` | `**/schemas/**/*.json` | `webpack/valid-schema` — the JSON schema conventions webpack's declaration and validator generators rely on. `webpack/format-schema` — key order, and definitions kept in sync with the base schema (autofixable). |
113
+ | `webpack/types` | `**/lib/**/*.{js,mjs,cjs}` | `webpack/inherit-jsdoc` — an overriding method carries the JSDoc of the method it overrides (autofixable). |
114
+ | `webpack/comments` | source files | `webpack/comment-length` — a plain comment spans at most three lines. |
68
115
 
69
116
  `webpack/valid-schema` accepts options for which keywords a schema may use.
70
117
  `keywords` replaces the default set — webpack's own — outright, `allow` adds to
@@ -91,6 +138,30 @@ value, uses `instanceof` without `tsType`, `absolutePath` off a string or
91
138
  `additionalProperties`, nests or mis-sizes `oneOf`/`anyOf`/`allOf`, or leaves a
92
139
  property without a description starting in uppercase and ending in a single dot.
93
140
 
141
+ `webpack/comment-length` caps how many lines one plain comment spans, so an
142
+ explanation that outgrows the code it sits above is split, cut or moved into the
143
+ JSDoc of what it describes. A run of `//` lines counts as one comment, and `max`
144
+ changes the limit:
145
+
146
+ ```js
147
+ export default defineConfig([
148
+ {
149
+ files: ["lib/**/*.js", "test/**/*.js"],
150
+ extends: [configs["webpack/comments"]],
151
+ rules: {
152
+ "webpack/comment-length": ["error", { max: 3 }],
153
+ },
154
+ },
155
+ ]);
156
+ ```
157
+
158
+ Three kinds of comment are exempt, because none of them is commentary on the
159
+ line below. A JSDoc block is a type contract and is multi-line by construction.
160
+ Every comment above the file's first statement documents the file, like the
161
+ license header — a `"use strict"` directive does not end that preamble. And a
162
+ `//` run opening with `WHY:` states why the code is shaped the way it is, which
163
+ is the one explanation that cannot be made shorter without losing it.
164
+
94
165
  `webpack/inherit-jsdoc` needs type information, so `webpack/types` sets up
95
166
  `@typescript-eslint/parser` with `projectService` — the project therefore needs
96
167
  a `tsconfig.json` covering the linted files, and `typescript` installed. The
@@ -103,7 +174,10 @@ export default defineConfig([
103
174
  {
104
175
  extends: [configs["webpack/types"]],
105
176
  rules: {
106
- "webpack/inherit-jsdoc": ["error", { stripTags: ["abstract", "virtual"] }],
177
+ "webpack/inherit-jsdoc": [
178
+ "error",
179
+ { stripTags: ["abstract", "virtual"] },
180
+ ],
107
181
  },
108
182
  },
109
183
  ]);
@@ -1,17 +1,13 @@
1
1
  import stylisticPlugin from "@stylistic/eslint-plugin";
2
2
  import prettierPlugin from "eslint-plugin-prettier";
3
- import prettierConfig from "eslint-plugin-prettier/recommended";
4
3
 
5
4
  /** @type {import("eslint").Linter.Config} */
6
5
  const recommendedConfig = {
7
- ...prettierConfig,
8
6
  name: "stylistic/recommended",
9
7
  plugins: {
10
8
  "@stylistic": stylisticPlugin,
11
- prettier: prettierPlugin,
12
9
  },
13
10
  rules: {
14
- "prettier/prettier": "error",
15
11
  curly: ["error", "multi-line", "consistent"],
16
12
  "@stylistic/lines-between-class-members": "error",
17
13
  "@stylistic/quotes": [
@@ -85,6 +81,20 @@ const recommendedConfig = {
85
81
  },
86
82
  };
87
83
 
84
+ // Opt-in: running Prettier as a lint rule formats every file inside ESLint,
85
+ // which is slow; run `prettier --check` on its own instead (see README).
86
+ /** @type {import("eslint").Linter.Config} */
87
+ const prettierConfig = {
88
+ name: "stylistic/prettier",
89
+ plugins: {
90
+ prettier: prettierPlugin,
91
+ },
92
+ rules: {
93
+ "prettier/prettier": "error",
94
+ },
95
+ };
96
+
88
97
  export default {
89
98
  "stylistic/recommended": recommendedConfig,
99
+ "stylistic/prettier": prettierConfig,
90
100
  };
@@ -106,7 +106,8 @@ async function getTypescriptJSDocRecommendedConfig() {
106
106
  // From recommended
107
107
  // "jsdoc/escape-inline-tags": "error",
108
108
 
109
- // "jsdoc/implements-on-classes": "error",
109
+ // TypeScript reports `@implements` outside a class itself (TS8022)
110
+ "jsdoc/implements-on-classes": "off",
110
111
 
111
112
  // No need
112
113
  // "jsdoc/informative-docs": "error",
@@ -141,37 +142,23 @@ async function getTypescriptJSDocRecommendedConfig() {
141
142
  "jsdoc/no-restricted-syntax": [
142
143
  "error",
143
144
  {
145
+ // One context with one `:has()` instead of one per check: the plugin
146
+ // rebuilds the comment AST per context and `:has()` walks it each time.
144
147
  contexts: [
145
- // Prefer TypeScript syntax for functions
146
148
  {
147
- comment: "JsdocBlock:has(JsdocTypeFunction[arrow=false])",
149
+ comment: `JsdocBlock:has(${[
150
+ // Prefer TypeScript syntax for functions
151
+ "JsdocTypeFunction[arrow=false]",
152
+ // Prefer `{string=}` over `{string} [arg]`
153
+ "JsdocTag[tag=/^(property|param)$/][name=/[\\[\\]]/]",
154
+ // No `?` type
155
+ "JsdocTypeUnknown",
156
+ // No `Object`
157
+ "JsdocTag[tag!=/^(typedef|template|param)$/]:has(JsdocTypeName[value=/^(Object|object)$/])",
158
+ "JsdocTag[tag=typedef][parsedType.type!=JsdocTypeName]:has(JsdocTypeName[value=/^(Object|object)$/])",
159
+ ].join(", ")})`,
148
160
  message:
149
- "Please use TypeScript syntax - `(a: string, b: boolean) => number`",
150
- },
151
- // Prefer `{string=}` over `{string} [arg]`
152
- {
153
- comment:
154
- "JsdocBlock:has(JsdocTag[tag=/^(property|param)$/][name=/[\\[\\]]/])",
155
- message:
156
- "Please use `@property {string=} property`/`@param {string=} arg` instead `[arg]` for optional properties and parameters",
157
- },
158
- // No `?` type
159
- {
160
- comment: "JsdocBlock:has(JsdocTypeUnknown)",
161
- message: "Please use `unknown` or `any` (or `EXPECTED_ANY`) type",
162
- },
163
- // No `Object`
164
- {
165
- comment:
166
- "JsdocBlock:has(JsdocTag[tag!=/^(typedef|template|param)$/]:has(JsdocTypeName[value=/^(Object|object)$/]))",
167
- message:
168
- "Please use provide types for object - `{ property: number:, result: () => number}` instead `Object`/`object` or use `EXPECTED_OBJECT` type",
169
- },
170
- {
171
- comment:
172
- "JsdocBlock:has(JsdocTag[tag=typedef][parsedType.type!=JsdocTypeName]:has(JsdocTypeName[value=/^(Object|object)$/]))",
173
- message:
174
- "Please use provide types for object - `{ property: number:, result: () => number}` instead `Object`/`object` or use `EXPECTED_OBJECT` type",
161
+ "Please use TypeScript syntax: `(a: string) => number` for functions, `{string=}` instead of `[arg]` for optional properties and parameters, `unknown`/`any` (or `EXPECTED_ANY`) instead of `?`, and object types such as `{ property: number }` (or `EXPECTED_OBJECT`) instead of `Object`/`object`",
175
162
  },
176
163
  ],
177
164
  },
@@ -180,8 +167,9 @@ async function getTypescriptJSDocRecommendedConfig() {
180
167
  // No need
181
168
  // "jsdoc/no-types": "error",
182
169
 
183
- // No need
184
- // "jsdoc/no-undefined-types": "error",
170
+ // TypeScript reports an unknown type name itself (TS2304); it ignores the
171
+ // types of `@throws` and `@yields`, so those go unchecked
172
+ "jsdoc/no-undefined-types": "off",
185
173
 
186
174
  // TODO enable me in future
187
175
  // "jsdoc/prefer-import-tag": "error",
@@ -213,8 +201,9 @@ async function getTypescriptJSDocRecommendedConfig() {
213
201
  // No need
214
202
  // "jsdoc/require-next-description":"error",
215
203
 
216
- // No need
217
- // "jsdoc/require-next-type": "error",
204
+ // TypeScript ignores the type of `@next`; type a generator with
205
+ // `@returns {Generator<…>}` instead
206
+ "jsdoc/require-next-type": "off",
218
207
 
219
208
  // From recommended
220
209
  // "jsdoc/require-param": "error",
@@ -222,11 +211,12 @@ async function getTypescriptJSDocRecommendedConfig() {
222
211
  // From recommended
223
212
  // "jsdoc/require-param-description": "error",
224
213
 
225
- // From recommended
226
- // "jsdoc/require-param-name": "error",
214
+ // TypeScript fails to parse a `@param` without a name (TS1003)
215
+ "jsdoc/require-param-name": "off",
227
216
 
228
- // From recommended
229
- // "jsdoc/require-param-type": "error",
217
+ // `tsc` with `noImplicitAny` already reports an untyped parameter, and
218
+ // accepts one typed by its default value
219
+ "jsdoc/require-param-type": "off",
230
220
 
231
221
  // From recommended
232
222
  // "jsdoc/require-property": "error",
@@ -234,11 +224,13 @@ async function getTypescriptJSDocRecommendedConfig() {
234
224
  // From recommended
235
225
  // "jsdoc/require-property-description": "error",
236
226
 
237
- // From recommended
238
- // "jsdoc/require-property-name": "error",
227
+ // TypeScript fails to parse a `@typedef` `@property` without a name
228
+ // (TS1003); it ignores `@property` anywhere else
229
+ "jsdoc/require-property-name": "off",
239
230
 
240
- // From recommended
241
- // "jsdoc/require-property-type": "error",
231
+ // Kept: `tsc` reports an untyped `@property` (TS7005) only once the typedef
232
+ // is used, so an exported-only typedef would silently get `any`
233
+ "jsdoc/require-property-type": "error",
242
234
 
243
235
  // A lot of false positive with loops/`switch`/`if`/etc
244
236
  "jsdoc/require-returns-check": "off",
@@ -263,8 +255,8 @@ async function getTypescriptJSDocRecommendedConfig() {
263
255
  // No need
264
256
  // "jsdoc/require-throws-description": "error",
265
257
 
266
- // No need
267
- // "jsdoc/require-throws-type": "error",
258
+ // TypeScript ignores the type of `@throws`
259
+ "jsdoc/require-throws-type": "off",
268
260
 
269
261
  // From recommended
270
262
  // "jsdoc/require-yields": "error",
@@ -272,8 +264,9 @@ async function getTypescriptJSDocRecommendedConfig() {
272
264
  // No need
273
265
  // "jsdoc/require-yields-description": "error",
274
266
 
275
- // No need
276
- // "jsdoc/require-yields-type": "error",
267
+ // TypeScript ignores the type of `@yields`; type a generator with
268
+ // `@returns {Generator<…>}` instead
269
+ "jsdoc/require-yields-type": "off",
277
270
 
278
271
  // From recommended
279
272
  // "jsdoc/require-yields-check": "error",
@@ -3,6 +3,9 @@ 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 webpackCommentsConfig = configs.comments;
8
+
6
9
  /** @type {import("eslint").Linter.Config} */
7
10
  const webpackSchemasConfig = configs.schemas;
8
11
 
@@ -11,6 +14,7 @@ const webpackTypesConfig = configs.types;
11
14
 
12
15
  export default {
13
16
  "webpack/special": recommendedWebpackSpecialConfig,
17
+ "webpack/comments": webpackCommentsConfig,
14
18
  "webpack/schemas": webpackSchemasConfig,
15
19
  "webpack/types": webpackTypesConfig,
16
20
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "eslint-config-webpack",
3
- "version": "4.13.0",
3
+ "version": "4.15.0",
4
4
  "description": "Provides Webpack's eslint rules as an extensible shared config",
5
5
  "keywords": [
6
6
  "eslint",
@@ -32,9 +32,12 @@
32
32
  "configs"
33
33
  ],
34
34
  "scripts": {
35
- "lint": "npm run lint:code && npm run lint:types",
35
+ "lint": "npm run lint:code && npm run lint:types && npm run fmt:check",
36
36
  "lint:code": "eslint --cache .",
37
37
  "lint:types": "tsc -p tsconfig.internal.json",
38
+ "fmt": "prettier --cache --write .",
39
+ "fmt:check": "prettier --cache --check .",
40
+ "test": "cd test && node --test",
38
41
  "version": "changeset version",
39
42
  "release": "changeset publish"
40
43
  },
@@ -2,6 +2,7 @@ import { createRequire } from "node:module";
2
2
  // eslint-disable-next-line import/no-unresolved
3
3
  import * as parserJsonc from "jsonc-eslint-parser";
4
4
  import { allExtensions } from "../../configs/utils/extensions.js";
5
+ import { rule as commentLength } from "./rules/comment-length.js";
5
6
  import { rule as formatSchema } from "./rules/format-schema.js";
6
7
  import { rule as inheritJsdoc } from "./rules/inherit-jsdoc.js";
7
8
  import { rule as noDuplicateImportTag } from "./rules/no-duplicate-import-tag.js";
@@ -15,6 +16,7 @@ const require = createRequire(import.meta.url);
15
16
  const { version } = require("../../package.json");
16
17
 
17
18
  const rules = {
19
+ "comment-length": commentLength,
18
20
  "format-schema": formatSchema,
19
21
  "inherit-jsdoc": inheritJsdoc,
20
22
  "no-duplicate-import-tag": noDuplicateImportTag,
@@ -33,8 +35,21 @@ const recommendedRules = {
33
35
  ),
34
36
  };
35
37
 
36
- /** @type {Record<"recommended" | "schemas" | "types", import("eslint").Linter.Config>} */
38
+ /** @type {Record<"comments" | "recommended" | "schemas" | "types", import("eslint").Linter.Config>} */
37
39
  const configs = {
40
+ comments: {
41
+ name: "webpack/comments",
42
+ files: [`**/*.{${allExtensions.map((item) => item.slice(1)).join(",")}}`],
43
+ plugins: {
44
+ get webpack() {
45
+ // eslint-disable-next-line no-use-before-define
46
+ return plugin;
47
+ },
48
+ },
49
+ rules: {
50
+ "webpack/comment-length": "error",
51
+ },
52
+ },
38
53
  recommended: {
39
54
  name: "webpack/recommended",
40
55
  files: [`**/*.{${allExtensions.map((item) => item.slice(1)).join(",")}}`],
@@ -0,0 +1,127 @@
1
+ /** @typedef {import("estree").Comment} Comment */
2
+ /** @typedef {import("estree").Program} Program */
3
+ /** @typedef {import("estree").Statement | import("estree").ModuleDeclaration} ProgramEntry */
4
+ /** @typedef {import("eslint").AST.SourceLocation} SourceLocation */
5
+ /** @typedef {Comment & { range: [number, number], loc: SourceLocation }} LocatedComment */
6
+
7
+ const DEFAULT_MAX = 3;
8
+ const MARKER = /^[ \t]*WHY:/;
9
+
10
+ /**
11
+ * A directive such as `"use strict"` documents how the file is parsed rather
12
+ * than what it does, so it does not end the preamble.
13
+ * @param {ProgramEntry} node statement to test
14
+ * @returns {boolean} whether the statement is a directive prologue entry
15
+ */
16
+ const isDirective = (node) =>
17
+ node.type === "ExpressionStatement" &&
18
+ typeof (/** @type {{ directive?: string }} */ (node).directive) === "string";
19
+
20
+ /**
21
+ * @param {LocatedComment} comment comment to test
22
+ * @returns {boolean} whether the comment is a JSDoc block
23
+ */
24
+ const isJsdoc = (comment) =>
25
+ comment.type === "Block" && comment.value.startsWith("*");
26
+
27
+ /** @type {import("eslint").Rule.RuleModule} rule rule */
28
+ export const rule = {
29
+ create(context) {
30
+ const { sourceCode } = context;
31
+ const { max = DEFAULT_MAX } = context.options[0] || {};
32
+
33
+ /**
34
+ * @param {LocatedComment} start first comment of the run
35
+ * @param {number} endLine last line the run covers
36
+ * @returns {void}
37
+ */
38
+ const check = (start, endLine) => {
39
+ const lines = endLine - start.loc.start.line + 1;
40
+
41
+ if (lines <= max) return;
42
+
43
+ context.report({
44
+ loc: { start: start.loc.start, end: { line: endLine, column: 0 } },
45
+ messageId: "tooLong",
46
+ data: { lines: String(lines), max: String(max) },
47
+ });
48
+ };
49
+
50
+ return {
51
+ "Program:exit"(program) {
52
+ // Every comment above the first statement documents the file, like the
53
+ // license header, so the limit does not apply to any of them
54
+ const first = /** @type {Program} */ (program).body.find(
55
+ (node) => !isDirective(node),
56
+ );
57
+ const preambleEnd = first
58
+ ? /** @type {{ range: [number, number] }} */ (first).range[0]
59
+ : Number.MAX_SAFE_INTEGER;
60
+
61
+ /** @type {LocatedComment | undefined} */
62
+ let start;
63
+ let endLine = 0;
64
+ let exempt = false;
65
+
66
+ for (const comment of /** @type {LocatedComment[]} */ (
67
+ sourceCode.getAllComments()
68
+ )) {
69
+ const before = sourceCode.getTokenBefore(comment, {
70
+ includeComments: true,
71
+ });
72
+ const ownLine = before?.loc?.end.line !== comment.loc.start.line;
73
+
74
+ if (comment.type !== "Line" || !ownLine) {
75
+ if (start && !exempt) check(start, endLine);
76
+ start = undefined;
77
+
78
+ if (
79
+ comment.type === "Block" &&
80
+ ownLine &&
81
+ !isJsdoc(comment) &&
82
+ comment.range[0] >= preambleEnd
83
+ ) {
84
+ check(comment, comment.loc.end.line);
85
+ }
86
+
87
+ continue;
88
+ }
89
+
90
+ if (start && comment.loc.start.line === endLine + 1) {
91
+ endLine = comment.loc.end.line;
92
+ continue;
93
+ }
94
+
95
+ if (start && !exempt) check(start, endLine);
96
+
97
+ start = comment;
98
+ endLine = comment.loc.end.line;
99
+ // A run opening with the marker states why the code is shaped the way
100
+ // it is, which is the one explanation that cannot be made shorter
101
+ exempt = comment.range[0] < preambleEnd || MARKER.test(comment.value);
102
+ }
103
+
104
+ if (start && !exempt) check(start, endLine);
105
+ },
106
+ };
107
+ },
108
+ meta: {
109
+ docs: {
110
+ category: "Stylistic Issues",
111
+ description: "Limit how many lines a plain comment spans",
112
+ recommended: false,
113
+ },
114
+ messages: {
115
+ tooLong:
116
+ "This comment spans {{lines}} lines, over the limit of {{max}}. Split the thought, cut it, or move it into the JSDoc of what it describes.",
117
+ },
118
+ schema: [
119
+ {
120
+ type: "object",
121
+ properties: { max: { type: "integer", minimum: 1 } },
122
+ additionalProperties: false,
123
+ },
124
+ ],
125
+ type: "suggestion",
126
+ },
127
+ };