mirror of
https://github.com/Drop-OSS/drop-api-autogen.git
synced 2026-01-30 20:55:17 +01:00
45 lines
1.1 KiB
TypeScript
45 lines
1.1 KiB
TypeScript
import * as ts from "typescript";
|
|
|
|
export function useProgram(currentDir: string) {
|
|
const configFile = ts.findConfigFile(
|
|
currentDir,
|
|
ts.sys.fileExists,
|
|
"tsconfig.json"
|
|
);
|
|
if (!configFile) throw Error("tsconfig.json not found");
|
|
const { config } = ts.readConfigFile(configFile, ts.sys.readFile);
|
|
|
|
config.compilerOptions = Object.assign({}, config.compilerOptions);
|
|
|
|
const { options, fileNames, errors } = ts.parseJsonConfigFileContent(
|
|
config,
|
|
ts.sys,
|
|
currentDir
|
|
);
|
|
|
|
const program = ts.createProgram({
|
|
options,
|
|
rootNames: fileNames,
|
|
configFileParsingDiagnostics: errors,
|
|
});
|
|
|
|
return program;
|
|
}
|
|
|
|
export function searchForSymbol(
|
|
checker: ts.TypeChecker,
|
|
node: ts.Node,
|
|
targetSymbol: ts.Symbol
|
|
): ts.Node | undefined {
|
|
const symbol = checker.getSymbolAtLocation(node);
|
|
if (symbol && symbol.escapedName == targetSymbol.escapedName) return node;
|
|
|
|
const children = node.getChildren();
|
|
for (const child of children) {
|
|
const recursiveSearch = searchForSymbol(checker, child, targetSymbol);
|
|
if (recursiveSearch) return recursiveSearch;
|
|
}
|
|
|
|
return undefined;
|
|
}
|