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.
Files changed (81) hide show
  1. package/README.md +447 -127
  2. package/dist/index.js +628 -57
  3. package/dist/oxlint.js +1 -0
  4. package/dist/presets/analyze.js +1 -0
  5. package/dist/presets/lint/{react-doctor.d.ts → architecture.d.ts} +1 -1
  6. package/dist/presets/lint/architecture.js +8 -0
  7. package/dist/presets/lint/core.js +1 -2
  8. package/dist/presets/lint/effect.js +26 -1
  9. package/dist/presets/lint/plugin/glob.d.ts +7 -0
  10. package/dist/presets/lint/plugin/glob.js +48 -0
  11. package/dist/presets/lint/plugin/imports.d.ts +4 -0
  12. package/dist/presets/lint/plugin/imports.js +4 -1
  13. package/dist/presets/lint/plugin/index.d.ts +3 -0
  14. package/dist/presets/lint/plugin/index.js +14 -8
  15. package/dist/presets/lint/plugin/options.d.ts +5 -5
  16. package/dist/presets/lint/plugin/options.js +19 -7
  17. package/dist/presets/lint/plugin/resolver.d.ts +8 -0
  18. package/dist/presets/lint/plugin/resolver.js +79 -0
  19. package/dist/presets/lint/plugin/rules/import-owners.d.ts +67 -0
  20. package/dist/presets/lint/plugin/rules/import-owners.js +310 -0
  21. package/dist/presets/lint/plugin/rules/layers.d.ts +136 -0
  22. package/dist/presets/lint/plugin/rules/layers.js +479 -0
  23. package/dist/presets/lint/plugin/rules/no-query-data-in-state.js +1 -1
  24. package/dist/presets/lint/plugin/rules/no-react-state-hooks.d.ts +0 -5
  25. package/dist/presets/lint/plugin/rules/no-react-state-hooks.js +3 -47
  26. package/dist/presets/lint/plugin/rules/query-from-loader.js +2 -2
  27. package/dist/presets/lint/plugin/rules/require-suppression-reason.d.ts +5 -0
  28. package/dist/presets/lint/plugin/rules/require-suppression-reason.js +32 -0
  29. package/dist/presets/lint/react-strict.js +22 -2
  30. package/dist/presets/lint/strict.js +35 -2
  31. package/dist/presets/lint/{antislop.d.ts → tailwind.d.ts} +1 -1
  32. package/dist/presets/lint/{shadcn.js → tailwind.js} +1 -1
  33. package/dist/presets/lint/vendor/antislop/license.md +26 -1
  34. package/dist/presets/lint/vendor/antislop/plugin.d.mts +3 -0
  35. package/dist/presets/lint/vendor/antislop/plugin.mjs +959 -33
  36. package/dist/presets/lint/vendor/antislop-effect/license.md +27 -0
  37. package/dist/presets/lint/vendor/antislop-effect/plugin.d.mts +17 -0
  38. package/dist/presets/lint/vendor/antislop-effect/plugin.mjs +579 -0
  39. package/dist/presets/{lint → rules}/custom.d.ts +3 -20
  40. package/dist/presets/{lint → rules}/custom.js +50 -46
  41. package/dist/presets/rules/folder.d.ts +34 -0
  42. package/dist/presets/rules/folder.js +52 -0
  43. package/dist/presets/rules/index.d.ts +2 -9
  44. package/dist/presets/rules/index.js +1 -29
  45. package/dist/presets/rules/load.d.ts +8 -0
  46. package/dist/presets/rules/load.js +29 -0
  47. package/dist/presets/rules/plugin.d.ts +3 -0
  48. package/dist/presets/rules/plugin.js +1 -0
  49. package/dist/presets/rules/testing.d.ts +2 -0
  50. package/dist/presets/rules/testing.js +14 -0
  51. package/dist/presets/tsconfig.json +3 -0
  52. package/dist/presets/typescript/resets/array-includes.d.ts +15 -0
  53. package/dist/presets/typescript/resets/array-index-of.d.ts +23 -0
  54. package/dist/presets/typescript/resets/eval.d.ts +1 -0
  55. package/dist/presets/typescript/resets/fetch.d.ts +3 -0
  56. package/dist/presets/typescript/resets/filter-boolean.d.ts +24 -0
  57. package/dist/presets/typescript/resets/helpers.d.ts +15 -0
  58. package/dist/presets/typescript/resets/index.d.ts +20 -0
  59. package/dist/presets/typescript/resets/is-array.d.ts +3 -0
  60. package/dist/presets/typescript/resets/json-parse.d.ts +14 -0
  61. package/dist/presets/typescript/resets/license.md +12 -0
  62. package/dist/presets/typescript/resets/map-constructor.d.ts +3 -0
  63. package/dist/presets/typescript/resets/map-groupby.d.ts +3 -0
  64. package/dist/presets/typescript/resets/map-has.d.ts +9 -0
  65. package/dist/presets/typescript/resets/object-assign.d.ts +8 -0
  66. package/dist/presets/typescript/resets/object-get-prototype-of.d.ts +3 -0
  67. package/dist/presets/typescript/resets/object-groupby.d.ts +6 -0
  68. package/dist/presets/typescript/resets/object-values.d.ts +8 -0
  69. package/dist/presets/typescript/resets/reflect.d.ts +12 -0
  70. package/dist/presets/typescript/resets/set-has.d.ts +9 -0
  71. package/dist/presets/typescript/resets/storage.d.ts +3 -0
  72. package/dist/presets/typescript.json +13 -0
  73. package/dist/runtime-CVZPDpfE.js +11 -0
  74. package/package.json +81 -19
  75. package/skills/adamantite/SKILL.md +110 -207
  76. package/skills/adamantite/references/custom-rules.md +120 -0
  77. package/skills/adamantite/references/lint-rules.md +152 -0
  78. package/skills/adamantite/references/setup.md +70 -0
  79. package/dist/presets/lint/antislop.js +0 -28
  80. package/dist/presets/lint/react-doctor.js +0 -20
  81. package/dist/presets/lint/shadcn.d.ts +0 -5
@@ -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 };