eslint-config-webpack 4.13.0 → 4.14.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
@@ -60,11 +60,12 @@ export default defineConfig([
60
60
  ]);
61
61
  ```
62
62
 
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). |
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). |
68
+ | `webpack/comments` | source files | `webpack/comment-length` — a plain comment spans at most three lines. |
68
69
 
69
70
  `webpack/valid-schema` accepts options for which keywords a schema may use.
70
71
  `keywords` replaces the default set — webpack's own — outright, `allow` adds to
@@ -91,6 +92,30 @@ value, uses `instanceof` without `tsType`, `absolutePath` off a string or
91
92
  `additionalProperties`, nests or mis-sizes `oneOf`/`anyOf`/`allOf`, or leaves a
92
93
  property without a description starting in uppercase and ending in a single dot.
93
94
 
95
+ `webpack/comment-length` caps how many lines one plain comment spans, so an
96
+ explanation that outgrows the code it sits above is split, cut or moved into the
97
+ JSDoc of what it describes. A run of `//` lines counts as one comment, and `max`
98
+ changes the limit:
99
+
100
+ ```js
101
+ export default defineConfig([
102
+ {
103
+ files: ["lib/**/*.js", "test/**/*.js"],
104
+ extends: [configs["webpack/comments"]],
105
+ rules: {
106
+ "webpack/comment-length": ["error", { max: 3 }],
107
+ },
108
+ },
109
+ ]);
110
+ ```
111
+
112
+ Three kinds of comment are exempt, because none of them is commentary on the
113
+ line below. A JSDoc block is a type contract and is multi-line by construction.
114
+ Every comment above the file's first statement documents the file, like the
115
+ license header — a `"use strict"` directive does not end that preamble. And a
116
+ `//` run opening with `WHY:` states why the code is shaped the way it is, which
117
+ is the one explanation that cannot be made shorter without losing it.
118
+
94
119
  `webpack/inherit-jsdoc` needs type information, so `webpack/types` sets up
95
120
  `@typescript-eslint/parser` with `projectService` — the project therefore needs
96
121
  a `tsconfig.json` covering the linted files, and `typescript` installed. The
@@ -103,7 +128,10 @@ export default defineConfig([
103
128
  {
104
129
  extends: [configs["webpack/types"]],
105
130
  rules: {
106
- "webpack/inherit-jsdoc": ["error", { stripTags: ["abstract", "virtual"] }],
131
+ "webpack/inherit-jsdoc": [
132
+ "error",
133
+ { stripTags: ["abstract", "virtual"] },
134
+ ],
107
135
  },
108
136
  },
109
137
  ]);
@@ -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.14.0",
4
4
  "description": "Provides Webpack's eslint rules as an extensible shared config",
5
5
  "keywords": [
6
6
  "eslint",
@@ -35,6 +35,7 @@
35
35
  "lint": "npm run lint:code && npm run lint:types",
36
36
  "lint:code": "eslint --cache .",
37
37
  "lint:types": "tsc -p tsconfig.internal.json",
38
+ "test": "cd test && node --test",
38
39
  "version": "changeset version",
39
40
  "release": "changeset publish"
40
41
  },
@@ -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
+ };