@brutalway/check-tree 0.11.0 → 0.13.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 +19 -13
- package/dist/checkTree.js +27 -12
- package/dist/{sliceViolations-C9F_Wxpt.js → sharedFeatureViolations-CG7GEvs8.js} +43 -35
- package/dist/sharedViolations.js +2 -2
- package/dist/{treeViolations-DV1n3hcr.js → treeViolations-DCxZDgGS.js} +46 -16
- package/dist/treeViolations.js +6 -1
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# @brutalway/check-tree
|
|
2
2
|
|
|
3
|
-
Checks the folders of a source tree, which a linter does not see: only the folders the layout names, no empty folder, no
|
|
3
|
+
Checks the folders of a source tree, which a linter does not see: only the folders the layout names, no empty folder, no kind folder with a single child, files of one kind grouped in that kind's folder once there are two of them, no file in a shared feature that a single feature uses, and no shared feature whose files never import each other.
|
|
4
4
|
|
|
5
5
|
## Install
|
|
6
6
|
|
|
@@ -29,7 +29,7 @@ check-tree reads its options from the `"check-tree"` key of the `package.json` i
|
|
|
29
29
|
|
|
30
30
|
Each key has a flag, `--categories`, `--root-entries` and `--platforms`, which wins over the key for one run.
|
|
31
31
|
|
|
32
|
-
### Use your own
|
|
32
|
+
### Use your own kind folders
|
|
33
33
|
|
|
34
34
|
`categories` lists the kind folders the layout accepts, and the grouping rule only applies to the kinds listed in the table below that are also in this list.
|
|
35
35
|
|
|
@@ -49,13 +49,19 @@ On a codebase that did not follow the layout, the first run prints a lot. Write
|
|
|
49
49
|
npx check-tree src --write-known-violations
|
|
50
50
|
```
|
|
51
51
|
|
|
52
|
-
check-tree then skips the violations listed in that file, which it reads from the current folder.
|
|
52
|
+
check-tree then skips the violations listed in that file, which it reads from the current folder. Each entry names the rule and the folder or file it is about, plus a count for the rules that have one:
|
|
53
53
|
|
|
54
54
|
```json
|
|
55
|
-
[
|
|
55
|
+
[
|
|
56
|
+
{ "rule": "single-child", "path": "src/features/player/hooks" },
|
|
57
|
+
{ "rule": "shared-feature-groups", "path": "src/shared/ui", "count": 9 },
|
|
58
|
+
{ "rule": "kind-folder", "path": "src/features/player/hooks", "count": 3 }
|
|
59
|
+
]
|
|
56
60
|
```
|
|
57
61
|
|
|
58
|
-
|
|
62
|
+
An entry stays known while the same rule fires on the same path with a count no higher than the one written. Adding a file to `src/shared/ui` linked to one of its groups, or merging two of its groups, changes the message without making a new violation. A tenth group, or a fourth hook to move into `hooks/`, is reported. check-tree also fails on an entry whose rule no longer fires on its path: delete it from the file.
|
|
63
|
+
|
|
64
|
+
The rules are `empty-folder`, `single-child`, `not-a-layer`, `not-a-kind-folder`, `kind-folder` (files to move into a kind folder, the path being that folder), `shared-root-files`, `single-consumer` and `shared-feature-groups`. Up to 0.12, the file held the printed messages: check-tree now refuses that format, and `--write-known-violations` rewrites it.
|
|
59
65
|
|
|
60
66
|
### Run the check from code
|
|
61
67
|
|
|
@@ -65,7 +71,7 @@ import { treeViolations } from '@brutalway/check-tree/treeViolations';
|
|
|
65
71
|
const violations = treeViolations('src');
|
|
66
72
|
```
|
|
67
73
|
|
|
68
|
-
It takes the
|
|
74
|
+
It takes the kind folders as second argument and `{ rootEntries: true, platforms: ['web'] }` as third, like `--categories`, `--root-entries` and `--platforms`. The checks on the shared features read the imports, so they have their own export, which takes the platforms as second argument:
|
|
69
75
|
|
|
70
76
|
```ts
|
|
71
77
|
import { sharedViolations } from '@brutalway/check-tree/sharedViolations';
|
|
@@ -78,12 +84,12 @@ const violations = await sharedViolations('src');
|
|
|
78
84
|
### Rules
|
|
79
85
|
|
|
80
86
|
- a folder has a name the layout gives it:
|
|
81
|
-
- `src/` holds `app/`, `features/` and `shared/`, next to its entry
|
|
82
|
-
- a `shared/`, at any depth, holds one folder per
|
|
83
|
-
- a feature (`features/<x>/`) or a
|
|
87
|
+
- `src/` holds `app/`, `features/` and `shared/`, next to its entry points. `app/` belongs to the router, so its folders are not checked and its files stay where they are, whatever their kind
|
|
88
|
+
- a `shared/`, at any depth, holds one folder per shared feature, named after its concept (`shared/todos/`, `shared/errors/`), and no file
|
|
89
|
+
- a feature (`features/<x>/`) or a shared feature (`shared/<x>/`), at any depth, holds kind folders, `features/` for its sub-features and `shared/` for the shared features they have in common
|
|
84
90
|
- a kind folder holds kind folders only, like `presenters/` inside `components/`
|
|
85
91
|
- no empty folder
|
|
86
|
-
- a
|
|
92
|
+
- a kind folder (`components`, `hooks`, `stores`…) does not hold a single child: move the child up and delete the folder
|
|
87
93
|
- files of one kind stay at the root of their folder while there is one of them, and move into the kind's folder from two on. A folder that has that kind's folder keeps none of them next to it. The kinds are recognized by their name:
|
|
88
94
|
|
|
89
95
|
| Folder | Files |
|
|
@@ -105,8 +111,8 @@ const violations = await sharedViolations('src');
|
|
|
105
111
|
| `stores/` | `xStore.ts` |
|
|
106
112
|
| `lib/` | any other `.ts` file, except `xContent.ts`, `xEnv.ts` and `.d.ts` |
|
|
107
113
|
|
|
108
|
-
- a file of a
|
|
109
|
-
- the files of a
|
|
114
|
+
- a file of a shared feature is used by at least two consumers. For `src/shared/`, a consumer is a feature or another shared feature, a sub-feature counting as the feature it sits in. For the `shared/` of a feature or a shared feature, a consumer is one of its sub-features, another of its shared features, or its own files outside them. A file imported by another file of the same shared feature 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 points 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 shared feature used by one feature.
|
|
115
|
+
- the files of a shared feature, tests aside, form one group in the import graph, whatever the direction of the imports. Since two shared features 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 shared feature per group`. A shared feature of a single file is fine.
|
|
110
116
|
|
|
111
117
|
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.
|
|
112
118
|
|
|
@@ -114,7 +120,7 @@ So a feature with two components puts them in `components/`, and a `components/`
|
|
|
114
120
|
|
|
115
121
|
Test files (`*.test.ts`, `*.test.tsx`) are not counted.
|
|
116
122
|
|
|
117
|
-
The files of a folder named `X.<platform>.<ext>`, like `useScreenTitle.web.ts` and `useScreenTitle.native.ts`, go with `X.<ext>`, whether that file exists or not. The bundler picks them by name and nothing imports them, so the variants and their base count as one file: they have the kind of `X.<ext>` (`AvatarPhoto.web.tsx` is a presenter), a `lib/` holding only `alert.ts` and `alert.web.ts` holds a single child, and they belong to one group in the import graph of a
|
|
123
|
+
The files of a folder named `X.<platform>.<ext>`, like `useScreenTitle.web.ts` and `useScreenTitle.native.ts`, go with `X.<ext>`, whether that file exists or not. The bundler picks them by name and nothing imports them, so the variants and their base count as one file: they have the kind of `X.<ext>` (`AvatarPhoto.web.tsx` is a presenter), a `lib/` holding only `alert.ts` and `alert.web.ts` holds a single child, and they belong to one group in the import graph of a shared feature. check-tree does not compare their exports: TypeScript checks the base only, unless `moduleSuffixes` in the tsconfig points it at a variant.
|
|
118
124
|
|
|
119
125
|
### Output
|
|
120
126
|
|
package/dist/checkTree.js
CHANGED
|
@@ -1,31 +1,46 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
|
-
import { t as treeViolations } from "./treeViolations-
|
|
3
|
-
import { n as cruiseImports, t as
|
|
2
|
+
import { t as treeViolations } from "./treeViolations-DCxZDgGS.js";
|
|
3
|
+
import { n as cruiseImports, t as sharedFeatureViolations } from "./sharedFeatureViolations-CG7GEvs8.js";
|
|
4
4
|
import { resolve } from "node:path";
|
|
5
5
|
import { parseArgs } from "node:util";
|
|
6
6
|
import { existsSync, readFileSync, writeFileSync } from "node:fs";
|
|
7
7
|
// src/features/known-violations/KnownViolationsConfig.ts
|
|
8
8
|
const KNOWN_VIOLATIONS_FILE = ".check-tree-known-violations.json";
|
|
9
|
-
// src/shared/string-lists/isStringList.ts
|
|
10
|
-
function isStringList(value) {
|
|
11
|
-
return Array.isArray(value) && value.every((entry) => typeof entry === "string");
|
|
12
|
-
}
|
|
13
9
|
// src/features/known-violations/knownViolations.ts
|
|
10
|
+
function isKnownViolation(value) {
|
|
11
|
+
if (typeof value !== "object" || value === null) return false;
|
|
12
|
+
if (!("rule" in value) || !("path" in value)) return false;
|
|
13
|
+
const count = "count" in value ? value.count : void 0;
|
|
14
|
+
return typeof value.rule === "string" && typeof value.path === "string" && (count === void 0 || typeof count === "number");
|
|
15
|
+
}
|
|
14
16
|
function readKnownViolations(file) {
|
|
15
17
|
if (!existsSync(file)) return [];
|
|
16
18
|
const parsed = JSON.parse(readFileSync(file, "utf8"));
|
|
17
|
-
if (!
|
|
19
|
+
if (!Array.isArray(parsed) || !parsed.every(isKnownViolation)) throw new Error(`${file} holds a JSON array of { "rule", "path", "count" } objects, count being optional: run check-tree with --write-known-violations to write it in this format`);
|
|
18
20
|
return parsed;
|
|
19
21
|
}
|
|
22
|
+
function covers(entry, violation) {
|
|
23
|
+
return entry.rule === violation.rule && entry.path === violation.path && (violation.count ?? 0) <= (entry.count ?? 0);
|
|
24
|
+
}
|
|
20
25
|
function unknownViolations(violations, file) {
|
|
21
|
-
const known =
|
|
22
|
-
|
|
23
|
-
return [...violations.filter((violation) => !known.has(violation)), ...[...known].filter((entry) => !found.has(entry)).map((entry) => `${file} lists a violation that no longer happens: ${entry}`)];
|
|
26
|
+
const known = readKnownViolations(file);
|
|
27
|
+
return [...violations.filter((violation) => !known.some((entry) => covers(entry, violation))).map(({ message }) => message), ...known.filter((entry) => !violations.some(({ rule, path }) => rule === entry.rule && path === entry.path)).map(({ rule, path }) => `${file} lists ${rule} for ${path}, which no longer happens: delete the entry`)];
|
|
24
28
|
}
|
|
25
29
|
function writeKnownViolations(violations, file) {
|
|
26
|
-
|
|
30
|
+
const entries = violations.map(({ rule, path, count }) => count === void 0 ? {
|
|
31
|
+
rule,
|
|
32
|
+
path
|
|
33
|
+
} : {
|
|
34
|
+
rule,
|
|
35
|
+
path,
|
|
36
|
+
count
|
|
37
|
+
}).toSorted((a, b) => a.path.localeCompare(b.path) || a.rule.localeCompare(b.rule));
|
|
38
|
+
writeFileSync(file, `${JSON.stringify(entries, void 0, 2)}\n`);
|
|
27
39
|
}
|
|
28
40
|
// src/features/tree-settings/readTreeSettings.ts
|
|
41
|
+
function isStringList(value) {
|
|
42
|
+
return Array.isArray(value) && value.every((entry) => typeof entry === "string");
|
|
43
|
+
}
|
|
29
44
|
const SETTING_CHECKS = /* @__PURE__ */ new Map([
|
|
30
45
|
["categories", isStringList],
|
|
31
46
|
["rootEntries", (value) => typeof value === "boolean"],
|
|
@@ -64,7 +79,7 @@ try {
|
|
|
64
79
|
const violations = [...treeViolations(resolve(folder), categories, {
|
|
65
80
|
rootEntries: values["root-entries"] === true || settings.rootEntries === true,
|
|
66
81
|
platforms
|
|
67
|
-
}), ...
|
|
82
|
+
}), ...sharedFeatureViolations(folder, await cruiseImports(folder), platforms)];
|
|
68
83
|
if (values["write-known-violations"] === true) writeKnownViolations(violations, KNOWN_VIOLATIONS_FILE);
|
|
69
84
|
else {
|
|
70
85
|
const reported = unknownViolations(violations, KNOWN_VIOLATIONS_FILE);
|
|
@@ -29,8 +29,8 @@ async function cruiseImports(root) {
|
|
|
29
29
|
}
|
|
30
30
|
// src/features/shared-consumers/SharedConsumersConfig.ts
|
|
31
31
|
const TEST_CODE = /\.test\.tsx?$/u;
|
|
32
|
-
const
|
|
33
|
-
const
|
|
32
|
+
const SUB_FEATURE_OR_SHARED_FEATURE = /^(?:features|shared)\/[^/]+/u;
|
|
33
|
+
const SHARED_FEATURE_FILE = /^((.*)\/shared\/[^/]+)\//u;
|
|
34
34
|
// src/features/shared-consumers/lib/checkedPaths.ts
|
|
35
35
|
function checkedPaths(checkedFolder) {
|
|
36
36
|
const root = normalize(checkedFolder).replace(/\/$/u, "");
|
|
@@ -43,8 +43,8 @@ function checkedPaths(checkedFolder) {
|
|
|
43
43
|
// src/features/shared-consumers/lib/consumerSlots.ts
|
|
44
44
|
function slotOf(owner, importer) {
|
|
45
45
|
if (!importer.startsWith(`${owner}/`)) return importer;
|
|
46
|
-
const
|
|
47
|
-
return
|
|
46
|
+
const child = SUB_FEATURE_OR_SHARED_FEATURE.exec(importer.slice(owner.length + 1));
|
|
47
|
+
return child === null ? owner : `${owner}/${child[0]}`;
|
|
48
48
|
}
|
|
49
49
|
function unitOf(path, platforms) {
|
|
50
50
|
return `${dirname(path)}/${platformBase(basename(path), platforms)}`;
|
|
@@ -63,20 +63,20 @@ function importersByUnit(modules, platforms) {
|
|
|
63
63
|
function consumerSlots(modules, platforms) {
|
|
64
64
|
const importers = importersByUnit(modules, platforms);
|
|
65
65
|
const slotsByModule = /* @__PURE__ */ new Map();
|
|
66
|
-
return function slots(path,
|
|
67
|
-
const key = `${
|
|
66
|
+
return function slots(path, sharedFeature) {
|
|
67
|
+
const key = `${sharedFeature.folder}:${path}`;
|
|
68
68
|
const known = slotsByModule.get(key);
|
|
69
69
|
if (known !== void 0) return known;
|
|
70
70
|
const found = /* @__PURE__ */ new Set();
|
|
71
71
|
slotsByModule.set(key, found);
|
|
72
|
-
for (const importer of importers.get(unitOf(path, platforms)) ?? []) if (importer.startsWith(`${
|
|
73
|
-
else found.add(slotOf(
|
|
72
|
+
for (const importer of importers.get(unitOf(path, platforms)) ?? []) if (importer.startsWith(`${sharedFeature.folder}/`)) for (const slot of slots(importer, sharedFeature)) found.add(slot);
|
|
73
|
+
else found.add(slotOf(sharedFeature.owner, importer));
|
|
74
74
|
return found;
|
|
75
75
|
};
|
|
76
76
|
}
|
|
77
|
-
// src/features/shared-consumers/lib/
|
|
78
|
-
function
|
|
79
|
-
const [, folder, owner] =
|
|
77
|
+
// src/features/shared-consumers/lib/sharedFeatureOf.ts
|
|
78
|
+
function sharedFeatureOf(path) {
|
|
79
|
+
const [, folder, owner] = SHARED_FEATURE_FILE.exec(path) ?? [];
|
|
80
80
|
if (folder === void 0 || owner === void 0) return void 0;
|
|
81
81
|
return {
|
|
82
82
|
owner,
|
|
@@ -85,19 +85,22 @@ function sliceOf(path) {
|
|
|
85
85
|
}
|
|
86
86
|
// src/features/shared-consumers/lib/sharedConsumerViolations.ts
|
|
87
87
|
function violation(path, owner, slot) {
|
|
88
|
-
|
|
89
|
-
|
|
88
|
+
return {
|
|
89
|
+
rule: "single-consumer",
|
|
90
|
+
path,
|
|
91
|
+
message: slot === owner ? `${path} is only used by ${owner} outside its sub-features: move it out of shared/ into the kind folders of ${owner}` : `${path} is only used by ${slot}: move it into ${slot}`
|
|
92
|
+
};
|
|
90
93
|
}
|
|
91
94
|
function sharedConsumerViolations(checkedFolder, modules, platforms) {
|
|
92
95
|
const { root, shown } = checkedPaths(checkedFolder);
|
|
93
96
|
const slots = consumerSlots(modules, platforms);
|
|
94
97
|
return modules.flatMap(({ source }) => {
|
|
95
|
-
const
|
|
96
|
-
if (
|
|
97
|
-
const [slot, ...others] = slots(source,
|
|
98
|
+
const sharedFeature = sharedFeatureOf(source);
|
|
99
|
+
if (sharedFeature === void 0 || TEST_CODE.test(source) || !source.startsWith(`${root}/`)) return [];
|
|
100
|
+
const [slot, ...others] = slots(source, sharedFeature);
|
|
98
101
|
if (slot === void 0 || others.length > 0) return [];
|
|
99
|
-
if (
|
|
100
|
-
return [violation(shown(source), shown(
|
|
102
|
+
if (sharedFeature.owner === root && !SUB_FEATURE_OR_SHARED_FEATURE.test(slot.slice(root.length + 1))) return [];
|
|
103
|
+
return [violation(shown(source), shown(sharedFeature.owner), shown(slot))];
|
|
101
104
|
});
|
|
102
105
|
}
|
|
103
106
|
// src/features/shared-consumers/lib/variantLinks.ts
|
|
@@ -111,12 +114,12 @@ function variantLinks(files, platforms) {
|
|
|
111
114
|
return [];
|
|
112
115
|
});
|
|
113
116
|
}
|
|
114
|
-
// src/features/shared-consumers/lib/
|
|
115
|
-
function
|
|
117
|
+
// src/features/shared-consumers/lib/sharedFeatureGroupViolations.ts
|
|
118
|
+
function sharedFeatureFolders(root, modules) {
|
|
116
119
|
const folders = /* @__PURE__ */ new Map();
|
|
117
120
|
for (const { source } of modules) {
|
|
118
|
-
const
|
|
119
|
-
if (
|
|
121
|
+
const sharedFeature = sharedFeatureOf(source);
|
|
122
|
+
if (sharedFeature !== void 0 && !TEST_CODE.test(source) && source.startsWith(`${root}/`)) folders.set(source, sharedFeature.folder);
|
|
120
123
|
}
|
|
121
124
|
return folders;
|
|
122
125
|
}
|
|
@@ -135,27 +138,32 @@ function linkedGroups(files, links) {
|
|
|
135
138
|
for (const [from, to] of links) parents.set(head(from), head(to));
|
|
136
139
|
return head;
|
|
137
140
|
}
|
|
138
|
-
function
|
|
139
|
-
const
|
|
141
|
+
function groupsBySharedFeature(folders, head) {
|
|
142
|
+
const sharedFeatures = /* @__PURE__ */ new Map();
|
|
140
143
|
for (const [file, folder] of [...folders].toSorted(([a], [b]) => a.localeCompare(b))) {
|
|
141
|
-
const groups =
|
|
144
|
+
const groups = sharedFeatures.get(folder) ?? /* @__PURE__ */ new Map();
|
|
142
145
|
groups.set(head(file), [...groups.get(head(file)) ?? [], file.slice(folder.length + 1)]);
|
|
143
|
-
|
|
146
|
+
sharedFeatures.set(folder, groups);
|
|
144
147
|
}
|
|
145
|
-
return
|
|
148
|
+
return sharedFeatures;
|
|
146
149
|
}
|
|
147
|
-
function
|
|
150
|
+
function sharedFeatureGroupViolations(checkedFolder, modules, platforms) {
|
|
148
151
|
const { root, shown } = checkedPaths(checkedFolder);
|
|
149
|
-
const folders =
|
|
152
|
+
const folders = sharedFeatureFolders(root, modules);
|
|
150
153
|
const links = [...importLinks(modules, folders), ...variantLinks(folders.keys(), platforms)];
|
|
151
|
-
return [...
|
|
154
|
+
return [...groupsBySharedFeature(folders, linkedGroups(folders.keys(), links))].flatMap(([folder, groups]) => {
|
|
152
155
|
if (groups.size < 2) return [];
|
|
153
156
|
const listed = [...groups.values()].map((files) => `(${files.join(", ")})`).join(" ");
|
|
154
|
-
return [
|
|
157
|
+
return [{
|
|
158
|
+
rule: "shared-feature-groups",
|
|
159
|
+
path: shown(folder),
|
|
160
|
+
count: groups.size,
|
|
161
|
+
message: `${shown(folder)} holds ${groups.size} groups of files that never import each other ${listed}: split it into one shared feature per group`
|
|
162
|
+
}];
|
|
155
163
|
});
|
|
156
164
|
}
|
|
157
|
-
// src/features/shared-consumers/lib/
|
|
158
|
-
function
|
|
159
|
-
return [...sharedConsumerViolations(checkedFolder, modules, platforms), ...
|
|
165
|
+
// src/features/shared-consumers/lib/sharedFeatureViolations.ts
|
|
166
|
+
function sharedFeatureViolations(checkedFolder, modules, platforms) {
|
|
167
|
+
return [...sharedConsumerViolations(checkedFolder, modules, platforms), ...sharedFeatureGroupViolations(checkedFolder, modules, platforms)];
|
|
160
168
|
}
|
|
161
|
-
export { cruiseImports as n,
|
|
169
|
+
export { cruiseImports as n, sharedFeatureViolations as t };
|
package/dist/sharedViolations.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import { n as cruiseImports, t as
|
|
1
|
+
import { n as cruiseImports, t as sharedFeatureViolations } from "./sharedFeatureViolations-CG7GEvs8.js";
|
|
2
2
|
// src/sharedViolations.ts
|
|
3
3
|
async function sharedViolations(root, platforms) {
|
|
4
|
-
return
|
|
4
|
+
return sharedFeatureViolations(root, await cruiseImports(root), platforms).map(({ message }) => message);
|
|
5
5
|
}
|
|
6
6
|
export { sharedViolations };
|
|
@@ -121,9 +121,9 @@ const FILE_GROUPS = [
|
|
|
121
121
|
const SOURCE_LAYERS = /* @__PURE__ */ new Map([
|
|
122
122
|
["app", "router"],
|
|
123
123
|
["features", "features"],
|
|
124
|
-
["shared", "
|
|
124
|
+
["shared", "sharedFeatures"]
|
|
125
125
|
]);
|
|
126
|
-
const FEATURE_LAYERS = /* @__PURE__ */ new Map([["features", "features"], ["shared", "
|
|
126
|
+
const FEATURE_LAYERS = /* @__PURE__ */ new Map([["features", "features"], ["shared", "sharedFeatures"]]);
|
|
127
127
|
function kindFolder(path, categories, reason) {
|
|
128
128
|
if (categories.has(basename(path))) return {
|
|
129
129
|
layer: "kind",
|
|
@@ -131,7 +131,11 @@ function kindFolder(path, categories, reason) {
|
|
|
131
131
|
};
|
|
132
132
|
return {
|
|
133
133
|
layer: "free",
|
|
134
|
-
violations: [
|
|
134
|
+
violations: [{
|
|
135
|
+
rule: "not-a-kind-folder",
|
|
136
|
+
path,
|
|
137
|
+
message: `${path} is not a kind folder: ${reason}`
|
|
138
|
+
}]
|
|
135
139
|
};
|
|
136
140
|
}
|
|
137
141
|
function sourceChild(path) {
|
|
@@ -142,7 +146,11 @@ function sourceChild(path) {
|
|
|
142
146
|
};
|
|
143
147
|
return {
|
|
144
148
|
layer: "free",
|
|
145
|
-
violations: [
|
|
149
|
+
violations: [{
|
|
150
|
+
rule: "not-a-layer",
|
|
151
|
+
path,
|
|
152
|
+
message: `${path} is not a layer: ${dirname(path)}/ holds app/, features/ and shared/ next to its entry points, move it into one of them`
|
|
153
|
+
}]
|
|
146
154
|
};
|
|
147
155
|
}
|
|
148
156
|
function featureChild(path, categories) {
|
|
@@ -151,11 +159,11 @@ function featureChild(path, categories) {
|
|
|
151
159
|
layer,
|
|
152
160
|
violations: []
|
|
153
161
|
};
|
|
154
|
-
return kindFolder(path, categories, "a feature or a
|
|
162
|
+
return kindFolder(path, categories, "a feature or a shared feature holds kind folders, features/ for its sub-features and shared/ for the shared features they have in common, move it into features/ as a sub-feature, into shared/ as a shared feature, or spread its files into kind folders");
|
|
155
163
|
}
|
|
156
164
|
function childLayer(layer, path, categories) {
|
|
157
165
|
if (layer === "source") return sourceChild(path);
|
|
158
|
-
if (layer === "features" || layer === "
|
|
166
|
+
if (layer === "features" || layer === "sharedFeatures") return {
|
|
159
167
|
layer: "feature",
|
|
160
168
|
violations: []
|
|
161
169
|
};
|
|
@@ -172,13 +180,28 @@ function childLayer(layer, path, categories) {
|
|
|
172
180
|
}
|
|
173
181
|
// src/features/tree-violations/features/layout/lib/filesCheck.ts
|
|
174
182
|
function filesCheck(layer, holdsEntries) {
|
|
175
|
-
if (layer === "
|
|
183
|
+
if (layer === "sharedFeatures") return "sharedFeatures";
|
|
176
184
|
return layer === "router" || holdsEntries ? "none" : "groups";
|
|
177
185
|
}
|
|
178
|
-
// src/features/tree-violations/features/layout/lib/
|
|
179
|
-
function
|
|
186
|
+
// src/features/tree-violations/features/layout/lib/kindFolderViolation.ts
|
|
187
|
+
function kindFolderViolation(name, folder, loose, folderExists) {
|
|
188
|
+
const message = folderExists ? `${name} has a ${folder}/ folder: move ${loose.join(", ")} into it` : `${name} holds ${loose.length} ${folder} files (${loose.join(", ")}): move them into ${folder}/`;
|
|
189
|
+
return {
|
|
190
|
+
rule: "kind-folder",
|
|
191
|
+
path: `${name}/${folder}`,
|
|
192
|
+
count: loose.length,
|
|
193
|
+
message
|
|
194
|
+
};
|
|
195
|
+
}
|
|
196
|
+
// src/features/tree-violations/features/layout/lib/sharedRootFileViolations.ts
|
|
197
|
+
function sharedRootFileViolations(name, files) {
|
|
180
198
|
if (files.length === 0) return [];
|
|
181
|
-
return [
|
|
199
|
+
return [{
|
|
200
|
+
rule: "shared-root-files",
|
|
201
|
+
path: name,
|
|
202
|
+
count: files.length,
|
|
203
|
+
message: `${name} holds ${files.join(", ")}: shared/ holds one folder per shared feature, move each file into the shared feature of its concept`
|
|
204
|
+
}];
|
|
182
205
|
}
|
|
183
206
|
// src/features/tree-violations/treeViolations.ts
|
|
184
207
|
function groupViolations(name, folderName, listing, categoryFolders) {
|
|
@@ -187,9 +210,8 @@ function groupViolations(name, folderName, listing, categoryFolders) {
|
|
|
187
210
|
if (!categoryFolders.has(folder) || !appliesIn(folderName)) return [];
|
|
188
211
|
const loose = listing.files.filter((file) => belongs(listing.baseOf(file)));
|
|
189
212
|
if (loose.length === 0 || holdsFilesOnly && loose.length === listing.files.length) return [];
|
|
190
|
-
if (listing.folders.has(folder)
|
|
191
|
-
|
|
192
|
-
return [`${name} holds ${loose.length} ${folder} files (${loose.join(", ")}): move them into ${folder}/`];
|
|
213
|
+
if (!listing.folders.has(folder) && new Set(loose.map(listing.baseOf)).size === 1) return [];
|
|
214
|
+
return [kindFolderViolation(name, folder, loose, listing.folders.has(folder))];
|
|
193
215
|
});
|
|
194
216
|
}
|
|
195
217
|
function folderListing(entries, platforms) {
|
|
@@ -200,15 +222,23 @@ function folderListing(entries, platforms) {
|
|
|
200
222
|
};
|
|
201
223
|
}
|
|
202
224
|
function shapeViolations(name, count, isCategory) {
|
|
203
|
-
if (count === 0) return [
|
|
204
|
-
|
|
225
|
+
if (count === 0) return [{
|
|
226
|
+
rule: "empty-folder",
|
|
227
|
+
path: name,
|
|
228
|
+
message: `${name} is empty: delete it`
|
|
229
|
+
}];
|
|
230
|
+
if (count === 1 && isCategory) return [{
|
|
231
|
+
rule: "single-child",
|
|
232
|
+
path: name,
|
|
233
|
+
message: `${name} holds a single child: move it up one level and delete the folder`
|
|
234
|
+
}];
|
|
205
235
|
return [];
|
|
206
236
|
}
|
|
207
237
|
function folderViolations(folder, name, scan, check) {
|
|
208
238
|
const children = readdirSync(folder, { withFileTypes: true }).filter((entry) => !TEST_FILE.test(entry.name));
|
|
209
239
|
const listing = folderListing(children, scan.platforms);
|
|
210
240
|
const violations = shapeViolations(name, new Set(listing.files.map(listing.baseOf)).size + children.length - listing.files.length, scan.categories.has(basename(folder)));
|
|
211
|
-
if (check === "
|
|
241
|
+
if (check === "sharedFeatures") return [...violations, ...sharedRootFileViolations(name, listing.files)];
|
|
212
242
|
if (check === "none") return violations;
|
|
213
243
|
return [...violations, ...groupViolations(name, basename(folder), listing, scan.categories)];
|
|
214
244
|
}
|
package/dist/treeViolations.js
CHANGED
|
@@ -1 +1,6 @@
|
|
|
1
|
-
|
|
1
|
+
import { t as treeViolations$1 } from "./treeViolations-DCxZDgGS.js";
|
|
2
|
+
// src/treeViolations.ts
|
|
3
|
+
function treeViolations(root, categoryFolders, options) {
|
|
4
|
+
return treeViolations$1(root, categoryFolders, options).map(({ message }) => message);
|
|
5
|
+
}
|
|
6
|
+
export { treeViolations };
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@brutalway/check-tree",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"description": "Checks a source tree: only the folders the layout names, no empty folder, no
|
|
3
|
+
"version": "0.13.0",
|
|
4
|
+
"description": "Checks a source tree: only the folders the layout names, no empty folder, no kind folder holding a single child, stylesheets grouped in styles/, no file of a shared feature used by a single feature, no shared feature whose files never import each other.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"lint",
|
|
7
7
|
"folder-structure",
|