Files
DecDuck f26e7f1a45 Initial commit
Created the barebones of the tooling.

Design comes next.
2025-08-09 20:19:16 +10:00

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;
}