Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 | 2x 2x 2x 2x 2x 83x 2x 83x 79x 76x 98x 69x 43x 2x 2x 14x 8x 14x 6x 14x 22x 27x 2x 29x 1x 2x 14x 2x 31x 12x 9x 12x 6x 5x 9x 31x 29x 14x 14x 29x 29x 20x 9x 2x 15x 15x 15x 24x 24x 5x 3x 19x 19x 15x 6x 2x 31x 9x 9x 12x 12x | import { posix } from "node:path";
import type { Finding } from "./findings.ts";
export interface SourceFile {
/** Relative to the app root, e.g. `layers/forms/shared/types/Challenge.ts`. */
path: string;
text: string;
}
export interface NuxtSources {
/** Repo-relative, e.g. `apps/huella-legal`. */
app: string;
files: SourceFile[];
}
type Side = "client" | "node";
const SPECIFIER = /(?:\bfrom|\bimport)\s*(?:\(\s*)?["']([^"']+)["']/g;
const EXPORTED =
/\bexport\s+(?:declare\s+)?(?:type|interface|const|let|function|enum|class)\s+(\w+)/g;
const COMMENT_OR_STRING = /\/\*[\s\S]*?\*\/|\/\/[^\n]*|"[^"\n]*"|'[^'\n]*'/g;
const SCRIPT = /(<script[^>]*>)([\s\S]*?)(<\/script>)/g;
// Nuxt auto-imports only the top level of these two, in the app root and in every layer.
const AUTO_IMPORTED = /(?:^|\/)shared\/(?:types|utils)\/[^/]+\.ts$/;
const isTest = (path: string): boolean => /\.(?:spec|test|stories)\.ts$/.test(path);
const roleOf = (path: string): Side | "shared" | undefined => {
if (isTest(path)) return undefined;
if (posix.basename(path) === "nuxt.config.ts") return "node";
for (const segment of path.split("/")) {
if (segment === "shared") return "shared";
if (segment === "app") return "client";
if (segment === "server" || segment === "tools" || segment === "modules") return "node";
}
return undefined;
};
const ALIASES: [string, string][] = [
["#layers/", "layers/"],
["#shared/", "shared/"],
["~~/", ""],
["@@/", ""],
["~/", "app/"],
["@/", "app/"],
];
const resolve = (specifier: string, from: string, known: Set<string>): string | undefined => {
let base: string | undefined;
if (specifier.startsWith(".")) {
base = posix.normalize(posix.join(posix.dirname(from), specifier));
} else {
const alias = ALIASES.find(([prefix]) => specifier.startsWith(prefix));
Eif (alias) base = alias[1] + specifier.slice(alias[0].length);
}
Iif (base === undefined) return undefined;
return [base, `${base}.ts`, `${base}/index.ts`].find((candidate) => known.has(candidate));
};
const blank = (code: string): string => code.replaceAll(COMMENT_OR_STRING, " ");
// A Vue template's expressions sit inside attribute quotes, so only its script blocks are blanked.
const codeOf = ({ path, text }: SourceFile): string =>
path.endsWith(".vue")
? text.replaceAll(SCRIPT, (_, open: string, body: string, close: string) =>
[open, blank(body), close].join(""),
)
: blank(text);
const mentions = (code: string, names: string[]): boolean =>
names.some((name) => new RegExp(`\\b${name}\\b`).test(code));
const importersOf = (files: SourceFile[], shared: SourceFile[]): Map<string, Set<string>> => {
const known = new Set(files.map(({ path }) => path));
const importers = new Map(shared.map(({ path }) => [path, new Set<string>()]));
const autoImported = shared
.filter(({ path }) => AUTO_IMPORTED.test(path))
.map(({ path, text }) => ({
path,
names: [...text.matchAll(EXPORTED)].map(([, n]) => n!),
}));
for (const file of files) {
if (roleOf(file.path) === undefined) continue;
for (const [, specifier] of file.text.matchAll(SPECIFIER)) {
const target = resolve(specifier!, file.path, known);
Eif (target !== undefined && target !== file.path) importers.get(target)?.add(file.path);
}
const code = codeOf(file);
for (const { path, names } of autoImported) {
if (path !== file.path && mentions(code, names)) importers.get(path)?.add(file.path);
}
}
return importers;
};
const sidesOf = (
path: string,
importers: Map<string, Set<string>>,
seen: Set<string> = new Set(),
): Set<Side> => {
const sides = new Set<Side>();
seen.add(path);
for (const importer of importers.get(path) ?? []) {
const role = roleOf(importer);
if (role === "shared") {
if (!seen.has(importer))
sidesOf(importer, importers, seen).forEach((s) => sides.add(s));
} else Eif (role !== undefined) {
sides.add(role);
}
}
return sides;
};
const oneSidedFinding = (path: string, side: Side): Finding => ({
id: "one-sided-shared",
title: "A shared/ file is used by one side only",
detail:
side === "client"
? `${path} is only reached from app/. shared/ is for code both app/ and server/ use, so it goes in that app/ or layer's app/types or app/utils.`
: `${path} is only reached from server/, tools/ or a nuxt.config. shared/ is for code both app/ and server/ use, so it goes in server/ or tools/.`,
evidence: [path],
blocks: true,
});
/** Every `shared/` file reached from exactly one of `app/` and the Node side, specs aside. */
export const oneSidedShared = ({ app, files }: NuxtSources): Finding[] => {
const shared = files.filter(({ path }) => path.endsWith(".ts") && roleOf(path) === "shared");
const importers = importersOf(files, shared);
return shared.flatMap(({ path }) => {
const sides = [...sidesOf(path, importers)];
return sides.length === 1 ? [oneSidedFinding(`${app}/${path}`, sides[0]!)] : [];
});
};
|