( projectRoot: string, filePaths: string[], readFile?: SourceReader, )
| 61 | } |
| 62 | |
| 63 | export async function getFileTokenScores( |
| 64 | projectRoot: string, |
| 65 | filePaths: string[], |
| 66 | readFile?: SourceReader, |
| 67 | ): Promise<FileTokenData> { |
| 68 | const startTime = Date.now() |
| 69 | const tokenScores: Record<string, Record<string, number>> = {} |
| 70 | const externalCalls: Record<string, number> = {} |
| 71 | const fileCallsMap = new Map<string, string[]>() |
| 72 | let parsedFiles = 0 |
| 73 | let totalParsedBytes = 0 |
| 74 | |
| 75 | for (const filePath of filePaths) { |
| 76 | if ( |
| 77 | parsedFiles >= MAX_PARSE_FILES || |
| 78 | totalParsedBytes >= MAX_TOTAL_PARSE_BYTES |
| 79 | ) { |
| 80 | break |
| 81 | } |
| 82 | |
| 83 | const fullPath = path.join(projectRoot, filePath) |
| 84 | const languageConfig = await getLanguageConfig(fullPath) |
| 85 | if (!languageConfig) continue |
| 86 | |
| 87 | const parsed = await parseTokensForScoring({ |
| 88 | filePath, |
| 89 | fullPath, |
| 90 | languageConfig, |
| 91 | readFile, |
| 92 | remainingBytes: MAX_TOTAL_PARSE_BYTES - totalParsedBytes, |
| 93 | }) |
| 94 | if (parsed.skipped) continue |
| 95 | |
| 96 | parsedFiles++ |
| 97 | totalParsedBytes += parsed.bytes |
| 98 | |
| 99 | const { scores, calls } = scoreFileTokens(fullPath, parsed) |
| 100 | tokenScores[filePath] = scores |
| 101 | fileCallsMap.set(filePath, calls) |
| 102 | |
| 103 | for (const call of calls) { |
| 104 | if (!scores[call]) { |
| 105 | externalCalls[call] = (externalCalls[call] ?? 0) + 1 |
| 106 | } |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | const tokenCallers = buildTokenCallers(tokenScores, fileCallsMap) |
| 111 | boostScoresByExternalCalls(tokenScores, externalCalls) |
| 112 | |
| 113 | if (DEBUG_PARSING) { |
| 114 | const endTime = Date.now() |
| 115 | console.log(`Parsed ${filePaths.length} files in ${endTime - startTime}ms`) |
| 116 | |
| 117 | try { |
| 118 | fs.writeFileSync( |
| 119 | '../debug/debug-parse.json', |
| 120 | JSON.stringify({ |
no test coverage detected