MCPcopy Create free account
hub / github.com/webpro-nl/knip / getModuleSourcePathHandler

Function getModuleSourcePathHandler

packages/knip/src/util/to-source-path.ts:85–108  ·  view source on GitHub ↗
(chief: ConfigurationChief)

Source from the content-addressed store, hash-verified

83};
84
85export const getModuleSourcePathHandler = (chief: ConfigurationChief) => {
86 const toSourceMapCache = new Map<string, string | undefined>();
87
88 return (filePath: string) => {
89 if (!isInternal(filePath) || hasTSExt.test(filePath)) return;
90 if (toSourceMapCache.has(filePath)) return toSourceMapCache.get(filePath);
91 const workspace = chief.findWorkspaceByFilePath(filePath);
92 let result: string | undefined;
93 if (workspace?.sourceMaps) {
94 for (const { srcDir, outDir } of workspace.sourceMaps) {
95 if (!(isUnderOutDir(filePath, outDir) || srcDir === outDir)) continue;
96 const basePath = (srcDir + filePath.slice(outDir.length)).replace(matchExt, '');
97 const srcFilePath = findFileWithExtensions(basePath, sourceExtensions);
98 if (srcFilePath && srcFilePath !== filePath) {
99 debugLog('*', `Source mapping ${toRelative(filePath, chief.cwd)} → ${toRelative(srcFilePath, chief.cwd)}`);
100 result = srcFilePath;
101 break;
102 }
103 }
104 }
105 toSourceMapCache.set(filePath, result);
106 return result;
107 };
108};
109
110export const getToSourcePathsHandler = (chief: ConfigurationChief) => {
111 return async (specifiers: Set<string>, dir: string, extensions = defaultExtensions, label: string) => {

Callers 1

runFunction · 0.90

Calls 6

isInternalFunction · 0.90
findFileWithExtensionsFunction · 0.90
toRelativeFunction · 0.90
isUnderOutDirFunction · 0.85
setMethod · 0.80

Tested by

no test coverage detected