Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

knip-schemas.ts

94 lines3.5 KB

1// @effect-diagnostics nodeBuiltinImport:off - Knip and the TypeScript compiler host use synchronous Node paths.
2import * as NodePath from "node:path";
3import type { Preprocessor } from "knip";
4// Knip needs the legacy compiler API, which TypeScript 7 no longer exports.
5import ts from "typescript-legacy";
6​
7const schemaTypeId = "~effect/Schema/Schema";
8​
9const preprocess: Preprocessor = (options) => {
10 const categories = ["exports", "nsExports", "duplicates"] as const;
11 const projects = new Map<string | undefined, Set<string>>();
12 for (const category of categories) {
13 for (const [filePath, issues] of Object.entries(options.issues[category])) {
14 if (Object.keys(issues).length === 0) continue;
15 const configPath = ts.findConfigFile(
16 NodePath.dirname(NodePath.resolve(options.cwd, filePath)),
17 ts.sys.fileExists,
18 );
19 const files = projects.get(configPath) ?? new Set<string>();
20 files.add(filePath);
21 projects.set(configPath, files);
22 }
23 }
24​
25 for (const [configPath, files] of projects) {
26 const config = configPath
27 ? ts.getParsedCommandLineOfConfigFile(
28 configPath,
29 {},
30 {
31 ...ts.sys,
32 onUnRecoverableConfigFileDiagnostic: (diagnostic) => {
33 throw new Error(ts.flattenDiagnosticMessageText(diagnostic.messageText, "\n"));
34 },
35 },
36 )
37 : undefined;
38 if (config?.errors.length) {
39 throw new Error(
40 config.errors
41 .map((error) => ts.flattenDiagnosticMessageText(error.messageText, "\n"))
42 .join("\n"),
43 );
44 }
45 const program = ts.createProgram(
46 [...files].map((file) => NodePath.resolve(options.cwd, file)),
47 {
48 module: ts.ModuleKind.NodeNext,
49 allowJs: true,
50 ...config?.options,
51 noEmit: true,
52 },
53 );
54 const checker = program.getTypeChecker();
55 for (const filePath of files) {
56 const source = program.getSourceFile(NodePath.resolve(options.cwd, filePath));
57 const module = source && checker.getSymbolAtLocation(source);
58 if (!source || !module) continue;
59 const schemas = new Set(
60 checker.getExportsOfModule(module).flatMap((symbol) => {
61 const exported =
62 symbol.flags & ts.SymbolFlags.Alias ? checker.getAliasedSymbol(symbol) : symbol;
63 const target =
64 exported.flags & ts.SymbolFlags.Value
65 ? exported
66 : checker.resolveName(symbol.name, source, ts.SymbolFlags.Value, false);
67 if (!target) return [];
68 const type = checker.getTypeOfSymbolAtLocation(target, target.valueDeclaration ?? source);
69 const marker = type.getProperty(schemaTypeId);
70 if (!marker) return [];
71 const markerType = checker.getTypeOfSymbolAtLocation(marker, source);
72 return markerType.isStringLiteral() && markerType.value === schemaTypeId
73 ? [symbol.name]
74 : [];
75 }),
76 );
77 for (const category of categories) {
78 const issues = options.issues[category][filePath];
79 if (!issues) continue;
80 for (const [key, issue] of Object.entries(issues)) {
81 const symbols = issue.symbols ?? [{ symbol: issue.symbol }];
82 if (symbols.length > 0 && symbols.every(({ symbol }) => schemas.has(symbol))) {
83 delete issues[key];
84 options.counters[category]--;
85 }
86 }
87 if (Object.keys(issues).length === 0) delete options.issues[category][filePath];
88 }
89 }
90 }
91 return options;
92};
93​
94export default preprocess;