eslint-config-webpack 4.14.0 → 5.0.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 +94 -4
- package/configs/browser.js +70 -7
- package/configs/index.js +7 -6
- package/configs/javascript.js +746 -151
- package/configs/json.js +72 -0
- package/configs/markdown.js +111 -6
- package/configs/node.js +78 -23
- package/configs/performance.js +8 -0
- package/configs/react.js +7 -19
- package/configs/stylistic.js +31 -4
- package/configs/{jest.js → test.js} +65 -5
- package/configs/typescript.js +77 -55
- package/configs/utils/code-files.js +20 -0
- package/configs/utils/get-es-version-from-node.js +9 -4
- package/configs/utils/get-json-file.js +1 -5
- package/configs/utils/is-jest-installed.js +20 -0
- package/configs/utils/is-typescript-installed.js +3 -3
- package/configs.js +47 -31
- package/package.json +34 -27
- package/plugins/package-json/index.js +0 -5
- package/plugins/package-json/rules/order-properties.js +8 -18
- package/plugins/performance/index.js +60 -0
- package/plugins/performance/rules/no-case-insensitive-unicode-range.js +214 -0
- package/plugins/performance/rules/require-precompiled-schema.js +414 -0
- package/plugins/webpack/index.js +0 -1
- package/plugins/webpack/rules/comment-length.js +1 -3
- package/plugins/webpack/rules/format-schema.js +18 -16
- package/plugins/webpack/rules/inherit-jsdoc.js +2 -3
- package/plugins/webpack/rules/no-duplicate-import-tag.js +3 -4
- package/plugins/webpack/rules/no-unused-import-tag.js +8 -11
- package/plugins/webpack/rules/prefer-import-tag.js +1 -2
- package/plugins/webpack/rules/require-license-comment.js +3 -5
- package/plugins/webpack/rules/valid-schema.js +9 -13
- package/plugins/webpack/utils/import-tags.js +4 -3
- package/configs/package-json.js +0 -8
|
@@ -1,10 +1,6 @@
|
|
|
1
1
|
import detectIndent from "detect-indent";
|
|
2
2
|
import { sortOrder } from "sort-package-json";
|
|
3
3
|
|
|
4
|
-
/** @typedef {import("jsonc-eslint-parser").AST.JSONProgram} JSONProgram */
|
|
5
|
-
/** @typedef {import("jsonc-eslint-parser").AST.JSONExpression} JSONExpression */
|
|
6
|
-
/** @typedef {import("jsonc-eslint-parser").AST.JSONObjectExpression} JSONObjectExpression */
|
|
7
|
-
|
|
8
4
|
/**
|
|
9
5
|
* @param {string} string string
|
|
10
6
|
* @returns {"\r\n" | "\n" | undefined} detected newline
|
|
@@ -34,7 +30,7 @@ function detectNewlineGraceful(string) {
|
|
|
34
30
|
return detectNewline(string) || "\n";
|
|
35
31
|
}
|
|
36
32
|
|
|
37
|
-
/** @
|
|
33
|
+
/** @import { PackageJson } from "type-fest" */
|
|
38
34
|
|
|
39
35
|
/**
|
|
40
36
|
* @param {PackageJson} object object to sort
|
|
@@ -94,7 +90,7 @@ export const rule = {
|
|
|
94
90
|
}
|
|
95
91
|
|
|
96
92
|
return {
|
|
97
|
-
"
|
|
93
|
+
"Document:exit"() {
|
|
98
94
|
const { ast, text } = context.sourceCode;
|
|
99
95
|
|
|
100
96
|
const options = {
|
|
@@ -115,17 +111,12 @@ export const rule = {
|
|
|
115
111
|
]);
|
|
116
112
|
const orderedKeys = Object.keys(orderedSource);
|
|
117
113
|
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
(
|
|
121
|
-
/** @type {JSONProgram} */ (/** @type {unknown} */ (ast)).body[0]
|
|
122
|
-
.expression
|
|
123
|
-
);
|
|
114
|
+
// @ts-expect-error bad types
|
|
115
|
+
const { members } = ast.body;
|
|
124
116
|
|
|
125
|
-
for (let i = 0; i <
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
const { value } = property;
|
|
117
|
+
for (let i = 0; i < members.length; i += 1) {
|
|
118
|
+
// Strict JSON parsing guarantees that keys are always string literals
|
|
119
|
+
const { value } = members[i].name;
|
|
129
120
|
|
|
130
121
|
if (value === orderedKeys[i]) {
|
|
131
122
|
continue;
|
|
@@ -151,7 +142,7 @@ export const rule = {
|
|
|
151
142
|
|
|
152
143
|
return fixer.replaceText(context.sourceCode.ast, result);
|
|
153
144
|
},
|
|
154
|
-
loc:
|
|
145
|
+
loc: members[i].loc,
|
|
155
146
|
messageId: "incorrectOrder",
|
|
156
147
|
});
|
|
157
148
|
}
|
|
@@ -160,7 +151,6 @@ export const rule = {
|
|
|
160
151
|
},
|
|
161
152
|
meta: {
|
|
162
153
|
docs: {
|
|
163
|
-
category: "Best Practices",
|
|
164
154
|
description: "Package properties must be declared in standard order",
|
|
165
155
|
recommended: true,
|
|
166
156
|
},
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
import { createRequire } from "node:module";
|
|
2
|
+
import {
|
|
3
|
+
allExtensions,
|
|
4
|
+
javascriptExtensions,
|
|
5
|
+
} from "../../configs/utils/extensions.js";
|
|
6
|
+
import isTypescriptInstalled from "../../configs/utils/is-typescript-installed.js";
|
|
7
|
+
import { rule as noCaseInsensitiveUnicodeRange } from "./rules/no-case-insensitive-unicode-range.js";
|
|
8
|
+
import { rule as requirePrecompiledSchema } from "./rules/require-precompiled-schema.js";
|
|
9
|
+
|
|
10
|
+
const require = createRequire(import.meta.url);
|
|
11
|
+
|
|
12
|
+
const { version } = require("../../package.json");
|
|
13
|
+
|
|
14
|
+
const rules = {
|
|
15
|
+
"no-case-insensitive-unicode-range": noCaseInsensitiveUnicodeRange,
|
|
16
|
+
"require-precompiled-schema": requirePrecompiledSchema,
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
/** @type {import("eslint").Linter.Config["rules"]} */
|
|
20
|
+
const recommendedRules = {
|
|
21
|
+
...Object.fromEntries(
|
|
22
|
+
Object.entries(rules)
|
|
23
|
+
.filter(([, rule]) => rule.meta?.docs?.recommended)
|
|
24
|
+
.map(([name]) => [`performance/${name}`, "error"]),
|
|
25
|
+
),
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
// Only the files the javascript config lints, so a project without TypeScript
|
|
29
|
+
// doesn't parse `.ts` files with the default parser.
|
|
30
|
+
const extensions = isTypescriptInstalled()
|
|
31
|
+
? allExtensions
|
|
32
|
+
: javascriptExtensions;
|
|
33
|
+
|
|
34
|
+
/** @type {Record<"recommended", import("eslint").Linter.Config>} */
|
|
35
|
+
const configs = {
|
|
36
|
+
recommended: {
|
|
37
|
+
name: "performance/recommended",
|
|
38
|
+
files: [`**/*.{${extensions.map((item) => item.slice(1)).join(",")}}`],
|
|
39
|
+
ignores: ["**/*.d.ts"],
|
|
40
|
+
plugins: {
|
|
41
|
+
get performance() {
|
|
42
|
+
// eslint-disable-next-line no-use-before-define
|
|
43
|
+
return plugin;
|
|
44
|
+
},
|
|
45
|
+
},
|
|
46
|
+
rules: recommendedRules,
|
|
47
|
+
},
|
|
48
|
+
};
|
|
49
|
+
|
|
50
|
+
const plugin = {
|
|
51
|
+
configs,
|
|
52
|
+
meta: {
|
|
53
|
+
version,
|
|
54
|
+
},
|
|
55
|
+
rules,
|
|
56
|
+
};
|
|
57
|
+
|
|
58
|
+
export { configs, rules };
|
|
59
|
+
|
|
60
|
+
export default plugin;
|
|
@@ -0,0 +1,214 @@
|
|
|
1
|
+
import { findVariable, getStaticValue } from "@eslint-community/eslint-utils";
|
|
2
|
+
import { RegExpParser, visitRegExpAST } from "@eslint-community/regexpp";
|
|
3
|
+
|
|
4
|
+
/** @import { Node } from "estree" */
|
|
5
|
+
/** @typedef {import("eslint").Scope.Scope} Scope */
|
|
6
|
+
/** @typedef {import("estree").CallExpression | import("estree").NewExpression} RegExpCall */
|
|
7
|
+
|
|
8
|
+
const parser = new RegExpParser();
|
|
9
|
+
|
|
10
|
+
/** @typedef {{ messageId: "range" | "property", raw: string }} FoldedPart */
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* The first part of a case-insensitive pattern V8 has to case-fold at compile
|
|
14
|
+
* time: a class range reaching past ASCII, or a Unicode property escape.
|
|
15
|
+
* @param {string} pattern regex source
|
|
16
|
+
* @param {string} flags regex flags
|
|
17
|
+
* @returns {FoldedPart | undefined} the offending part, if any
|
|
18
|
+
*/
|
|
19
|
+
const findFoldedRange = (pattern, flags) => {
|
|
20
|
+
/** @type {import("@eslint-community/regexpp").AST.Pattern} */
|
|
21
|
+
let ast;
|
|
22
|
+
|
|
23
|
+
try {
|
|
24
|
+
ast = parser.parsePattern(pattern, 0, pattern.length, {
|
|
25
|
+
unicode: flags.includes("u"),
|
|
26
|
+
unicodeSets: flags.includes("v"),
|
|
27
|
+
});
|
|
28
|
+
} catch {
|
|
29
|
+
return undefined;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/** @type {FoldedPart | undefined} */
|
|
33
|
+
let found;
|
|
34
|
+
|
|
35
|
+
visitRegExpAST(ast, {
|
|
36
|
+
onCharacterClassRangeEnter(node) {
|
|
37
|
+
if (found === undefined && node.max.value > 0x7f) {
|
|
38
|
+
found = { messageId: "range", raw: node.raw };
|
|
39
|
+
}
|
|
40
|
+
},
|
|
41
|
+
onCharacterSetEnter(node) {
|
|
42
|
+
if (found === undefined && node.kind === "property") {
|
|
43
|
+
found = { messageId: "property", raw: node.raw };
|
|
44
|
+
}
|
|
45
|
+
},
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
return found;
|
|
49
|
+
};
|
|
50
|
+
|
|
51
|
+
// A part whose value cannot be known stands in as a group that matches nothing
|
|
52
|
+
// in particular, so the constant parts around it still parse as one pattern.
|
|
53
|
+
const UNKNOWN = "(?:)";
|
|
54
|
+
const MAX_DEPTH = 8;
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* The source a pattern argument builds, with every part that cannot be resolved
|
|
58
|
+
* statically replaced by an empty group.
|
|
59
|
+
* @param {Node} node pattern expression
|
|
60
|
+
* @param {Scope} scope scope the expression is evaluated in
|
|
61
|
+
* @param {number} depth how many constants were followed to get here
|
|
62
|
+
* @returns {string} the approximate pattern
|
|
63
|
+
*/
|
|
64
|
+
const approximate = (node, scope, depth) => {
|
|
65
|
+
const known = getStaticValue(node, scope);
|
|
66
|
+
|
|
67
|
+
if (known !== null) {
|
|
68
|
+
return typeof known.value === "string" ? known.value : UNKNOWN;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
if (depth >= MAX_DEPTH) return UNKNOWN;
|
|
72
|
+
|
|
73
|
+
if (node.type === "TemplateLiteral") {
|
|
74
|
+
return node.quasis
|
|
75
|
+
.map(
|
|
76
|
+
(quasi, i) =>
|
|
77
|
+
(quasi.value.cooked ?? "") +
|
|
78
|
+
(i < node.expressions.length
|
|
79
|
+
? approximate(
|
|
80
|
+
/** @type {Node} */ (node.expressions[i]),
|
|
81
|
+
scope,
|
|
82
|
+
depth + 1,
|
|
83
|
+
)
|
|
84
|
+
: ""),
|
|
85
|
+
)
|
|
86
|
+
.join("");
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
if (
|
|
90
|
+
node.type === "TaggedTemplateExpression" &&
|
|
91
|
+
node.tag.type === "MemberExpression" &&
|
|
92
|
+
node.tag.object.type === "Identifier" &&
|
|
93
|
+
node.tag.object.name === "String" &&
|
|
94
|
+
node.tag.property.type === "Identifier" &&
|
|
95
|
+
node.tag.property.name === "raw"
|
|
96
|
+
) {
|
|
97
|
+
const { quasis, expressions } = node.quasi;
|
|
98
|
+
|
|
99
|
+
return quasis
|
|
100
|
+
.map(
|
|
101
|
+
(quasi, i) =>
|
|
102
|
+
quasi.value.raw +
|
|
103
|
+
(i < expressions.length
|
|
104
|
+
? approximate(
|
|
105
|
+
/** @type {Node} */ (expressions[i]),
|
|
106
|
+
scope,
|
|
107
|
+
depth + 1,
|
|
108
|
+
)
|
|
109
|
+
: ""),
|
|
110
|
+
)
|
|
111
|
+
.join("");
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
if (node.type === "BinaryExpression" && node.operator === "+") {
|
|
115
|
+
return (
|
|
116
|
+
approximate(/** @type {Node} */ (node.left), scope, depth + 1) +
|
|
117
|
+
approximate(node.right, scope, depth + 1)
|
|
118
|
+
);
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
if (node.type === "Identifier") {
|
|
122
|
+
const variable = findVariable(scope, node);
|
|
123
|
+
const definition =
|
|
124
|
+
variable?.defs.length === 1 ? variable.defs[0] : undefined;
|
|
125
|
+
|
|
126
|
+
if (
|
|
127
|
+
definition?.type === "Variable" &&
|
|
128
|
+
definition.parent.kind === "const" &&
|
|
129
|
+
definition.node.init
|
|
130
|
+
) {
|
|
131
|
+
return approximate(definition.node.init, scope, depth + 1);
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
return UNKNOWN;
|
|
136
|
+
};
|
|
137
|
+
|
|
138
|
+
/** @type {import("eslint").Rule.RuleModule} */
|
|
139
|
+
export const rule = {
|
|
140
|
+
create(context) {
|
|
141
|
+
const { sourceCode } = context;
|
|
142
|
+
|
|
143
|
+
/**
|
|
144
|
+
* @param {Node} node node to report
|
|
145
|
+
* @param {string} pattern regex source
|
|
146
|
+
* @param {string} flags regex flags
|
|
147
|
+
* @returns {void}
|
|
148
|
+
*/
|
|
149
|
+
const check = (node, pattern, flags) => {
|
|
150
|
+
if (!flags.includes("i")) return;
|
|
151
|
+
|
|
152
|
+
const part = findFoldedRange(pattern, flags);
|
|
153
|
+
|
|
154
|
+
if (part === undefined) return;
|
|
155
|
+
|
|
156
|
+
context.report({
|
|
157
|
+
node,
|
|
158
|
+
messageId: part.messageId,
|
|
159
|
+
data: { range: part.raw },
|
|
160
|
+
});
|
|
161
|
+
};
|
|
162
|
+
|
|
163
|
+
/**
|
|
164
|
+
* @param {RegExpCall} node `RegExp(…)` or `new RegExp(…)`
|
|
165
|
+
* @returns {void}
|
|
166
|
+
*/
|
|
167
|
+
const checkCall = (node) => {
|
|
168
|
+
if (
|
|
169
|
+
node.callee.type !== "Identifier" ||
|
|
170
|
+
node.callee.name !== "RegExp" ||
|
|
171
|
+
node.arguments.length < 2
|
|
172
|
+
) {
|
|
173
|
+
return;
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
const scope = sourceCode.getScope(node);
|
|
177
|
+
const [patternNode, flagsNode] = node.arguments;
|
|
178
|
+
const flags = getStaticValue(/** @type {Node} */ (flagsNode), scope);
|
|
179
|
+
|
|
180
|
+
if (flags === null || typeof flags.value !== "string") return;
|
|
181
|
+
|
|
182
|
+
check(
|
|
183
|
+
node,
|
|
184
|
+
approximate(/** @type {Node} */ (patternNode), scope, 0),
|
|
185
|
+
flags.value,
|
|
186
|
+
);
|
|
187
|
+
};
|
|
188
|
+
|
|
189
|
+
return {
|
|
190
|
+
Literal(node) {
|
|
191
|
+
if ("regex" in node && node.regex) {
|
|
192
|
+
check(node, node.regex.pattern, node.regex.flags);
|
|
193
|
+
}
|
|
194
|
+
},
|
|
195
|
+
CallExpression: checkCall,
|
|
196
|
+
NewExpression: checkCall,
|
|
197
|
+
};
|
|
198
|
+
},
|
|
199
|
+
meta: {
|
|
200
|
+
docs: {
|
|
201
|
+
description:
|
|
202
|
+
"Disallow the `i` flag on a regex whose character ranges reach past ASCII",
|
|
203
|
+
recommended: true,
|
|
204
|
+
},
|
|
205
|
+
messages: {
|
|
206
|
+
range:
|
|
207
|
+
"`{{range}}` makes V8 case-fold the whole range when it compiles this case-insensitive regex. Spell the ASCII letters in both cases (`a-zA-Z`) and drop the `i` flag.",
|
|
208
|
+
property:
|
|
209
|
+
"`{{range}}` makes V8 case-fold the whole property when it compiles this case-insensitive regex. Drop the `i` flag, naming the cases to match in the property itself (`\\p{L}` rather than `\\p{Lu}`).",
|
|
210
|
+
},
|
|
211
|
+
schema: [],
|
|
212
|
+
type: "suggestion",
|
|
213
|
+
},
|
|
214
|
+
};
|