adamantite 0.45.0 → 0.47.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 +447 -127
- package/dist/index.js +628 -57
- package/dist/oxlint.js +1 -0
- package/dist/presets/analyze.js +1 -0
- package/dist/presets/lint/{react-doctor.d.ts → architecture.d.ts} +1 -1
- package/dist/presets/lint/architecture.js +8 -0
- package/dist/presets/lint/core.js +1 -2
- package/dist/presets/lint/effect.js +26 -1
- package/dist/presets/lint/plugin/glob.d.ts +7 -0
- package/dist/presets/lint/plugin/glob.js +48 -0
- package/dist/presets/lint/plugin/imports.d.ts +4 -0
- package/dist/presets/lint/plugin/imports.js +4 -1
- package/dist/presets/lint/plugin/index.d.ts +3 -0
- package/dist/presets/lint/plugin/index.js +14 -8
- package/dist/presets/lint/plugin/options.d.ts +5 -5
- package/dist/presets/lint/plugin/options.js +19 -7
- package/dist/presets/lint/plugin/resolver.d.ts +8 -0
- package/dist/presets/lint/plugin/resolver.js +79 -0
- package/dist/presets/lint/plugin/rules/import-owners.d.ts +67 -0
- package/dist/presets/lint/plugin/rules/import-owners.js +310 -0
- package/dist/presets/lint/plugin/rules/layers.d.ts +136 -0
- package/dist/presets/lint/plugin/rules/layers.js +479 -0
- package/dist/presets/lint/plugin/rules/no-query-data-in-state.js +1 -1
- package/dist/presets/lint/plugin/rules/no-react-state-hooks.d.ts +0 -5
- package/dist/presets/lint/plugin/rules/no-react-state-hooks.js +3 -47
- package/dist/presets/lint/plugin/rules/query-from-loader.js +2 -2
- package/dist/presets/lint/plugin/rules/require-suppression-reason.d.ts +5 -0
- package/dist/presets/lint/plugin/rules/require-suppression-reason.js +32 -0
- package/dist/presets/lint/react-strict.js +22 -2
- package/dist/presets/lint/strict.js +35 -2
- package/dist/presets/lint/{antislop.d.ts → tailwind.d.ts} +1 -1
- package/dist/presets/lint/{shadcn.js → tailwind.js} +1 -1
- package/dist/presets/lint/vendor/antislop/license.md +26 -1
- package/dist/presets/lint/vendor/antislop/plugin.d.mts +3 -0
- package/dist/presets/lint/vendor/antislop/plugin.mjs +959 -33
- package/dist/presets/lint/vendor/antislop-effect/license.md +27 -0
- package/dist/presets/lint/vendor/antislop-effect/plugin.d.mts +17 -0
- package/dist/presets/lint/vendor/antislop-effect/plugin.mjs +579 -0
- package/dist/presets/{lint → rules}/custom.d.ts +3 -20
- package/dist/presets/{lint → rules}/custom.js +50 -46
- package/dist/presets/rules/folder.d.ts +34 -0
- package/dist/presets/rules/folder.js +52 -0
- package/dist/presets/rules/index.d.ts +2 -9
- package/dist/presets/rules/index.js +1 -29
- package/dist/presets/rules/load.d.ts +8 -0
- package/dist/presets/rules/load.js +29 -0
- package/dist/presets/rules/plugin.d.ts +3 -0
- package/dist/presets/rules/plugin.js +1 -0
- package/dist/presets/rules/testing.d.ts +2 -0
- package/dist/presets/rules/testing.js +14 -0
- package/dist/presets/tsconfig.json +3 -0
- package/dist/presets/typescript/resets/array-includes.d.ts +15 -0
- package/dist/presets/typescript/resets/array-index-of.d.ts +23 -0
- package/dist/presets/typescript/resets/eval.d.ts +1 -0
- package/dist/presets/typescript/resets/fetch.d.ts +3 -0
- package/dist/presets/typescript/resets/filter-boolean.d.ts +24 -0
- package/dist/presets/typescript/resets/helpers.d.ts +15 -0
- package/dist/presets/typescript/resets/index.d.ts +20 -0
- package/dist/presets/typescript/resets/is-array.d.ts +3 -0
- package/dist/presets/typescript/resets/json-parse.d.ts +14 -0
- package/dist/presets/typescript/resets/license.md +12 -0
- package/dist/presets/typescript/resets/map-constructor.d.ts +3 -0
- package/dist/presets/typescript/resets/map-groupby.d.ts +3 -0
- package/dist/presets/typescript/resets/map-has.d.ts +9 -0
- package/dist/presets/typescript/resets/object-assign.d.ts +8 -0
- package/dist/presets/typescript/resets/object-get-prototype-of.d.ts +3 -0
- package/dist/presets/typescript/resets/object-groupby.d.ts +6 -0
- package/dist/presets/typescript/resets/object-values.d.ts +8 -0
- package/dist/presets/typescript/resets/reflect.d.ts +12 -0
- package/dist/presets/typescript/resets/set-has.d.ts +9 -0
- package/dist/presets/typescript/resets/storage.d.ts +3 -0
- package/dist/presets/typescript.json +13 -0
- package/dist/runtime-CVZPDpfE.js +11 -0
- package/package.json +81 -19
- package/skills/adamantite/SKILL.md +110 -207
- package/skills/adamantite/references/custom-rules.md +120 -0
- package/skills/adamantite/references/lint-rules.md +152 -0
- package/skills/adamantite/references/setup.md +70 -0
- package/dist/presets/lint/antislop.js +0 -28
- package/dist/presets/lint/react-doctor.js +0 -20
- package/dist/presets/lint/shadcn.d.ts +0 -5
|
@@ -0,0 +1,479 @@
|
|
|
1
|
+
import { globToRegExp } from "../glob.js";
|
|
2
|
+
import { isStringLiteral } from "../imports.js";
|
|
3
|
+
import { readBooleanOption, readObjectOption, readStringArrayOption, readStringOption } from "../options.js";
|
|
4
|
+
import { resolveImport } from "../resolver.js";
|
|
5
|
+
import { existsSync } from "node:fs";
|
|
6
|
+
import { posix } from "node:path";
|
|
7
|
+
//#region presets/lint/plugin/rules/layers.ts
|
|
8
|
+
const LAYER_MAP_HINT = "Layer map: the `adamantite/layers` options for root `{{root}}` in the Oxlint config at `{{configPath}}`.";
|
|
9
|
+
function toPosixPath(path) {
|
|
10
|
+
return path.replaceAll("\\", "/");
|
|
11
|
+
}
|
|
12
|
+
/**
|
|
13
|
+
* Read one layer map from one option object. Undefined when `root` or `layers` is missing.
|
|
14
|
+
*/
|
|
15
|
+
function readLayerMap(option, root, configPath) {
|
|
16
|
+
const options = [option];
|
|
17
|
+
const declarations = readObjectOption(options, "layers");
|
|
18
|
+
if (root === void 0 || declarations === void 0) return;
|
|
19
|
+
const layers = [];
|
|
20
|
+
const children = /* @__PURE__ */ new Map();
|
|
21
|
+
for (const [name, declaration] of Object.entries(declarations)) {
|
|
22
|
+
const imports = readStringArrayOption([declaration], "imports");
|
|
23
|
+
if (imports === void 0) return;
|
|
24
|
+
layers.push([name, imports]);
|
|
25
|
+
const child = readLayerMap(declaration, posix.join(root, name), `${configPath}.${name}.layers`);
|
|
26
|
+
if (child !== void 0) children.set(name, child);
|
|
27
|
+
}
|
|
28
|
+
return {
|
|
29
|
+
children,
|
|
30
|
+
conditions: readStringArrayOption(options, "conditions") ?? [],
|
|
31
|
+
configPath,
|
|
32
|
+
entries: (readStringArrayOption(options, "entries") ?? []).map((glob) => globToRegExp(glob)),
|
|
33
|
+
ignore: (readStringArrayOption(options, "ignore") ?? []).map((glob) => globToRegExp(glob)),
|
|
34
|
+
ignoreTypes: readBooleanOption(options, "ignoreTypes") ?? false,
|
|
35
|
+
isolate: new Set(readStringArrayOption(options, "isolate")),
|
|
36
|
+
layers: Object.fromEntries(layers),
|
|
37
|
+
root: globToRegExp(root.split("/").filter((segment) => segment !== "" && segment !== ".").join("/")),
|
|
38
|
+
rootGlob: root
|
|
39
|
+
};
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* Read the rule options: one layer map for each option object. A map without `root` or `layers` is
|
|
43
|
+
* left out.
|
|
44
|
+
*/
|
|
45
|
+
function readLayersOptions(options) {
|
|
46
|
+
return options.flatMap((option, index) => readLayerMap(option, readStringOption([option], "root"), `options[${index}].layers`) ?? []);
|
|
47
|
+
}
|
|
48
|
+
/**
|
|
49
|
+
* Find the layers that `name` imports, including itself.
|
|
50
|
+
*/
|
|
51
|
+
function listImportable(layers, name) {
|
|
52
|
+
const importable = /* @__PURE__ */ new Set();
|
|
53
|
+
const pending = [name];
|
|
54
|
+
for (let next = pending.pop(); next !== void 0; next = pending.pop()) if (!importable.has(next)) {
|
|
55
|
+
importable.add(next);
|
|
56
|
+
pending.push(...layers[next] ?? []);
|
|
57
|
+
}
|
|
58
|
+
return importable;
|
|
59
|
+
}
|
|
60
|
+
function findCycle(layers) {
|
|
61
|
+
const done = /* @__PURE__ */ new Set();
|
|
62
|
+
function visit(name, path) {
|
|
63
|
+
const start = path.indexOf(name);
|
|
64
|
+
if (start !== -1) return [...path.slice(start), name];
|
|
65
|
+
if (done.has(name)) return;
|
|
66
|
+
for (const below of layers[name] ?? []) {
|
|
67
|
+
const cycle = visit(below, [...path, name]);
|
|
68
|
+
if (cycle !== void 0) return cycle;
|
|
69
|
+
}
|
|
70
|
+
done.add(name);
|
|
71
|
+
}
|
|
72
|
+
for (const name of Object.keys(layers)) {
|
|
73
|
+
const cycle = visit(name, []);
|
|
74
|
+
if (cycle !== void 0) return cycle;
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
/**
|
|
78
|
+
* Find the mistakes in a layer map: a name that is not a layer, and a cycle.
|
|
79
|
+
*/
|
|
80
|
+
function findMapErrors(layers, isolate) {
|
|
81
|
+
const errors = [];
|
|
82
|
+
for (const [layer, imports] of Object.entries(layers)) for (const name of imports) if (!Object.hasOwn(layers, name)) errors.push({
|
|
83
|
+
kind: "unknownImport",
|
|
84
|
+
layer,
|
|
85
|
+
name
|
|
86
|
+
});
|
|
87
|
+
for (const name of isolate) if (!Object.hasOwn(layers, name)) errors.push({
|
|
88
|
+
kind: "unknownIsolate",
|
|
89
|
+
name
|
|
90
|
+
});
|
|
91
|
+
const cycle = findCycle(layers);
|
|
92
|
+
if (cycle !== void 0) errors.push({
|
|
93
|
+
cycle,
|
|
94
|
+
kind: "cycle"
|
|
95
|
+
});
|
|
96
|
+
return errors;
|
|
97
|
+
}
|
|
98
|
+
/**
|
|
99
|
+
* Split a path relative to the package folder at the root. Undefined when the path is not below the
|
|
100
|
+
* root.
|
|
101
|
+
*/
|
|
102
|
+
function splitAtRoot(root, relative) {
|
|
103
|
+
const segments = relative.split("/");
|
|
104
|
+
for (let length = 0; length < segments.length; length += 1) {
|
|
105
|
+
const folder = segments.slice(0, length).join("/");
|
|
106
|
+
if (root.test(folder)) return {
|
|
107
|
+
below: segments.slice(length),
|
|
108
|
+
root: folder
|
|
109
|
+
};
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
function getUnit(inside) {
|
|
113
|
+
const [first = ""] = inside;
|
|
114
|
+
return inside.length === 1 ? first.replace(/\.[^.]+$/, "") : first;
|
|
115
|
+
}
|
|
116
|
+
function locate(options, root, below) {
|
|
117
|
+
const path = below.join("/");
|
|
118
|
+
if (options.entries.some((pattern) => pattern.test(path))) return {
|
|
119
|
+
kind: "entry",
|
|
120
|
+
path
|
|
121
|
+
};
|
|
122
|
+
const [top = "", ...inside] = below;
|
|
123
|
+
if (inside.length === 0) return {
|
|
124
|
+
kind: "unmappedFile",
|
|
125
|
+
path: posix.join(root, path)
|
|
126
|
+
};
|
|
127
|
+
if (!Object.hasOwn(options.layers, top)) return {
|
|
128
|
+
folder: top,
|
|
129
|
+
kind: "unmappedFolder",
|
|
130
|
+
path: posix.join(root, top)
|
|
131
|
+
};
|
|
132
|
+
return {
|
|
133
|
+
kind: "layer",
|
|
134
|
+
layer: top,
|
|
135
|
+
unit: options.isolate.has(top) ? getUnit(inside) : void 0
|
|
136
|
+
};
|
|
137
|
+
}
|
|
138
|
+
/**
|
|
139
|
+
* Find the rule that an import from one layer to another breaks.
|
|
140
|
+
*/
|
|
141
|
+
function findViolation(layers, from, to) {
|
|
142
|
+
if (from.layer === to.layer) return from.unit === to.unit ? void 0 : "isolated";
|
|
143
|
+
if (listImportable(layers, from.layer).has(to.layer)) return;
|
|
144
|
+
return listImportable(layers, to.layer).has(from.layer) ? "upward" : "sibling";
|
|
145
|
+
}
|
|
146
|
+
/**
|
|
147
|
+
* Keep the names that no other name imports: the highest layers.
|
|
148
|
+
*/
|
|
149
|
+
function keepHighest(layers, names) {
|
|
150
|
+
return names.filter((name) => !names.some((other) => other !== name && listImportable(layers, other).has(name)));
|
|
151
|
+
}
|
|
152
|
+
/**
|
|
153
|
+
* Keep the names that import no other name: the lowest layers.
|
|
154
|
+
*/
|
|
155
|
+
function keepLowest(layers, names) {
|
|
156
|
+
return names.filter((name) => !names.some((other) => other !== name && listImportable(layers, name).has(other)));
|
|
157
|
+
}
|
|
158
|
+
/**
|
|
159
|
+
* The highest layers that both `from` and `to` import, where code that both need can move. For two
|
|
160
|
+
* units of one layer, the layer itself does not count.
|
|
161
|
+
*/
|
|
162
|
+
function findLowerRepairs(layers, from, to) {
|
|
163
|
+
const fromImportable = listImportable(layers, from);
|
|
164
|
+
const toImportable = listImportable(layers, to);
|
|
165
|
+
return keepHighest(layers, Object.keys(layers).filter((name) => fromImportable.has(name) && toImportable.has(name) && (from !== to || name !== from)));
|
|
166
|
+
}
|
|
167
|
+
/**
|
|
168
|
+
* The lowest layers that import both `from` and `to`, where the importing file can move, in layer
|
|
169
|
+
* map order. For two units of one layer, the layer itself does not count.
|
|
170
|
+
*/
|
|
171
|
+
function findUpperRepairs(layers, from, to) {
|
|
172
|
+
return keepLowest(layers, Object.keys(layers).filter((name) => {
|
|
173
|
+
const importable = listImportable(layers, name);
|
|
174
|
+
return importable.has(from) && importable.has(to) && (from !== to || name !== from);
|
|
175
|
+
}));
|
|
176
|
+
}
|
|
177
|
+
function formatList(names) {
|
|
178
|
+
const quoted = names.map((name) => `\`${name}\``);
|
|
179
|
+
if (quoted.length <= 2) return quoted.join(" or ");
|
|
180
|
+
return `${quoted.slice(0, -1).join(", ")}, or ${quoted.at(-1) ?? ""}`;
|
|
181
|
+
}
|
|
182
|
+
/**
|
|
183
|
+
* The words of each repair, by the rule that the import breaks.
|
|
184
|
+
*/
|
|
185
|
+
const REPAIR_WORDS = {
|
|
186
|
+
isolated: {
|
|
187
|
+
lower: "both need",
|
|
188
|
+
upper: "compose them in"
|
|
189
|
+
},
|
|
190
|
+
sibling: {
|
|
191
|
+
lower: "both need",
|
|
192
|
+
upper: "move this file up to"
|
|
193
|
+
},
|
|
194
|
+
upward: {
|
|
195
|
+
lower: "this file needs",
|
|
196
|
+
upper: "move this file up to"
|
|
197
|
+
}
|
|
198
|
+
};
|
|
199
|
+
/**
|
|
200
|
+
* The repair sentence for a layer violation, with a leading space, or an empty string when the map
|
|
201
|
+
* allows no repair.
|
|
202
|
+
*/
|
|
203
|
+
function describeRepairs(violation, lower, upper) {
|
|
204
|
+
const words = REPAIR_WORDS[violation];
|
|
205
|
+
const text = [...lower.length > 0 ? [`move the code that ${words.lower} down to ${formatList(lower)}`] : [], ...upper.length > 0 ? [`${words.upper} ${formatList(upper)}`] : []].join(", or ");
|
|
206
|
+
return text === "" ? "" : ` ${text.charAt(0).toUpperCase()}${text.slice(1)}.`;
|
|
207
|
+
}
|
|
208
|
+
function describeLayer(location) {
|
|
209
|
+
return location.unit === void 0 ? location.layer : `${location.layer}/${location.unit}`;
|
|
210
|
+
}
|
|
211
|
+
const packages = /* @__PURE__ */ new Map();
|
|
212
|
+
function findPackage(dir) {
|
|
213
|
+
if (packages.has(dir)) return packages.get(dir);
|
|
214
|
+
const parent = posix.dirname(dir);
|
|
215
|
+
let found;
|
|
216
|
+
if (existsSync(posix.join(dir, "package.json"))) found = dir;
|
|
217
|
+
else if (parent !== dir) found = findPackage(parent);
|
|
218
|
+
packages.set(dir, found);
|
|
219
|
+
return found;
|
|
220
|
+
}
|
|
221
|
+
const modules = {
|
|
222
|
+
findPackage: (file) => findPackage(posix.dirname(file)),
|
|
223
|
+
resolve: resolveImport
|
|
224
|
+
};
|
|
225
|
+
const MAP_PROPERTIES = {
|
|
226
|
+
conditions: {
|
|
227
|
+
items: { type: "string" },
|
|
228
|
+
type: "array"
|
|
229
|
+
},
|
|
230
|
+
entries: {
|
|
231
|
+
items: { type: "string" },
|
|
232
|
+
type: "array"
|
|
233
|
+
},
|
|
234
|
+
ignore: {
|
|
235
|
+
items: { type: "string" },
|
|
236
|
+
type: "array"
|
|
237
|
+
},
|
|
238
|
+
ignoreTypes: { type: "boolean" },
|
|
239
|
+
isolate: {
|
|
240
|
+
items: { type: "string" },
|
|
241
|
+
type: "array"
|
|
242
|
+
},
|
|
243
|
+
layers: {
|
|
244
|
+
additionalProperties: { $ref: "#/definitions/layer" },
|
|
245
|
+
type: "object"
|
|
246
|
+
}
|
|
247
|
+
};
|
|
248
|
+
function isTypeOnlyImport(node) {
|
|
249
|
+
return node.importKind === "type" || node.specifiers.length > 0 && node.specifiers.every((specifier) => specifier.type === "ImportSpecifier" && specifier.importKind === "type");
|
|
250
|
+
}
|
|
251
|
+
function isTypeOnlyExport(node) {
|
|
252
|
+
return node.exportKind === "type" || node.specifiers.length > 0 && node.specifiers.every((specifier) => specifier.exportKind === "type");
|
|
253
|
+
}
|
|
254
|
+
function formatCycle(cycle) {
|
|
255
|
+
return cycle.map((name) => `\`${name}\``).join(" → ");
|
|
256
|
+
}
|
|
257
|
+
function reportUnmapped(context, node, location, root, configPath) {
|
|
258
|
+
if (location.kind === "unmappedFolder") context.report({
|
|
259
|
+
data: {
|
|
260
|
+
configPath,
|
|
261
|
+
folder: location.folder,
|
|
262
|
+
path: location.path,
|
|
263
|
+
root
|
|
264
|
+
},
|
|
265
|
+
messageId: "unmappedFolder",
|
|
266
|
+
node
|
|
267
|
+
});
|
|
268
|
+
else context.report({
|
|
269
|
+
data: {
|
|
270
|
+
configPath,
|
|
271
|
+
path: location.path,
|
|
272
|
+
root
|
|
273
|
+
},
|
|
274
|
+
messageId: "unmappedFile",
|
|
275
|
+
node
|
|
276
|
+
});
|
|
277
|
+
}
|
|
278
|
+
/**
|
|
279
|
+
* The message data of a layer map error. The switch has no default, so a new kind of error fails
|
|
280
|
+
* the type check until it has its data.
|
|
281
|
+
*/
|
|
282
|
+
function getMapErrorData(error) {
|
|
283
|
+
switch (error.kind) {
|
|
284
|
+
case "cycle": return { cycle: formatCycle(error.cycle) };
|
|
285
|
+
case "unknownImport": return {
|
|
286
|
+
layer: error.layer,
|
|
287
|
+
name: error.name
|
|
288
|
+
};
|
|
289
|
+
case "unknownIsolate": return { name: error.name };
|
|
290
|
+
}
|
|
291
|
+
}
|
|
292
|
+
function reportFile(context, node, file) {
|
|
293
|
+
const { errors, from } = file;
|
|
294
|
+
const root = file.root || ".";
|
|
295
|
+
const { configPath } = file.options;
|
|
296
|
+
for (const error of errors) context.report({
|
|
297
|
+
data: {
|
|
298
|
+
configPath,
|
|
299
|
+
root,
|
|
300
|
+
...getMapErrorData(error)
|
|
301
|
+
},
|
|
302
|
+
messageId: error.kind,
|
|
303
|
+
node
|
|
304
|
+
});
|
|
305
|
+
if (from.kind === "unmappedFile" || from.kind === "unmappedFolder") reportUnmapped(context, node, from, root, configPath);
|
|
306
|
+
}
|
|
307
|
+
function checkImport(context, file, source, typeOnly) {
|
|
308
|
+
const { from, options } = file;
|
|
309
|
+
const root = file.root || ".";
|
|
310
|
+
if (typeOnly && options.ignoreTypes) return;
|
|
311
|
+
const target = file.modules.resolve(file.importer, source.value, options.conditions);
|
|
312
|
+
const relative = target === void 0 ? void 0 : posix.relative(posix.join(file.packageDir, file.root), target);
|
|
313
|
+
if (relative === void 0 || relative === ".." || relative.startsWith("../") || relative.split("/").includes("node_modules")) return;
|
|
314
|
+
const to = locate(options, file.root, relative.split("/"));
|
|
315
|
+
if (to.kind === "entry") {
|
|
316
|
+
context.report({
|
|
317
|
+
data: {
|
|
318
|
+
configPath: options.configPath,
|
|
319
|
+
from: describeLayer(from),
|
|
320
|
+
root,
|
|
321
|
+
to: to.path
|
|
322
|
+
},
|
|
323
|
+
messageId: "entry",
|
|
324
|
+
node: source
|
|
325
|
+
});
|
|
326
|
+
return;
|
|
327
|
+
}
|
|
328
|
+
if (to.kind !== "layer") {
|
|
329
|
+
reportUnmapped(context, source, to, root, options.configPath);
|
|
330
|
+
return;
|
|
331
|
+
}
|
|
332
|
+
const violation = findViolation(options.layers, from, to);
|
|
333
|
+
if (violation === void 0) return;
|
|
334
|
+
context.report({
|
|
335
|
+
data: {
|
|
336
|
+
configPath: options.configPath,
|
|
337
|
+
from: describeLayer(from),
|
|
338
|
+
layer: from.layer,
|
|
339
|
+
repair: describeRepairs(violation, findLowerRepairs(options.layers, from.layer, to.layer), findUpperRepairs(options.layers, from.layer, to.layer)),
|
|
340
|
+
root,
|
|
341
|
+
to: describeLayer(to)
|
|
342
|
+
},
|
|
343
|
+
messageId: violation,
|
|
344
|
+
node: source
|
|
345
|
+
});
|
|
346
|
+
}
|
|
347
|
+
function createLayersRule(modules) {
|
|
348
|
+
return {
|
|
349
|
+
create(context) {
|
|
350
|
+
const importer = toPosixPath(context.filename);
|
|
351
|
+
const packageDir = modules.findPackage(importer);
|
|
352
|
+
if (packageDir === void 0) return {};
|
|
353
|
+
const relative = posix.relative(packageDir, importer);
|
|
354
|
+
const reported = [];
|
|
355
|
+
const linted = [];
|
|
356
|
+
const active = [];
|
|
357
|
+
function activate(options, root, below) {
|
|
358
|
+
active.push({
|
|
359
|
+
below,
|
|
360
|
+
options,
|
|
361
|
+
root
|
|
362
|
+
});
|
|
363
|
+
const [folder = "", ...inside] = below;
|
|
364
|
+
const child = options.children.get(folder);
|
|
365
|
+
if (child !== void 0 && inside.length > 0) activate(child, posix.join(root, folder), inside);
|
|
366
|
+
}
|
|
367
|
+
for (const options of readLayersOptions(context.options)) {
|
|
368
|
+
const split = splitAtRoot(options.root, relative);
|
|
369
|
+
if (split !== void 0) activate(options, split.root, split.below);
|
|
370
|
+
}
|
|
371
|
+
const declarations = /* @__PURE__ */ new Map();
|
|
372
|
+
for (const { options, root } of active) declarations.set(root, [...declarations.get(root) ?? [], options.configPath]);
|
|
373
|
+
const duplicates = [...declarations].filter(([, paths]) => paths.length > 1);
|
|
374
|
+
const duplicateRoots = new Set(duplicates.map(([root]) => root));
|
|
375
|
+
for (const { below, options, root } of active) {
|
|
376
|
+
if (duplicateRoots.has(root) || options.ignore.some((pattern) => pattern.test(below.join("/")))) continue;
|
|
377
|
+
const from = locate(options, root, below);
|
|
378
|
+
const errors = findMapErrors(options.layers, options.isolate);
|
|
379
|
+
if (errors.length > 0 || from.kind !== "layer") reported.push({
|
|
380
|
+
errors,
|
|
381
|
+
from,
|
|
382
|
+
options,
|
|
383
|
+
root
|
|
384
|
+
});
|
|
385
|
+
else linted.push({
|
|
386
|
+
from,
|
|
387
|
+
importer,
|
|
388
|
+
modules,
|
|
389
|
+
options,
|
|
390
|
+
packageDir,
|
|
391
|
+
root
|
|
392
|
+
});
|
|
393
|
+
}
|
|
394
|
+
if (reported.length === 0 && linted.length === 0 && duplicates.length === 0) return {};
|
|
395
|
+
function check(source, typeOnly) {
|
|
396
|
+
for (const file of linted) checkImport(context, file, source, typeOnly);
|
|
397
|
+
}
|
|
398
|
+
return {
|
|
399
|
+
ExportAllDeclaration(node) {
|
|
400
|
+
check(node.source, node.exportKind === "type");
|
|
401
|
+
},
|
|
402
|
+
ExportNamedDeclaration(node) {
|
|
403
|
+
if (node.source !== null) check(node.source, isTypeOnlyExport(node));
|
|
404
|
+
},
|
|
405
|
+
ImportDeclaration(node) {
|
|
406
|
+
check(node.source, isTypeOnlyImport(node));
|
|
407
|
+
},
|
|
408
|
+
ImportExpression(node) {
|
|
409
|
+
if (isStringLiteral(node.source)) check(node.source, false);
|
|
410
|
+
},
|
|
411
|
+
Program(node) {
|
|
412
|
+
for (const [root, paths] of duplicates) context.report({
|
|
413
|
+
data: {
|
|
414
|
+
declarations: paths.map((path) => `\`${path}\``).join(" and "),
|
|
415
|
+
root: root || "."
|
|
416
|
+
},
|
|
417
|
+
messageId: "duplicateMap",
|
|
418
|
+
node
|
|
419
|
+
});
|
|
420
|
+
for (const file of reported) reportFile(context, node, file);
|
|
421
|
+
},
|
|
422
|
+
TSImportType(node) {
|
|
423
|
+
check(node.source, true);
|
|
424
|
+
}
|
|
425
|
+
};
|
|
426
|
+
},
|
|
427
|
+
meta: {
|
|
428
|
+
docs: { description: "Enforce one-way imports between the layers of a folder, from one or more layer maps in the rule options." },
|
|
429
|
+
messages: {
|
|
430
|
+
cycle: `The layer map has a cycle: {{cycle}}. Layers import in one direction. ${LAYER_MAP_HINT}`,
|
|
431
|
+
duplicateMap: "Root `{{root}}` has more than one layer map: {{declarations}}. Keep one declaration for this scope.",
|
|
432
|
+
entry: `\`{{from}}\` cannot import the entry \`{{to}}\`: no layer imports an entry. Move the code that it needs into a layer. ${LAYER_MAP_HINT}`,
|
|
433
|
+
isolated: `\`{{from}}\` cannot import \`{{to}}\`: units in \`{{layer}}\` do not import each other.{{repair}} ${LAYER_MAP_HINT}`,
|
|
434
|
+
sibling: `\`{{from}}\` cannot import \`{{to}}\`: they are siblings.{{repair}} ${LAYER_MAP_HINT}`,
|
|
435
|
+
unknownImport: `\`{{layer}}\` imports \`{{name}}\`, which is not a layer. ${LAYER_MAP_HINT}`,
|
|
436
|
+
unknownIsolate: `\`isolate\` lists \`{{name}}\`, which is not a layer. ${LAYER_MAP_HINT}`,
|
|
437
|
+
unmappedFile: `\`{{path}}\` is in no layer. Move it into a layer folder, or list it in \`entries\`. ${LAYER_MAP_HINT}`,
|
|
438
|
+
unmappedFolder: `\`{{path}}\` is in no layer. Add \`{{folder}}\` to the layer map, with the layers that it imports. ${LAYER_MAP_HINT}`,
|
|
439
|
+
upward: `\`{{from}}\` cannot import \`{{to}}\`: it is a higher layer.{{repair}} ${LAYER_MAP_HINT}`
|
|
440
|
+
},
|
|
441
|
+
schema: {
|
|
442
|
+
definitions: { layer: { oneOf: [{
|
|
443
|
+
additionalProperties: false,
|
|
444
|
+
properties: { imports: {
|
|
445
|
+
items: { type: "string" },
|
|
446
|
+
type: "array"
|
|
447
|
+
} },
|
|
448
|
+
required: ["imports"],
|
|
449
|
+
type: "object"
|
|
450
|
+
}, {
|
|
451
|
+
additionalProperties: false,
|
|
452
|
+
properties: {
|
|
453
|
+
...MAP_PROPERTIES,
|
|
454
|
+
imports: {
|
|
455
|
+
items: { type: "string" },
|
|
456
|
+
type: "array"
|
|
457
|
+
}
|
|
458
|
+
},
|
|
459
|
+
required: ["imports", "layers"],
|
|
460
|
+
type: "object"
|
|
461
|
+
}] } },
|
|
462
|
+
items: {
|
|
463
|
+
additionalProperties: false,
|
|
464
|
+
properties: {
|
|
465
|
+
...MAP_PROPERTIES,
|
|
466
|
+
root: { type: "string" }
|
|
467
|
+
},
|
|
468
|
+
required: ["layers", "root"],
|
|
469
|
+
type: "object"
|
|
470
|
+
},
|
|
471
|
+
type: "array"
|
|
472
|
+
},
|
|
473
|
+
type: "problem"
|
|
474
|
+
}
|
|
475
|
+
};
|
|
476
|
+
}
|
|
477
|
+
const rule = createLayersRule(modules);
|
|
478
|
+
//#endregion
|
|
479
|
+
export { createLayersRule, rule as default, describeRepairs, findLowerRepairs, findMapErrors, findUpperRepairs, findViolation, listImportable, locate, readLayersOptions, splitAtRoot };
|
|
@@ -76,7 +76,7 @@ const rule = {
|
|
|
76
76
|
},
|
|
77
77
|
meta: {
|
|
78
78
|
docs: { description: "Disallow copying TanStack Query data into React state, where the copy stops updating when the query refetches." },
|
|
79
|
-
messages: { copiedData: "`{{hook}}` copies query data into local state
|
|
79
|
+
messages: { copiedData: "`{{hook}}` copies query data into local state. The copy can become stale when the query changes. Read values from the query result where you use them. Keep only the user's edits in local state, then merge them with the query data during render." },
|
|
80
80
|
schema: [],
|
|
81
81
|
type: "problem"
|
|
82
82
|
}
|
|
@@ -1,10 +1,5 @@
|
|
|
1
1
|
import { Rule } from "@oxlint/plugins";
|
|
2
2
|
//#region presets/lint/plugin/rules/no-react-state-hooks.d.ts
|
|
3
|
-
/**
|
|
4
|
-
* Convert a glob to a regular expression that matches a whole path with forward slashes. Supports
|
|
5
|
-
* `**`, `*`, `?`, `[a-z]` and `[!a-z]` character classes, and `{a,b}` alternation.
|
|
6
|
-
*/
|
|
7
|
-
export declare function globToRegExp(glob: string): RegExp;
|
|
8
3
|
declare const rule: Rule;
|
|
9
4
|
//#endregion
|
|
10
5
|
export { rule as default };
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { globToRegExp } from "../glob.js";
|
|
1
2
|
import { findImportedCalls } from "../imports.js";
|
|
2
3
|
import { readStringArrayOption } from "../options.js";
|
|
3
4
|
//#region presets/lint/plugin/rules/no-react-state-hooks.ts
|
|
@@ -22,51 +23,6 @@ const DEFAULT_ALLOW = [
|
|
|
22
23
|
"**/hooks/**"
|
|
23
24
|
];
|
|
24
25
|
const REACT_MODULE = "react";
|
|
25
|
-
function escapeRegExp(text) {
|
|
26
|
-
return text.replaceAll(/[$()*+.?[\\\]^{|}]/g, String.raw`\$&`);
|
|
27
|
-
}
|
|
28
|
-
/**
|
|
29
|
-
* Convert a glob to a regular expression that matches a whole path with forward slashes. Supports
|
|
30
|
-
* `**`, `*`, `?`, `[a-z]` and `[!a-z]` character classes, and `{a,b}` alternation.
|
|
31
|
-
*/
|
|
32
|
-
function globToRegExp(glob) {
|
|
33
|
-
let source = "";
|
|
34
|
-
let index = 0;
|
|
35
|
-
while (index < glob.length) {
|
|
36
|
-
const rest = glob.slice(index);
|
|
37
|
-
if (rest.startsWith("**/")) {
|
|
38
|
-
source += "(?:.*/)?";
|
|
39
|
-
index += 3;
|
|
40
|
-
} else if (rest === "/**") {
|
|
41
|
-
source += "(?:/.*)?";
|
|
42
|
-
index += 3;
|
|
43
|
-
} else if (rest.startsWith("**")) {
|
|
44
|
-
source += ".*";
|
|
45
|
-
index += 2;
|
|
46
|
-
} else if (rest.startsWith("*")) {
|
|
47
|
-
source += "[^/]*";
|
|
48
|
-
index += 1;
|
|
49
|
-
} else if (rest.startsWith("?")) {
|
|
50
|
-
source += "[^/]";
|
|
51
|
-
index += 1;
|
|
52
|
-
} else if (rest.startsWith("[") && rest.indexOf("]") > 1) {
|
|
53
|
-
const end = rest.indexOf("]");
|
|
54
|
-
const negated = rest.startsWith("[!");
|
|
55
|
-
const characters = rest.slice(negated ? 2 : 1, end).replaceAll(/[\\^]/g, String.raw`\$&`);
|
|
56
|
-
source += `[${negated ? "^" : ""}${characters}]`;
|
|
57
|
-
index += end + 1;
|
|
58
|
-
} else if (rest.startsWith("{") && rest.includes("}")) {
|
|
59
|
-
const end = rest.indexOf("}");
|
|
60
|
-
const alternatives = rest.slice(1, end).split(",").map((alternative) => escapeRegExp(alternative));
|
|
61
|
-
source += `(?:${alternatives.join("|")})`;
|
|
62
|
-
index += end + 1;
|
|
63
|
-
} else {
|
|
64
|
-
source += escapeRegExp(glob.charAt(index));
|
|
65
|
-
index += 1;
|
|
66
|
-
}
|
|
67
|
-
}
|
|
68
|
-
return new RegExp(`^${source}$`);
|
|
69
|
-
}
|
|
70
26
|
function toPosixPath(path) {
|
|
71
27
|
return path.replaceAll("\\", "/");
|
|
72
28
|
}
|
|
@@ -105,7 +61,7 @@ const rule = {
|
|
|
105
61
|
effect: "`{{hook}}` runs a side effect in feature code. Move the side effect to a loader, an action, or an event handler.",
|
|
106
62
|
externalStore: "`{{hook}}` subscribes to an external store in feature code. Subscribe through a query or a store abstraction instead.",
|
|
107
63
|
localState: "`{{hook}}` keeps local state in feature code. Derive the value from loader data or URL params instead.",
|
|
108
|
-
memoization: "`{{hook}}` memoizes
|
|
64
|
+
memoization: "`{{hook}}` memoizes in feature code. Remove it or move it into a hook module."
|
|
109
65
|
},
|
|
110
66
|
schema: [{
|
|
111
67
|
additionalProperties: false,
|
|
@@ -125,4 +81,4 @@ const rule = {
|
|
|
125
81
|
}
|
|
126
82
|
};
|
|
127
83
|
//#endregion
|
|
128
|
-
export { rule as default
|
|
84
|
+
export { rule as default };
|
|
@@ -99,8 +99,8 @@ const rule = {
|
|
|
99
99
|
meta: {
|
|
100
100
|
docs: { description: "Require TanStack Query hooks in route files to take options defined once with `queryOptions()` or `infiniteQueryOptions()`, so the route loader can preload the same query." },
|
|
101
101
|
messages: {
|
|
102
|
-
localOptions: "`{{hook}}` takes query options created inside a function
|
|
103
|
-
plainObject: "`{{hook}}` takes a plain options object
|
|
102
|
+
localOptions: "`{{hook}}` takes query options created inside a function. The route loader cannot preload them. Define `{{factory}}()` at module scope or in an exported factory such as `userQuery(id)`. Preload those options in the route loader with `queryClient.{{preload}}()`, and pass them to this hook.",
|
|
103
|
+
plainObject: "`{{hook}}` takes a plain options object. Define the options once with `{{factory}}()` at module scope or in a shared module. Preload them in the route loader with `queryClient.{{preload}}()`, and pass the same options to this hook."
|
|
104
104
|
},
|
|
105
105
|
schema: [],
|
|
106
106
|
type: "suggestion"
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
//#region presets/lint/plugin/rules/require-suppression-reason.ts
|
|
2
|
+
/**
|
|
3
|
+
* A comment that turns off lint rules: `oxlint-disable`, `eslint-disable`, and their `-line` and
|
|
4
|
+
* `-next-line` forms. An `-enable` comment turns rules back on, so it needs no reason.
|
|
5
|
+
*/
|
|
6
|
+
const SUPPRESSION = /^\s*(?:eslint|oxlint)-disable(?:-line|-next-line)?(?=\s|$)/u;
|
|
7
|
+
/**
|
|
8
|
+
* The reason follows two or more dashes with white space on both sides, as ESLint and Oxlint parse
|
|
9
|
+
* a directive description.
|
|
10
|
+
*/
|
|
11
|
+
const REASON = /\s-{2,}\s(.*)$/su;
|
|
12
|
+
const rule = {
|
|
13
|
+
create(context) {
|
|
14
|
+
return { Program() {
|
|
15
|
+
for (const comment of context.sourceCode.getAllComments()) {
|
|
16
|
+
if (!SUPPRESSION.test(comment.value)) continue;
|
|
17
|
+
if (!REASON.exec(comment.value)?.[1]?.trim()) context.report({
|
|
18
|
+
messageId: "missingReason",
|
|
19
|
+
node: comment
|
|
20
|
+
});
|
|
21
|
+
}
|
|
22
|
+
} };
|
|
23
|
+
},
|
|
24
|
+
meta: {
|
|
25
|
+
docs: { description: "Require a reason after ` -- ` on each comment that turns off lint rules." },
|
|
26
|
+
messages: { missingReason: "Give this suppression a reason after ` -- `, such as `// oxlint-disable-next-line no-console -- the CLI writes its report to stdout`. Fix the code instead if no reason applies." },
|
|
27
|
+
schema: [],
|
|
28
|
+
type: "suggestion"
|
|
29
|
+
}
|
|
30
|
+
};
|
|
31
|
+
//#endregion
|
|
32
|
+
export { rule as default };
|
|
@@ -1,11 +1,31 @@
|
|
|
1
1
|
//#region presets/lint/react-strict.ts
|
|
2
2
|
const PLUGIN_EXTENSION = import.meta.url.endsWith(".ts") ? "ts" : "js";
|
|
3
3
|
const config = {
|
|
4
|
-
jsPlugins: [{
|
|
4
|
+
jsPlugins: ["oxlint-plugin-react-doctor", {
|
|
5
5
|
name: "adamantite",
|
|
6
6
|
specifier: new URL(`plugin/index.${PLUGIN_EXTENSION}`, import.meta.url).href
|
|
7
7
|
}],
|
|
8
|
-
rules: {
|
|
8
|
+
rules: {
|
|
9
|
+
"adamantite/no-react-state-hooks": "error",
|
|
10
|
+
"react-doctor/no-create-store-in-render": "error",
|
|
11
|
+
"react-doctor/no-direct-state-mutation": "error",
|
|
12
|
+
"react-doctor/no-eager-new-in-use-state-initializer": "error",
|
|
13
|
+
"react-doctor/no-impure-state-updater": "error",
|
|
14
|
+
"react-doctor/no-loading-flag-reset-outside-finally": "error",
|
|
15
|
+
"react-doctor/no-mutating-reducer-state": "error",
|
|
16
|
+
"react-doctor/no-create-context-in-render": "error",
|
|
17
|
+
"react-doctor/no-create-object-url-in-render": "error",
|
|
18
|
+
"react-doctor/debounce-no-cleanup": "error",
|
|
19
|
+
"react-doctor/effect-needs-cleanup": "error",
|
|
20
|
+
"react-doctor/effect-raf-loop-needs-cancel": "error",
|
|
21
|
+
"react-doctor/no-effect-event-handler": "error",
|
|
22
|
+
"react-doctor/no-effect-wrapper-discards-callback-cleanup-return": "error",
|
|
23
|
+
"react-doctor/no-event-trigger-state": "error",
|
|
24
|
+
"react-doctor/no-fetch-in-effect": "error",
|
|
25
|
+
"react-doctor/no-pass-live-state-to-parent": "error",
|
|
26
|
+
"react-doctor/no-set-state-after-await-in-effect": "error",
|
|
27
|
+
"react-doctor/no-stale-timer-ref": "error"
|
|
28
|
+
}
|
|
9
29
|
};
|
|
10
30
|
//#endregion
|
|
11
31
|
export { config as default };
|