fix(client): lint:layers holds src/types.ts to the rules of types

A file's layer was the first path segment under src/, so src/types.ts,
the shared types module every layer imports as '../types', got the
layer "types.ts" and no rule at all, while the imports into it already
resolved to "types". It could import a component or a page unnoticed.
A file directly in src/ is now the layer of its name without the
extension, for the importer and the import alike. No file changes its
count: types.ts imports only @trek/shared today.
This commit is contained in:
Maurice
2026-10-08 12:50:33 +02:00
parent 6a6f3f6cf6
commit 3e37dcfb45
4 changed files with 38 additions and 8 deletions
+15 -5
View File
@@ -53,9 +53,19 @@ export function importsOf(source, file) {
}
/**
* The top directory under src/ a relative import lands in ("components",
* "api"; a file directly in src/ gives its own name), or null for a package
* or a path outside src/.
* The layer of a path from src/: its top directory ("components", "api"), or,
* for a file directly in src/, its name without the extension. src/types.ts
* is then the layer "types" both when it imports and when it is imported as
* '../types', and its rules are those of the src/types/ directory.
*/
export function layerOfPath(fromSrc) {
const [top, ...rest] = fromSrc.split('/');
return rest.length ? top : top.replace(/(\.d)?\.[cm]?[jt]sx?$/, '');
}
/**
* The layer a relative import lands in (see layerOfPath), or null for a
* package or a path outside src/.
*/
export function layerOf(srcRoot, fromFile, specifier) {
if (!specifier.startsWith('.')) return null;
@@ -63,7 +73,7 @@ export function layerOf(srcRoot, fromFile, specifier) {
.split('\\')
.join('/');
if (!target || target.startsWith('..')) return null;
return target.split('/')[0];
return layerOfPath(target);
}
/** Every source file under root/src outside the tests, with its key, its layer and its imports resolved to layers. */
@@ -71,7 +81,7 @@ export function sourceGraph(root) {
const src = join(root, 'src');
return listFiles(root, ['src'], (key) => /\.tsx?$/.test(key) && !TEST_FILE.test(key)).map((path) => ({
key: toKey(root, path),
layer: toKey(src, path).split('/')[0],
layer: layerOfPath(toKey(src, path)),
imports: importsOf(readText(path), path).map((imp) => ({ ...imp, layer: layerOf(src, path, imp.specifier) })),
}));
}
+2 -1
View File
@@ -17,7 +17,8 @@ const VIEW = ['components', 'mobile', 'pages', 'hooks'];
* (date helpers) are helpers like utils/. Directories without an entry are
* free on purpose: mobile and pages sit at the top, the app root wires
* everything, and managed/ is the slot an operator fills with whole screens
* at build time.
* at build time. A file directly in src/ is the layer of its name, so
* src/types.ts falls under types like the src/types/ directory.
*/
export const RULES = {
components: ['pages', 'mobile'],