adamantite 0.44.0 → 0.46.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 +413 -122
- package/dist/index.js +638 -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 +0 -1
- package/dist/presets/lint/effect.js +26 -1
- package/dist/presets/lint/jest.d.ts +13 -0
- package/dist/presets/lint/jest.js +88 -68
- 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 +940 -31
- 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/vitest.d.ts +13 -0
- package/dist/presets/lint/vitest.js +111 -91
- 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/runtime-CVZPDpfE.js +11 -0
- package/package.json +79 -18
- package/skills/adamantite/SKILL.md +112 -200
- package/skills/adamantite/references/custom-rules.md +120 -0
- package/skills/adamantite/references/lint-rules.md +158 -0
- package/skills/adamantite/references/setup.md +64 -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,67 @@
|
|
|
1
|
+
import { Options, Rule } from "@oxlint/plugins";
|
|
2
|
+
//#region presets/lint/plugin/rules/import-owners.d.ts
|
|
3
|
+
/**
|
|
4
|
+
* One import and its owners: one option object of the rule.
|
|
5
|
+
*/
|
|
6
|
+
export interface ImportOwner {
|
|
7
|
+
/**
|
|
8
|
+
* The module as the import states it, such as `giget`. It also covers the module's subpaths, such
|
|
9
|
+
* as `zod/mini` for `zod`.
|
|
10
|
+
*/
|
|
11
|
+
readonly import: string;
|
|
12
|
+
/**
|
|
13
|
+
* How the option names the module, which sets how the rule matches an import of it.
|
|
14
|
+
*/
|
|
15
|
+
readonly kind: ModuleKind;
|
|
16
|
+
/**
|
|
17
|
+
* The exports that the owners own. Undefined when they own the whole module.
|
|
18
|
+
*/
|
|
19
|
+
readonly names: ReadonlySet<string> | undefined;
|
|
20
|
+
/**
|
|
21
|
+
* The owner files and folders as the option states them, which messages name.
|
|
22
|
+
*/
|
|
23
|
+
readonly owners: readonly string[];
|
|
24
|
+
/**
|
|
25
|
+
* A pattern for each owner that matches the owner file, or each file below the owner folder.
|
|
26
|
+
*/
|
|
27
|
+
readonly patterns: readonly RegExp[];
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* How an option names its module, by the start of `import`:
|
|
31
|
+
*
|
|
32
|
+
* - `package`, such as `giget`: the rule matches the import as written.
|
|
33
|
+
* - `alias`, such as `#lib/db`: a module of the project. The rule matches the import as written, and
|
|
34
|
+
* also any import that resolves to the module, such as a relative path. The alias resolves from
|
|
35
|
+
* the root, so the text match keeps an alias that a workspace package declares.
|
|
36
|
+
* - `path`, such as `./src/lib/db` or `/src/lib/db`: a module of the project that the rule matches
|
|
37
|
+
* only by the resolved file, because the text of a relative import depends on its file.
|
|
38
|
+
*/
|
|
39
|
+
export type ModuleKind = "alias" | "package" | "path";
|
|
40
|
+
/**
|
|
41
|
+
* What the rule reads from the file system. The plugin reads real files, and unit tests pass fakes.
|
|
42
|
+
*/
|
|
43
|
+
export interface Project {
|
|
44
|
+
/**
|
|
45
|
+
* The folder that owner paths are relative to: the root of the workspace that holds `file`, or
|
|
46
|
+
* the nearest folder above `file` that holds a `package.json` when no workspace holds it.
|
|
47
|
+
*/
|
|
48
|
+
readonly findRoot: (file: string) => string | undefined;
|
|
49
|
+
/**
|
|
50
|
+
* The file that `specifier` in `importer` resolves to, with `/` between folders, or undefined
|
|
51
|
+
* when it does not resolve.
|
|
52
|
+
*/
|
|
53
|
+
readonly resolve: (importer: string, specifier: string) => string | undefined;
|
|
54
|
+
}
|
|
55
|
+
/**
|
|
56
|
+
* Read the rule options: one import and its owners for each option object. An object without
|
|
57
|
+
* `import` or `owners` is left out.
|
|
58
|
+
*/
|
|
59
|
+
export declare function readImportOwners(options: Readonly<Options>): ImportOwner[];
|
|
60
|
+
/**
|
|
61
|
+
* Whether `specifier` imports `module` or one of its subpaths.
|
|
62
|
+
*/
|
|
63
|
+
export declare function isImportOf(module: string, specifier: string): boolean;
|
|
64
|
+
export declare function createImportOwnersRule(project: Project): Rule;
|
|
65
|
+
declare const rule: Rule;
|
|
66
|
+
//#endregion
|
|
67
|
+
export { rule as default };
|
|
@@ -0,0 +1,310 @@
|
|
|
1
|
+
import { globToRegExp } from "../glob.js";
|
|
2
|
+
import { isStringLiteral } from "../imports.js";
|
|
3
|
+
import { readStringArrayOption, readStringOption } from "../options.js";
|
|
4
|
+
import { resolveImport } from "../resolver.js";
|
|
5
|
+
import { existsSync, readFileSync } from "node:fs";
|
|
6
|
+
import { posix } from "node:path";
|
|
7
|
+
//#region presets/lint/plugin/rules/import-owners.ts
|
|
8
|
+
/**
|
|
9
|
+
* Whether an import of each kind of module matches by its text, and by its resolved file.
|
|
10
|
+
*/
|
|
11
|
+
const MODULE_MATCHES = {
|
|
12
|
+
alias: {
|
|
13
|
+
file: true,
|
|
14
|
+
text: true
|
|
15
|
+
},
|
|
16
|
+
package: {
|
|
17
|
+
file: false,
|
|
18
|
+
text: true
|
|
19
|
+
},
|
|
20
|
+
path: {
|
|
21
|
+
file: true,
|
|
22
|
+
text: false
|
|
23
|
+
}
|
|
24
|
+
};
|
|
25
|
+
function readModuleKind(specifier) {
|
|
26
|
+
if (specifier.startsWith("#")) return "alias";
|
|
27
|
+
return /^[./]/.test(specifier) ? "path" : "package";
|
|
28
|
+
}
|
|
29
|
+
const OWNERS_HINT = "Owners: the `adamantite/import-owners` options in the Oxlint config.";
|
|
30
|
+
function toPosixPath(path) {
|
|
31
|
+
return path.replaceAll("\\", "/");
|
|
32
|
+
}
|
|
33
|
+
function normalizeOwner(owner) {
|
|
34
|
+
return owner.split("/").filter((segment) => segment !== "" && segment !== ".").join("/");
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* Read one import and its owners from one option object. Undefined when `import` or `owners` is
|
|
38
|
+
* missing.
|
|
39
|
+
*/
|
|
40
|
+
function readImportOwner(option) {
|
|
41
|
+
const options = [option];
|
|
42
|
+
const specifier = readStringOption(options, "import");
|
|
43
|
+
const owners = readStringArrayOption(options, "owners");
|
|
44
|
+
const names = readStringArrayOption(options, "names");
|
|
45
|
+
if (specifier === void 0 || owners === void 0) return;
|
|
46
|
+
return {
|
|
47
|
+
import: specifier,
|
|
48
|
+
kind: readModuleKind(specifier),
|
|
49
|
+
names: names === void 0 ? void 0 : new Set(names),
|
|
50
|
+
owners,
|
|
51
|
+
patterns: owners.flatMap((owner) => [globToRegExp(normalizeOwner(owner)), globToRegExp(`${normalizeOwner(owner)}/**`)])
|
|
52
|
+
};
|
|
53
|
+
}
|
|
54
|
+
/**
|
|
55
|
+
* Read the rule options: one import and its owners for each option object. An object without
|
|
56
|
+
* `import` or `owners` is left out.
|
|
57
|
+
*/
|
|
58
|
+
function readImportOwners(options) {
|
|
59
|
+
return options.flatMap((option) => readImportOwner(option) ?? []);
|
|
60
|
+
}
|
|
61
|
+
/**
|
|
62
|
+
* Whether `specifier` imports `module` or one of its subpaths.
|
|
63
|
+
*/
|
|
64
|
+
function isImportOf(module, specifier) {
|
|
65
|
+
return specifier === module || specifier.startsWith(`${module}/`);
|
|
66
|
+
}
|
|
67
|
+
function formatList(names) {
|
|
68
|
+
const quoted = names.map((name) => `\`${name}\``);
|
|
69
|
+
if (quoted.length <= 2) return quoted.join(" or ");
|
|
70
|
+
return `${quoted.slice(0, -1).join(", ")}, or ${quoted.at(-1) ?? ""}`;
|
|
71
|
+
}
|
|
72
|
+
function getExportName(name) {
|
|
73
|
+
return name.type === "Literal" ? name.value : name.name;
|
|
74
|
+
}
|
|
75
|
+
function hasWorkspacesField(packageJson) {
|
|
76
|
+
try {
|
|
77
|
+
const manifest = JSON.parse(readFileSync(packageJson, "utf8"));
|
|
78
|
+
return manifest instanceof Object && Object.hasOwn(manifest, "workspaces");
|
|
79
|
+
} catch {
|
|
80
|
+
return false;
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
/**
|
|
84
|
+
* Whether `dir` lists workspace packages. A `pnpm-workspace.yaml` without a `packages` key only
|
|
85
|
+
* holds settings.
|
|
86
|
+
*/
|
|
87
|
+
function listsWorkspacePackages(dir) {
|
|
88
|
+
const pnpmWorkspace = posix.join(dir, "pnpm-workspace.yaml");
|
|
89
|
+
if (existsSync(pnpmWorkspace) && /^packages\s*:/m.test(readFileSync(pnpmWorkspace, "utf8"))) return true;
|
|
90
|
+
const packageJson = posix.join(dir, "package.json");
|
|
91
|
+
return existsSync(packageJson) && hasWorkspacesField(packageJson);
|
|
92
|
+
}
|
|
93
|
+
const workspaces = /* @__PURE__ */ new Map();
|
|
94
|
+
/**
|
|
95
|
+
* The nearest folder at or above `dir` that lists workspace packages, in `pnpm-workspace.yaml` or
|
|
96
|
+
* in the `workspaces` field of `package.json`.
|
|
97
|
+
*/
|
|
98
|
+
function findWorkspace(dir) {
|
|
99
|
+
if (workspaces.has(dir)) return workspaces.get(dir);
|
|
100
|
+
const parent = posix.dirname(dir);
|
|
101
|
+
let found;
|
|
102
|
+
if (listsWorkspacePackages(dir)) found = dir;
|
|
103
|
+
else if (parent !== dir) found = findWorkspace(parent);
|
|
104
|
+
workspaces.set(dir, found);
|
|
105
|
+
return found;
|
|
106
|
+
}
|
|
107
|
+
function findPackage(dir) {
|
|
108
|
+
if (existsSync(posix.join(dir, "package.json"))) return dir;
|
|
109
|
+
const parent = posix.dirname(dir);
|
|
110
|
+
return parent === dir ? void 0 : findPackage(parent);
|
|
111
|
+
}
|
|
112
|
+
const project = {
|
|
113
|
+
findRoot: (file) => findWorkspace(posix.dirname(file)) ?? findPackage(posix.dirname(file)),
|
|
114
|
+
resolve: resolveImport
|
|
115
|
+
};
|
|
116
|
+
/**
|
|
117
|
+
* The names that an alias can give the file at `path`: the path itself, the path without its
|
|
118
|
+
* extension for an alias such as `"#lib/*": "./src/lib/*.ts"`, and the folder of an index file for
|
|
119
|
+
* an alias that adds `/index.ts` after the name. A declaration file such as `rows.d.ts` has two
|
|
120
|
+
* names without its extension: `rows.d` for an alias that adds `.ts`, and `rows` for an alias that
|
|
121
|
+
* adds `.d.ts`.
|
|
122
|
+
*/
|
|
123
|
+
function listAliasNames(path) {
|
|
124
|
+
const bareNames = [path.replace(/\.[^./]+$/, ""), path.replace(/\.d\.[^./]+$/, "")];
|
|
125
|
+
const folders = bareNames.flatMap((name) => posix.basename(name) === "index" ? [posix.dirname(name)] : []);
|
|
126
|
+
return [.../* @__PURE__ */ new Set([
|
|
127
|
+
path,
|
|
128
|
+
...bareNames,
|
|
129
|
+
...folders
|
|
130
|
+
])].filter((name) => name !== ".");
|
|
131
|
+
}
|
|
132
|
+
function createImportOwnersRule(project) {
|
|
133
|
+
const coverage = /* @__PURE__ */ new Map();
|
|
134
|
+
/**
|
|
135
|
+
* Whether `file` is the project module `entry.import`, or a file below it when the module is a
|
|
136
|
+
* folder. The module resolves from the root, as owner paths do. A folder resolves only through a
|
|
137
|
+
* file in it, so each tail of the file's path is tried below the module, in each name that an
|
|
138
|
+
* alias can give it.
|
|
139
|
+
*/
|
|
140
|
+
function covers(entry, root, file) {
|
|
141
|
+
const key = [
|
|
142
|
+
entry.import,
|
|
143
|
+
root,
|
|
144
|
+
file
|
|
145
|
+
].join("\0");
|
|
146
|
+
const known = coverage.get(key);
|
|
147
|
+
if (known !== void 0) return known;
|
|
148
|
+
const anchor = posix.join(root, "package.json");
|
|
149
|
+
const segments = posix.relative(root, file).split("/");
|
|
150
|
+
const tails = segments.flatMap((_, index) => listAliasNames(segments.slice(index).join("/")));
|
|
151
|
+
const result = project.resolve(anchor, entry.import) === file || tails.some((tail) => project.resolve(anchor, `${entry.import}/${tail}`) === file);
|
|
152
|
+
coverage.set(key, result);
|
|
153
|
+
return result;
|
|
154
|
+
}
|
|
155
|
+
return {
|
|
156
|
+
create(context) {
|
|
157
|
+
const importer = toPosixPath(context.filename);
|
|
158
|
+
const root = project.findRoot(importer);
|
|
159
|
+
if (root === void 0) return {};
|
|
160
|
+
const projectRoot = root;
|
|
161
|
+
const relative = posix.relative(root, importer);
|
|
162
|
+
const restricted = readImportOwners(context.options).filter((entry) => !entry.patterns.some((pattern) => pattern.test(relative)));
|
|
163
|
+
if (restricted.length === 0) return {};
|
|
164
|
+
function reportModule(entry, node) {
|
|
165
|
+
context.report({
|
|
166
|
+
data: {
|
|
167
|
+
import: entry.import,
|
|
168
|
+
owners: formatList(entry.owners)
|
|
169
|
+
},
|
|
170
|
+
messageId: "module",
|
|
171
|
+
node
|
|
172
|
+
});
|
|
173
|
+
}
|
|
174
|
+
function reportName(entry, node, name) {
|
|
175
|
+
context.report({
|
|
176
|
+
data: {
|
|
177
|
+
import: entry.import,
|
|
178
|
+
name,
|
|
179
|
+
owners: formatList(entry.owners)
|
|
180
|
+
},
|
|
181
|
+
messageId: "name",
|
|
182
|
+
node
|
|
183
|
+
});
|
|
184
|
+
}
|
|
185
|
+
function reportNamespace(entry, node, names) {
|
|
186
|
+
context.report({
|
|
187
|
+
data: {
|
|
188
|
+
import: entry.import,
|
|
189
|
+
names: formatList([...names]),
|
|
190
|
+
owners: formatList(entry.owners)
|
|
191
|
+
},
|
|
192
|
+
messageId: "namespace",
|
|
193
|
+
node
|
|
194
|
+
});
|
|
195
|
+
}
|
|
196
|
+
const matchesFile = restricted.some((entry) => MODULE_MATCHES[entry.kind].file);
|
|
197
|
+
function findRestricted(source) {
|
|
198
|
+
const resolved = matchesFile ? project.resolve(importer, source.value) : void 0;
|
|
199
|
+
const file = resolved?.includes("/node_modules/") === false ? resolved : void 0;
|
|
200
|
+
return restricted.filter((entry) => {
|
|
201
|
+
const matches = MODULE_MATCHES[entry.kind];
|
|
202
|
+
return matches.text && isImportOf(entry.import, source.value) || matches.file && file !== void 0 && covers(entry, projectRoot, file);
|
|
203
|
+
});
|
|
204
|
+
}
|
|
205
|
+
/**
|
|
206
|
+
* Check an import that reaches every export of the module, such as `import()`. A plain
|
|
207
|
+
* `export * from` reaches every export except the default export.
|
|
208
|
+
*/
|
|
209
|
+
function checkWhole(source, withDefault = true) {
|
|
210
|
+
for (const entry of findRestricted(source)) {
|
|
211
|
+
if (entry.names === void 0) {
|
|
212
|
+
reportModule(entry, source);
|
|
213
|
+
continue;
|
|
214
|
+
}
|
|
215
|
+
const reached = new Set([...entry.names].filter((name) => withDefault || name !== "default"));
|
|
216
|
+
if (reached.size > 0) reportNamespace(entry, source, reached);
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
/**
|
|
220
|
+
* Check a type import, such as `import("react-native").Alert`. With a qualifier it reaches
|
|
221
|
+
* only the export that the qualifier names first.
|
|
222
|
+
*/
|
|
223
|
+
function checkImportType(node) {
|
|
224
|
+
if (node.qualifier === null) {
|
|
225
|
+
checkWhole(node.source);
|
|
226
|
+
return;
|
|
227
|
+
}
|
|
228
|
+
let first = node.qualifier;
|
|
229
|
+
while (first.type === "TSQualifiedName") first = first.left;
|
|
230
|
+
for (const entry of findRestricted(node.source)) if (entry.names === void 0) reportModule(entry, node.source);
|
|
231
|
+
else if (entry.names.has(first.name)) reportName(entry, node.qualifier, first.name);
|
|
232
|
+
}
|
|
233
|
+
function checkImport(node) {
|
|
234
|
+
for (const entry of findRestricted(node.source)) {
|
|
235
|
+
const { names } = entry;
|
|
236
|
+
if (names === void 0) {
|
|
237
|
+
reportModule(entry, node.source);
|
|
238
|
+
continue;
|
|
239
|
+
}
|
|
240
|
+
for (const specifier of node.specifiers) if (specifier.type === "ImportNamespaceSpecifier") reportNamespace(entry, specifier, names);
|
|
241
|
+
else if (specifier.type === "ImportDefaultSpecifier") {
|
|
242
|
+
if (names.has("default")) reportName(entry, specifier, "default");
|
|
243
|
+
} else if (names.has(getExportName(specifier.imported))) reportName(entry, specifier, getExportName(specifier.imported));
|
|
244
|
+
}
|
|
245
|
+
}
|
|
246
|
+
function checkExport(node, source) {
|
|
247
|
+
for (const entry of findRestricted(source)) {
|
|
248
|
+
const { names } = entry;
|
|
249
|
+
if (names === void 0) {
|
|
250
|
+
reportModule(entry, source);
|
|
251
|
+
continue;
|
|
252
|
+
}
|
|
253
|
+
for (const specifier of node.specifiers) if (names.has(getExportName(specifier.local))) reportName(entry, specifier, getExportName(specifier.local));
|
|
254
|
+
}
|
|
255
|
+
}
|
|
256
|
+
return {
|
|
257
|
+
ExportAllDeclaration(node) {
|
|
258
|
+
checkWhole(node.source, node.exported !== null);
|
|
259
|
+
},
|
|
260
|
+
ExportNamedDeclaration(node) {
|
|
261
|
+
if (node.source !== null) checkExport(node, node.source);
|
|
262
|
+
},
|
|
263
|
+
ImportDeclaration: checkImport,
|
|
264
|
+
ImportExpression(node) {
|
|
265
|
+
if (isStringLiteral(node.source)) checkWhole(node.source);
|
|
266
|
+
},
|
|
267
|
+
TSImportType: checkImportType
|
|
268
|
+
};
|
|
269
|
+
},
|
|
270
|
+
meta: {
|
|
271
|
+
docs: { description: "Restrict an import to the files and folders that own it, from the owners in the rule options." },
|
|
272
|
+
messages: {
|
|
273
|
+
module: `Only {{owners}} may import \`{{import}}\`. Use what the owner exports, or move this code into the owner. ${OWNERS_HINT}`,
|
|
274
|
+
name: `Only {{owners}} may import \`{{name}}\` from \`{{import}}\`. Use what the owner exports, or move this code into the owner. ${OWNERS_HINT}`,
|
|
275
|
+
namespace: `This import reaches {{names}} from \`{{import}}\`, which only {{owners}} may import. Import only the names that this file needs. ${OWNERS_HINT}`
|
|
276
|
+
},
|
|
277
|
+
schema: {
|
|
278
|
+
items: {
|
|
279
|
+
additionalProperties: false,
|
|
280
|
+
properties: {
|
|
281
|
+
import: {
|
|
282
|
+
minLength: 1,
|
|
283
|
+
type: "string"
|
|
284
|
+
},
|
|
285
|
+
names: {
|
|
286
|
+
items: { type: "string" },
|
|
287
|
+
minItems: 1,
|
|
288
|
+
type: "array"
|
|
289
|
+
},
|
|
290
|
+
owners: {
|
|
291
|
+
items: {
|
|
292
|
+
minLength: 1,
|
|
293
|
+
type: "string"
|
|
294
|
+
},
|
|
295
|
+
minItems: 1,
|
|
296
|
+
type: "array"
|
|
297
|
+
}
|
|
298
|
+
},
|
|
299
|
+
required: ["import", "owners"],
|
|
300
|
+
type: "object"
|
|
301
|
+
},
|
|
302
|
+
type: "array"
|
|
303
|
+
},
|
|
304
|
+
type: "problem"
|
|
305
|
+
}
|
|
306
|
+
};
|
|
307
|
+
}
|
|
308
|
+
const rule = createImportOwnersRule(project);
|
|
309
|
+
//#endregion
|
|
310
|
+
export { createImportOwnersRule, rule as default, isImportOf, readImportOwners };
|
|
@@ -0,0 +1,136 @@
|
|
|
1
|
+
import { Options, Rule } from "@oxlint/plugins";
|
|
2
|
+
//#region presets/lint/plugin/rules/layers.d.ts
|
|
3
|
+
/**
|
|
4
|
+
* Each layer by folder name, with the layers directly below it. Imports are transitive: a layer
|
|
5
|
+
* imports the layers it lists and every layer that those import.
|
|
6
|
+
*/
|
|
7
|
+
export type LayerMap = Readonly<Record<string, readonly string[]>>;
|
|
8
|
+
/**
|
|
9
|
+
* One layer map and its settings: one option object of the rule.
|
|
10
|
+
*/
|
|
11
|
+
export interface LayersOptions {
|
|
12
|
+
readonly children: ReadonlyMap<string, LayersOptions>;
|
|
13
|
+
readonly configPath: string;
|
|
14
|
+
/**
|
|
15
|
+
* Import conditions to match before the default ones, such as the `customConditions` of the
|
|
16
|
+
* target project's `tsconfig.json`.
|
|
17
|
+
*/
|
|
18
|
+
readonly conditions: readonly string[];
|
|
19
|
+
/**
|
|
20
|
+
* Composition files, by glob below the root. An entry imports any layer, and no layer imports an
|
|
21
|
+
* entry.
|
|
22
|
+
*/
|
|
23
|
+
readonly entries: readonly RegExp[];
|
|
24
|
+
/**
|
|
25
|
+
* Importers that the rule does not check, by glob below the root.
|
|
26
|
+
*/
|
|
27
|
+
readonly ignore: readonly RegExp[];
|
|
28
|
+
readonly ignoreTypes: boolean;
|
|
29
|
+
/**
|
|
30
|
+
* Layers whose units do not import each other.
|
|
31
|
+
*/
|
|
32
|
+
readonly isolate: ReadonlySet<string>;
|
|
33
|
+
readonly layers: LayerMap;
|
|
34
|
+
/**
|
|
35
|
+
* The root glob, relative to the package folder.
|
|
36
|
+
*/
|
|
37
|
+
readonly root: RegExp;
|
|
38
|
+
/**
|
|
39
|
+
* The declared root glob, before matching a concrete folder.
|
|
40
|
+
*/
|
|
41
|
+
readonly rootGlob: string;
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* What the rule reads from the file system. The plugin reads real files, and unit tests pass fakes.
|
|
45
|
+
*/
|
|
46
|
+
export interface Modules {
|
|
47
|
+
/**
|
|
48
|
+
* The nearest folder above `file` that holds a `package.json`.
|
|
49
|
+
*/
|
|
50
|
+
readonly findPackage: (file: string) => string | undefined;
|
|
51
|
+
/**
|
|
52
|
+
* The file that `specifier` in `importer` resolves to, or undefined when it does not resolve.
|
|
53
|
+
*/
|
|
54
|
+
readonly resolve: (importer: string, specifier: string, conditions: readonly string[]) => string | undefined;
|
|
55
|
+
}
|
|
56
|
+
/**
|
|
57
|
+
* Where a file sits below the root.
|
|
58
|
+
*/
|
|
59
|
+
export type Location = {
|
|
60
|
+
readonly kind: "entry";
|
|
61
|
+
readonly path: string;
|
|
62
|
+
} | {
|
|
63
|
+
readonly kind: "layer";
|
|
64
|
+
readonly layer: string;
|
|
65
|
+
readonly unit: string | undefined;
|
|
66
|
+
} | {
|
|
67
|
+
readonly kind: "unmappedFile";
|
|
68
|
+
readonly path: string;
|
|
69
|
+
} | {
|
|
70
|
+
readonly kind: "unmappedFolder";
|
|
71
|
+
readonly folder: string;
|
|
72
|
+
readonly path: string;
|
|
73
|
+
};
|
|
74
|
+
type LayerLocation = Extract<Location, {
|
|
75
|
+
kind: "layer";
|
|
76
|
+
}>;
|
|
77
|
+
/**
|
|
78
|
+
* The rule that an import from one layer to another breaks.
|
|
79
|
+
*/
|
|
80
|
+
type Violation = "isolated" | "sibling" | "upward";
|
|
81
|
+
export type MapError = {
|
|
82
|
+
readonly kind: "cycle";
|
|
83
|
+
readonly cycle: readonly string[];
|
|
84
|
+
} | {
|
|
85
|
+
readonly kind: "unknownImport";
|
|
86
|
+
readonly layer: string;
|
|
87
|
+
readonly name: string;
|
|
88
|
+
} | {
|
|
89
|
+
readonly kind: "unknownIsolate";
|
|
90
|
+
readonly name: string;
|
|
91
|
+
};
|
|
92
|
+
/**
|
|
93
|
+
* Read the rule options: one layer map for each option object. A map without `root` or `layers` is
|
|
94
|
+
* left out.
|
|
95
|
+
*/
|
|
96
|
+
export declare function readLayersOptions(options: Readonly<Options>): LayersOptions[];
|
|
97
|
+
/**
|
|
98
|
+
* Find the layers that `name` imports, including itself.
|
|
99
|
+
*/
|
|
100
|
+
export declare function listImportable(layers: LayerMap, name: string): ReadonlySet<string>;
|
|
101
|
+
/**
|
|
102
|
+
* Find the mistakes in a layer map: a name that is not a layer, and a cycle.
|
|
103
|
+
*/
|
|
104
|
+
export declare function findMapErrors(layers: LayerMap, isolate: ReadonlySet<string>): MapError[];
|
|
105
|
+
/**
|
|
106
|
+
* Split a path relative to the package folder at the root. Undefined when the path is not below the
|
|
107
|
+
* root.
|
|
108
|
+
*/
|
|
109
|
+
export declare function splitAtRoot(root: RegExp, relative: string): {
|
|
110
|
+
readonly below: readonly string[];
|
|
111
|
+
readonly root: string;
|
|
112
|
+
} | undefined;
|
|
113
|
+
export declare function locate(options: LayersOptions, root: string, below: readonly string[]): Location;
|
|
114
|
+
/**
|
|
115
|
+
* Find the rule that an import from one layer to another breaks.
|
|
116
|
+
*/
|
|
117
|
+
export declare function findViolation(layers: LayerMap, from: LayerLocation, to: LayerLocation): Violation | undefined;
|
|
118
|
+
/**
|
|
119
|
+
* The highest layers that both `from` and `to` import, where code that both need can move. For two
|
|
120
|
+
* units of one layer, the layer itself does not count.
|
|
121
|
+
*/
|
|
122
|
+
export declare function findLowerRepairs(layers: LayerMap, from: string, to: string): string[];
|
|
123
|
+
/**
|
|
124
|
+
* The lowest layers that import both `from` and `to`, where the importing file can move, in layer
|
|
125
|
+
* map order. For two units of one layer, the layer itself does not count.
|
|
126
|
+
*/
|
|
127
|
+
export declare function findUpperRepairs(layers: LayerMap, from: string, to: string): string[];
|
|
128
|
+
/**
|
|
129
|
+
* The repair sentence for a layer violation, with a leading space, or an empty string when the map
|
|
130
|
+
* allows no repair.
|
|
131
|
+
*/
|
|
132
|
+
export declare function describeRepairs(violation: Violation, lower: readonly string[], upper: readonly string[]): string;
|
|
133
|
+
export declare function createLayersRule(modules: Modules): Rule;
|
|
134
|
+
declare const rule: Rule;
|
|
135
|
+
//#endregion
|
|
136
|
+
export { rule as default };
|