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

knip-schemas.ts

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