eslint-config-webpack 4.15.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 +48 -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 +17 -0
- package/configs/{jest.js → test.js} +65 -5
- package/configs/typescript.js +39 -10
- 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 +31 -26
- 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
|
@@ -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
|
+
};
|
|
@@ -0,0 +1,414 @@
|
|
|
1
|
+
import fs from "node:fs";
|
|
2
|
+
import path from "node:path";
|
|
3
|
+
import { findVariable } from "@eslint-community/eslint-utils";
|
|
4
|
+
|
|
5
|
+
/** @import { CallExpression, Node } from "estree" */
|
|
6
|
+
/** @typedef {import("eslint").Scope.Scope} Scope */
|
|
7
|
+
|
|
8
|
+
const SCHEMA_UTILS = "schema-utils";
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* @typedef {object} ValidationApi
|
|
12
|
+
* @property {string} name how the call is named in the report
|
|
13
|
+
* @property {number} checkIndex position of the check among the arguments
|
|
14
|
+
*/
|
|
15
|
+
|
|
16
|
+
/** @type {Record<"plugin" | "loader" | "schemaUtils", ValidationApi>} */
|
|
17
|
+
const APIS = {
|
|
18
|
+
plugin: {
|
|
19
|
+
name: "compiler.validate(schema, options, configuration, check)",
|
|
20
|
+
checkIndex: 3,
|
|
21
|
+
},
|
|
22
|
+
loader: { name: "this.getOptions(schema, check)", checkIndex: 1 },
|
|
23
|
+
schemaUtils: {
|
|
24
|
+
name: "validate(schema, options, configuration, check)",
|
|
25
|
+
checkIndex: 3,
|
|
26
|
+
},
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* @param {Node} node node
|
|
31
|
+
* @returns {string | undefined} name of an identifier or of the last property of a member chain
|
|
32
|
+
*/
|
|
33
|
+
const lastName = (node) => {
|
|
34
|
+
if (node.type === "Identifier") return node.name;
|
|
35
|
+
|
|
36
|
+
if (
|
|
37
|
+
node.type === "MemberExpression" &&
|
|
38
|
+
!node.computed &&
|
|
39
|
+
node.property.type === "Identifier"
|
|
40
|
+
) {
|
|
41
|
+
return node.property.name;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
return undefined;
|
|
45
|
+
};
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* @param {Node | null | undefined} node node
|
|
49
|
+
* @returns {string | undefined} the module a `require("…")` call loads
|
|
50
|
+
*/
|
|
51
|
+
const requiredModule = (node) => {
|
|
52
|
+
if (
|
|
53
|
+
node?.type === "CallExpression" &&
|
|
54
|
+
node.callee.type === "Identifier" &&
|
|
55
|
+
node.callee.name === "require" &&
|
|
56
|
+
node.arguments.length === 1 &&
|
|
57
|
+
node.arguments[0].type === "Literal" &&
|
|
58
|
+
typeof node.arguments[0].value === "string"
|
|
59
|
+
) {
|
|
60
|
+
return node.arguments[0].value;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
return undefined;
|
|
64
|
+
};
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* @param {Node | null | undefined} node node
|
|
68
|
+
* @returns {string | undefined} the module an `await import("…")` loads
|
|
69
|
+
*/
|
|
70
|
+
const importedModule = (node) => {
|
|
71
|
+
const expression = node?.type === "AwaitExpression" ? node.argument : node;
|
|
72
|
+
|
|
73
|
+
if (
|
|
74
|
+
expression?.type === "ImportExpression" &&
|
|
75
|
+
expression.source.type === "Literal" &&
|
|
76
|
+
typeof expression.source.value === "string"
|
|
77
|
+
) {
|
|
78
|
+
return expression.source.value;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
return undefined;
|
|
82
|
+
};
|
|
83
|
+
|
|
84
|
+
/**
|
|
85
|
+
* @param {Node | null | undefined} node node
|
|
86
|
+
* @returns {string | undefined} the module a `require` or dynamic `import` loads
|
|
87
|
+
*/
|
|
88
|
+
const loadedModule = (node) => requiredModule(node) ?? importedModule(node);
|
|
89
|
+
|
|
90
|
+
/**
|
|
91
|
+
* @typedef {object} Binding
|
|
92
|
+
* @property {string} source module the binding comes from
|
|
93
|
+
* @property {string | undefined} imported the export it is bound to, `undefined` for the whole module
|
|
94
|
+
*/
|
|
95
|
+
|
|
96
|
+
/**
|
|
97
|
+
* Where a variable gets its value from when it is bound to a module or one of its exports.
|
|
98
|
+
* @param {Scope} scope scope
|
|
99
|
+
* @param {import("estree").Identifier} identifier identifier
|
|
100
|
+
* @returns {Binding | undefined} binding
|
|
101
|
+
*/
|
|
102
|
+
const resolveBinding = (scope, identifier) => {
|
|
103
|
+
const variable = findVariable(scope, identifier);
|
|
104
|
+
|
|
105
|
+
if (!variable || variable.defs.length !== 1) return undefined;
|
|
106
|
+
|
|
107
|
+
const [definition] = variable.defs;
|
|
108
|
+
|
|
109
|
+
if (definition.type === "ImportBinding") {
|
|
110
|
+
const source = /** @type {import("estree").ImportDeclaration} */ (
|
|
111
|
+
definition.parent
|
|
112
|
+
).source.value;
|
|
113
|
+
|
|
114
|
+
if (typeof source !== "string") return undefined;
|
|
115
|
+
|
|
116
|
+
const { node } = definition;
|
|
117
|
+
|
|
118
|
+
if (node.type === "ImportSpecifier") {
|
|
119
|
+
return {
|
|
120
|
+
source,
|
|
121
|
+
imported:
|
|
122
|
+
node.imported.type === "Identifier"
|
|
123
|
+
? node.imported.name
|
|
124
|
+
: String(node.imported.value),
|
|
125
|
+
};
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
// A default import of a CommonJS module is the whole module
|
|
129
|
+
return { source, imported: undefined };
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
if (definition.type !== "Variable") return undefined;
|
|
133
|
+
|
|
134
|
+
const { id, init } = definition.node;
|
|
135
|
+
const source = loadedModule(init);
|
|
136
|
+
|
|
137
|
+
if (source === undefined) {
|
|
138
|
+
// `const { validate } = schemaUtils`
|
|
139
|
+
if (id.type === "ObjectPattern" && init?.type === "Identifier") {
|
|
140
|
+
const parent = resolveBinding(scope, init);
|
|
141
|
+
|
|
142
|
+
if (parent && parent.imported === undefined) {
|
|
143
|
+
const property = id.properties.find(
|
|
144
|
+
(item) =>
|
|
145
|
+
item.type === "Property" &&
|
|
146
|
+
item.value.type === "Identifier" &&
|
|
147
|
+
item.value.name === identifier.name,
|
|
148
|
+
);
|
|
149
|
+
|
|
150
|
+
if (
|
|
151
|
+
property?.type === "Property" &&
|
|
152
|
+
!property.computed &&
|
|
153
|
+
property.key.type === "Identifier"
|
|
154
|
+
) {
|
|
155
|
+
return { source: parent.source, imported: property.key.name };
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
return undefined;
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
if (id.type === "Identifier") return { source, imported: undefined };
|
|
164
|
+
|
|
165
|
+
if (id.type === "ObjectPattern") {
|
|
166
|
+
const property = id.properties.find(
|
|
167
|
+
(item) =>
|
|
168
|
+
item.type === "Property" &&
|
|
169
|
+
item.value.type === "Identifier" &&
|
|
170
|
+
item.value.name === identifier.name,
|
|
171
|
+
);
|
|
172
|
+
|
|
173
|
+
if (
|
|
174
|
+
property?.type === "Property" &&
|
|
175
|
+
!property.computed &&
|
|
176
|
+
property.key.type === "Identifier"
|
|
177
|
+
) {
|
|
178
|
+
return { source, imported: property.key.name };
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
return undefined;
|
|
183
|
+
};
|
|
184
|
+
|
|
185
|
+
/**
|
|
186
|
+
* @param {Scope} scope scope
|
|
187
|
+
* @param {Node} node expression
|
|
188
|
+
* @returns {boolean} true when the expression is the `schema-utils` module itself
|
|
189
|
+
*/
|
|
190
|
+
const isSchemaUtilsModule = (scope, node) => {
|
|
191
|
+
if (loadedModule(node) === SCHEMA_UTILS) return true;
|
|
192
|
+
|
|
193
|
+
if (node.type === "Identifier") {
|
|
194
|
+
const binding = resolveBinding(scope, node);
|
|
195
|
+
|
|
196
|
+
return (
|
|
197
|
+
binding !== undefined &&
|
|
198
|
+
binding.source === SCHEMA_UTILS &&
|
|
199
|
+
binding.imported === undefined
|
|
200
|
+
);
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
return false;
|
|
204
|
+
};
|
|
205
|
+
|
|
206
|
+
/**
|
|
207
|
+
* Which validation API a call is, if any.
|
|
208
|
+
* @param {Scope} scope scope
|
|
209
|
+
* @param {CallExpression} node call
|
|
210
|
+
* @returns {ValidationApi | undefined} api
|
|
211
|
+
*/
|
|
212
|
+
const getApi = (scope, node) => {
|
|
213
|
+
const { callee } = node;
|
|
214
|
+
|
|
215
|
+
if (callee.type === "Identifier") {
|
|
216
|
+
const binding = resolveBinding(scope, callee);
|
|
217
|
+
|
|
218
|
+
return binding?.source === SCHEMA_UTILS && binding.imported === "validate"
|
|
219
|
+
? APIS.schemaUtils
|
|
220
|
+
: undefined;
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
if (
|
|
224
|
+
callee.type !== "MemberExpression" ||
|
|
225
|
+
callee.computed ||
|
|
226
|
+
callee.property.type !== "Identifier"
|
|
227
|
+
) {
|
|
228
|
+
return undefined;
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
const { object } = callee;
|
|
232
|
+
|
|
233
|
+
switch (callee.property.name) {
|
|
234
|
+
case "validate": {
|
|
235
|
+
if (isSchemaUtilsModule(scope, object)) return APIS.schemaUtils;
|
|
236
|
+
|
|
237
|
+
const name = lastName(object);
|
|
238
|
+
|
|
239
|
+
// `compiler.validate`, `this.compiler.validate`, `childCompiler.validate`,
|
|
240
|
+
// but not an unrelated `templateCompiler.validate`
|
|
241
|
+
return name !== undefined &&
|
|
242
|
+
/^(?:child|first|parent|webpack)?compiler$/i.test(name)
|
|
243
|
+
? APIS.plugin
|
|
244
|
+
: undefined;
|
|
245
|
+
}
|
|
246
|
+
case "getOptions": {
|
|
247
|
+
// `this.getOptions` in a loader, or the loader context passed around
|
|
248
|
+
if (object.type === "ThisExpression") return APIS.loader;
|
|
249
|
+
|
|
250
|
+
const name = lastName(object);
|
|
251
|
+
|
|
252
|
+
return name !== undefined &&
|
|
253
|
+
/^(?:loaderContext|loaderCtx|ctx)$/i.test(name)
|
|
254
|
+
? APIS.loader
|
|
255
|
+
: undefined;
|
|
256
|
+
}
|
|
257
|
+
default:
|
|
258
|
+
return undefined;
|
|
259
|
+
}
|
|
260
|
+
};
|
|
261
|
+
|
|
262
|
+
/**
|
|
263
|
+
* The relative `.json` file a schema argument is loaded from.
|
|
264
|
+
* @param {Scope} scope scope
|
|
265
|
+
* @param {Node} node schema argument
|
|
266
|
+
* @returns {{ request: string, required: boolean } | undefined} request of the schema file and whether it is loaded with `require`
|
|
267
|
+
*/
|
|
268
|
+
const getSchemaRequest = (scope, node) => {
|
|
269
|
+
// `() => require("./options.json")`, a schema loaded on demand
|
|
270
|
+
if (
|
|
271
|
+
node.type === "ArrowFunctionExpression" &&
|
|
272
|
+
node.body.type !== "BlockStatement"
|
|
273
|
+
) {
|
|
274
|
+
return getSchemaRequest(scope, node.body);
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
const required = requiredModule(node);
|
|
278
|
+
|
|
279
|
+
if (required !== undefined) return { request: required, required: true };
|
|
280
|
+
|
|
281
|
+
if (node.type === "Identifier") {
|
|
282
|
+
const variable = findVariable(scope, node);
|
|
283
|
+
|
|
284
|
+
if (!variable || variable.defs.length !== 1) return undefined;
|
|
285
|
+
|
|
286
|
+
const [definition] = variable.defs;
|
|
287
|
+
|
|
288
|
+
if (
|
|
289
|
+
definition.type === "ImportBinding" &&
|
|
290
|
+
definition.node.type === "ImportDefaultSpecifier"
|
|
291
|
+
) {
|
|
292
|
+
const source = /** @type {import("estree").ImportDeclaration} */ (
|
|
293
|
+
definition.parent
|
|
294
|
+
).source.value;
|
|
295
|
+
|
|
296
|
+
return typeof source === "string"
|
|
297
|
+
? { request: source, required: false }
|
|
298
|
+
: undefined;
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
if (
|
|
302
|
+
definition.type === "Variable" &&
|
|
303
|
+
definition.node.id.type === "Identifier"
|
|
304
|
+
) {
|
|
305
|
+
const request = requiredModule(definition.node.init);
|
|
306
|
+
|
|
307
|
+
return request === undefined ? undefined : { request, required: true };
|
|
308
|
+
}
|
|
309
|
+
}
|
|
310
|
+
|
|
311
|
+
return undefined;
|
|
312
|
+
};
|
|
313
|
+
|
|
314
|
+
/** @type {import("eslint").Rule.RuleModule} */
|
|
315
|
+
export const rule = {
|
|
316
|
+
create(context) {
|
|
317
|
+
const { sourceCode } = context;
|
|
318
|
+
|
|
319
|
+
return {
|
|
320
|
+
CallExpression(node) {
|
|
321
|
+
if (node.arguments.some((item) => item.type === "SpreadElement")) {
|
|
322
|
+
return;
|
|
323
|
+
}
|
|
324
|
+
|
|
325
|
+
const scope = sourceCode.getScope(node);
|
|
326
|
+
const api = getApi(scope, node);
|
|
327
|
+
|
|
328
|
+
// No schema means no validation, `getOptions()` only parses the options
|
|
329
|
+
if (
|
|
330
|
+
!api ||
|
|
331
|
+
node.arguments.length === 0 ||
|
|
332
|
+
node.arguments.length > api.checkIndex
|
|
333
|
+
) {
|
|
334
|
+
return;
|
|
335
|
+
}
|
|
336
|
+
|
|
337
|
+
const [schemaArgument] = node.arguments;
|
|
338
|
+
const found = getSchemaRequest(
|
|
339
|
+
scope,
|
|
340
|
+
/** @type {Node} */ (schemaArgument),
|
|
341
|
+
);
|
|
342
|
+
// Only a schema file next to the code can be precompiled from here
|
|
343
|
+
const schema =
|
|
344
|
+
found !== undefined &&
|
|
345
|
+
/^\.\.?\//.test(found.request) &&
|
|
346
|
+
found.request.endsWith(".json")
|
|
347
|
+
? found
|
|
348
|
+
: undefined;
|
|
349
|
+
const command =
|
|
350
|
+
schema === undefined
|
|
351
|
+
? "npx schema-utils precompile <schema.json>"
|
|
352
|
+
: `npx schema-utils precompile ${path.posix.join(
|
|
353
|
+
path.posix.dirname(
|
|
354
|
+
path
|
|
355
|
+
.relative(context.cwd, context.physicalFilename)
|
|
356
|
+
.replaceAll("\\", "/"),
|
|
357
|
+
),
|
|
358
|
+
schema.request,
|
|
359
|
+
)}`;
|
|
360
|
+
|
|
361
|
+
/** @type {import("eslint").Rule.SuggestionReportDescriptor[]} */
|
|
362
|
+
const suggest = [];
|
|
363
|
+
const checkRequest =
|
|
364
|
+
schema === undefined
|
|
365
|
+
? undefined
|
|
366
|
+
: `${schema.request.slice(0, -".json".length)}.check.js`;
|
|
367
|
+
|
|
368
|
+
// An imported schema would need an import of the check added too
|
|
369
|
+
if (
|
|
370
|
+
schema?.required &&
|
|
371
|
+
checkRequest !== undefined &&
|
|
372
|
+
fs.existsSync(
|
|
373
|
+
path.resolve(path.dirname(context.physicalFilename), checkRequest),
|
|
374
|
+
)
|
|
375
|
+
) {
|
|
376
|
+
const missing = api.checkIndex - node.arguments.length;
|
|
377
|
+
const lastArgument = node.arguments.at(-1);
|
|
378
|
+
|
|
379
|
+
suggest.push({
|
|
380
|
+
messageId: "useCheck",
|
|
381
|
+
data: { check: checkRequest },
|
|
382
|
+
fix: (fixer) =>
|
|
383
|
+
fixer.insertTextAfter(
|
|
384
|
+
lastArgument,
|
|
385
|
+
`${", undefined".repeat(missing)}, require(${JSON.stringify(checkRequest)})`,
|
|
386
|
+
),
|
|
387
|
+
});
|
|
388
|
+
}
|
|
389
|
+
|
|
390
|
+
context.report({
|
|
391
|
+
node,
|
|
392
|
+
messageId: "missingCheck",
|
|
393
|
+
data: { api: api.name, command },
|
|
394
|
+
suggest,
|
|
395
|
+
});
|
|
396
|
+
},
|
|
397
|
+
};
|
|
398
|
+
},
|
|
399
|
+
meta: {
|
|
400
|
+
docs: {
|
|
401
|
+
description:
|
|
402
|
+
"Require a precompiled check when validating options against a schema",
|
|
403
|
+
recommended: true,
|
|
404
|
+
},
|
|
405
|
+
hasSuggestions: true,
|
|
406
|
+
messages: {
|
|
407
|
+
missingCheck:
|
|
408
|
+
"Validating against a schema without a precompiled check loads `ajv` and compiles the schema on every start, about 150ms for the first one. Precompile it with `{{command}}` and pass the check: `{{api}}`.",
|
|
409
|
+
useCheck: "Pass the precompiled check `{{check}}`.",
|
|
410
|
+
},
|
|
411
|
+
schema: [],
|
|
412
|
+
type: "suggestion",
|
|
413
|
+
},
|
|
414
|
+
};
|
package/plugins/webpack/index.js
CHANGED
|
@@ -1,5 +1,4 @@
|
|
|
1
1
|
import { createRequire } from "node:module";
|
|
2
|
-
// eslint-disable-next-line import/no-unresolved
|
|
3
2
|
import * as parserJsonc from "jsonc-eslint-parser";
|
|
4
3
|
import { allExtensions } from "../../configs/utils/extensions.js";
|
|
5
4
|
import { rule as commentLength } from "./rules/comment-length.js";
|
|
@@ -1,5 +1,4 @@
|
|
|
1
|
-
/** @
|
|
2
|
-
/** @typedef {import("estree").Program} Program */
|
|
1
|
+
/** @import { Comment, Program } from "estree" */
|
|
3
2
|
/** @typedef {import("estree").Statement | import("estree").ModuleDeclaration} ProgramEntry */
|
|
4
3
|
/** @typedef {import("eslint").AST.SourceLocation} SourceLocation */
|
|
5
4
|
/** @typedef {Comment & { range: [number, number], loc: SourceLocation }} LocatedComment */
|
|
@@ -107,7 +106,6 @@ export const rule = {
|
|
|
107
106
|
},
|
|
108
107
|
meta: {
|
|
109
108
|
docs: {
|
|
110
|
-
category: "Stylistic Issues",
|
|
111
109
|
description: "Limit how many lines a plain comment spans",
|
|
112
110
|
recommended: false,
|
|
113
111
|
},
|
|
@@ -52,11 +52,13 @@ function processSchema(visitor, json, context) {
|
|
|
52
52
|
if (visitor.schema) json = visitor.schema(json, context);
|
|
53
53
|
|
|
54
54
|
for (const name of NESTED_WITH_NAME) {
|
|
55
|
-
if (name in json && json[name] && typeof json[name] === "object") {
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
55
|
+
if (!(name in json && json[name] && typeof json[name] === "object")) {
|
|
56
|
+
continue;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
if (visitor.object) json[name] = visitor.object(json[name], context);
|
|
60
|
+
for (const key of Object.keys(json[name])) {
|
|
61
|
+
json[name][key] = processSchema(visitor, json[name][key], context);
|
|
60
62
|
}
|
|
61
63
|
}
|
|
62
64
|
for (const name of NESTED_DIRECT) {
|
|
@@ -65,13 +67,15 @@ function processSchema(visitor, json, context) {
|
|
|
65
67
|
}
|
|
66
68
|
}
|
|
67
69
|
for (const name of NESTED_ARRAY) {
|
|
68
|
-
if (name in json && Array.isArray(json[name])) {
|
|
69
|
-
|
|
70
|
-
for (let i = 0; i < json[name].length; i++) {
|
|
71
|
-
json[name][i] = processSchema(visitor, json[name][i], context);
|
|
72
|
-
}
|
|
73
|
-
if (visitor.array) json[name] = visitor.array(json[name], context);
|
|
70
|
+
if (!(name in json && Array.isArray(json[name]))) {
|
|
71
|
+
continue;
|
|
74
72
|
}
|
|
73
|
+
|
|
74
|
+
json[name] = [...json[name]];
|
|
75
|
+
for (let i = 0; i < json[name].length; i++) {
|
|
76
|
+
json[name][i] = processSchema(visitor, json[name][i], context);
|
|
77
|
+
}
|
|
78
|
+
if (visitor.array) json[name] = visitor.array(json[name], context);
|
|
75
79
|
}
|
|
76
80
|
|
|
77
81
|
return json;
|
|
@@ -100,10 +104,9 @@ function sortArrayByType(array) {
|
|
|
100
104
|
const bType = b.type || b.instanceof || (b.enum && "enum");
|
|
101
105
|
const aPosition = TYPE_ORDER.indexOf(aType);
|
|
102
106
|
const bPosition = TYPE_ORDER.indexOf(bType);
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
return aPosition - bPosition;
|
|
107
|
+
return aPosition === bPosition
|
|
108
|
+
? array.indexOf(a) - array.indexOf(b)
|
|
109
|
+
: aPosition - bPosition;
|
|
107
110
|
});
|
|
108
111
|
}
|
|
109
112
|
|
|
@@ -311,7 +314,6 @@ export const rule = {
|
|
|
311
314
|
},
|
|
312
315
|
meta: {
|
|
313
316
|
docs: {
|
|
314
|
-
category: "Stylistic Issues",
|
|
315
317
|
description:
|
|
316
318
|
"Keep schema keys ordered and shared definitions in sync with the base schema",
|
|
317
319
|
recommended: false,
|