@brutalway/check-tree 0.10.0 → 0.11.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
CHANGED
|
@@ -105,7 +105,7 @@ const violations = await sharedViolations('src');
|
|
|
105
105
|
| `stores/` | `xStore.ts` |
|
|
106
106
|
| `lib/` | any other `.ts` file, except `xContent.ts`, `xEnv.ts` and `.d.ts` |
|
|
107
107
|
|
|
108
|
-
- a file of a slice is used by at least two consumers. For `src/shared/`, a consumer is a feature or another slice, a sub-feature counting as the feature it sits in. For the `shared/` of a feature or a slice, a consumer is one of its sub-features, another of its slices, or its own files outside them. A file imported by another file of the same slice gets the consumers of that file. A test
|
|
108
|
+
- a file of a slice is used by at least two consumers. For `src/shared/`, a consumer is a feature or another slice, a sub-feature counting as the feature it sits in. For the `shared/` of a feature or a slice, a consumer is one of its sub-features, another of its slices, or its own files outside them. A file imported by another file of the same slice gets the consumers of that file. A test is never a consumer, wherever it sits: it exists for the code it covers and gives no reason to share it. A file that only tests import has no consumer at all and passes here, `knip --production` reports it. A file of `src/shared/` used only by `app/` or by the entry files of `src/` passes. A file with a single consumer gets `src/shared/dates/format.ts is only used by src/features/cart: move it into src/features/cart`, which also catches a whole slice used by one feature.
|
|
109
109
|
- the files of a slice, tests aside, form one group in the import graph, whatever the direction of the imports. Since two slices never import each other, the folder then only names a boundary the graph already draws. A `shared/utils/` holding `errorMessage.ts`, `isRecord.ts` and `packageRoot.ts`, which do not import each other, gets `src/shared/utils holds 3 groups of files that never import each other (errorMessage.ts) (isRecord.ts) (packageRoot.ts): split it into one slice per group`. A slice of a single file is fine.
|
|
110
110
|
|
|
111
111
|
The first of these two rules says why a file is shared, the second why files sit together. Each one lets through what the other catches: in the `shared/utils/` above, each file has two consumers.
|
package/dist/checkTree.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
2
|
import { t as treeViolations } from "./treeViolations-DV1n3hcr.js";
|
|
3
|
-
import { n as cruiseImports, t as sliceViolations } from "./sliceViolations-
|
|
3
|
+
import { n as cruiseImports, t as sliceViolations } from "./sliceViolations-C9F_Wxpt.js";
|
|
4
4
|
import { resolve } from "node:path";
|
|
5
5
|
import { parseArgs } from "node:util";
|
|
6
6
|
import { existsSync, readFileSync, writeFileSync } from "node:fs";
|
package/dist/sharedViolations.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { n as cruiseImports, t as sliceViolations } from "./sliceViolations-
|
|
1
|
+
import { n as cruiseImports, t as sliceViolations } from "./sliceViolations-C9F_Wxpt.js";
|
|
2
2
|
// src/sharedViolations.ts
|
|
3
3
|
async function sharedViolations(root, platforms) {
|
|
4
4
|
return sliceViolations(root, await cruiseImports(root), platforms);
|
|
@@ -46,13 +46,22 @@ function slotOf(owner, importer) {
|
|
|
46
46
|
const slice = SLICE.exec(importer.slice(owner.length + 1));
|
|
47
47
|
return slice === null ? owner : `${owner}/${slice[0]}`;
|
|
48
48
|
}
|
|
49
|
-
function
|
|
49
|
+
function unitOf(path, platforms) {
|
|
50
|
+
return `${dirname(path)}/${platformBase(basename(path), platforms)}`;
|
|
51
|
+
}
|
|
52
|
+
function importersByUnit(modules, platforms) {
|
|
50
53
|
const importers = /* @__PURE__ */ new Map();
|
|
51
|
-
for (const { source, imports } of modules)
|
|
54
|
+
for (const { source, imports } of modules) {
|
|
55
|
+
if (TEST_CODE.test(source)) continue;
|
|
56
|
+
for (const target of imports) {
|
|
57
|
+
const unit = unitOf(target, platforms);
|
|
58
|
+
importers.set(unit, [...importers.get(unit) ?? [], source]);
|
|
59
|
+
}
|
|
60
|
+
}
|
|
52
61
|
return importers;
|
|
53
62
|
}
|
|
54
|
-
function consumerSlots(modules) {
|
|
55
|
-
const importers =
|
|
63
|
+
function consumerSlots(modules, platforms) {
|
|
64
|
+
const importers = importersByUnit(modules, platforms);
|
|
56
65
|
const slotsByModule = /* @__PURE__ */ new Map();
|
|
57
66
|
return function slots(path, slice) {
|
|
58
67
|
const key = `${slice.folder}:${path}`;
|
|
@@ -60,7 +69,7 @@ function consumerSlots(modules) {
|
|
|
60
69
|
if (known !== void 0) return known;
|
|
61
70
|
const found = /* @__PURE__ */ new Set();
|
|
62
71
|
slotsByModule.set(key, found);
|
|
63
|
-
for (const importer of importers.get(path) ?? []) if (importer.startsWith(`${slice.folder}/`)) for (const slot of slots(importer, slice)) found.add(slot);
|
|
72
|
+
for (const importer of importers.get(unitOf(path, platforms)) ?? []) if (importer.startsWith(`${slice.folder}/`)) for (const slot of slots(importer, slice)) found.add(slot);
|
|
64
73
|
else found.add(slotOf(slice.owner, importer));
|
|
65
74
|
return found;
|
|
66
75
|
};
|
|
@@ -79,9 +88,9 @@ function violation(path, owner, slot) {
|
|
|
79
88
|
if (slot === owner) return `${path} is only used by ${owner} outside its sub-features: move it out of shared/ into the kind folders of ${owner}`;
|
|
80
89
|
return `${path} is only used by ${slot}: move it into ${slot}`;
|
|
81
90
|
}
|
|
82
|
-
function sharedConsumerViolations(checkedFolder, modules) {
|
|
91
|
+
function sharedConsumerViolations(checkedFolder, modules, platforms) {
|
|
83
92
|
const { root, shown } = checkedPaths(checkedFolder);
|
|
84
|
-
const slots = consumerSlots(modules);
|
|
93
|
+
const slots = consumerSlots(modules, platforms);
|
|
85
94
|
return modules.flatMap(({ source }) => {
|
|
86
95
|
const slice = sliceOf(source);
|
|
87
96
|
if (slice === void 0 || TEST_CODE.test(source) || !source.startsWith(`${root}/`)) return [];
|
|
@@ -147,6 +156,6 @@ function sliceGroupViolations(checkedFolder, modules, platforms) {
|
|
|
147
156
|
}
|
|
148
157
|
// src/features/shared-consumers/lib/sliceViolations.ts
|
|
149
158
|
function sliceViolations(checkedFolder, modules, platforms) {
|
|
150
|
-
return [...sharedConsumerViolations(checkedFolder, modules), ...sliceGroupViolations(checkedFolder, modules, platforms)];
|
|
159
|
+
return [...sharedConsumerViolations(checkedFolder, modules, platforms), ...sliceGroupViolations(checkedFolder, modules, platforms)];
|
|
151
160
|
}
|
|
152
161
|
export { cruiseImports as n, sliceViolations as t };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@brutalway/check-tree",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.11.0",
|
|
4
4
|
"description": "Checks a source tree: only the folders the layout names, no empty folder, no category folder holding a single child, stylesheets grouped in styles/, no file of a slice of shared/ used by a single feature, no slice whose files never import each other.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"lint",
|