Skip to content

Commit 2c80e1d

Browse files
committed
new tool: find-bad-imports.ts to catch bad import statements in our codebase
1 parent 022103b commit 2c80e1d

File tree

1 file changed

+51
-0
lines changed

1 file changed

+51
-0
lines changed

tools/find-bad-imports.ts

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
// find-bad-imports
2+
//
3+
// uses `deno info --json` to find all imports that are not in our
4+
// standard format.
5+
6+
const entryPoints = [
7+
"package/src/bld.ts",
8+
"src/quarto.ts",
9+
];
10+
11+
let foundBadImport = false;
12+
13+
for (const entry of entryPoints) {
14+
// this only works if you have deno installed!
15+
const cmd = new Deno.Command("deno", {
16+
args: ["info", "--json", entry],
17+
stdout: "piped",
18+
stderr: "piped",
19+
});
20+
21+
const output = cmd.outputSync();
22+
23+
if (!output.success) {
24+
console.log(new TextDecoder().decode(output.stderr));
25+
Deno.exit(1);
26+
}
27+
28+
const info = JSON.parse(new TextDecoder().decode(output.stdout));
29+
const modules = info.modules || [];
30+
for (const module of modules) {
31+
if ((module.local || "").match("src/vendor")) {
32+
// don't analyze vendor code
33+
continue;
34+
}
35+
for (const dep of module.dependencies || []) {
36+
const code = dep.code || {};
37+
if (((code.specifier || "") as string).match("src/vendor")) {
38+
foundBadImport = true;
39+
console.log(
40+
`Bad import in ${module.local}:${code.span.start.line + 1}(${
41+
code.span.start.character + 1
42+
}--${code.span.end.character + 1})`,
43+
);
44+
}
45+
}
46+
}
47+
}
48+
49+
if (foundBadImport) {
50+
Deno.exit(1);
51+
}

0 commit comments

Comments
 (0)