( filePath: string, languageConfig: LanguageConfig, readFile: ((filePath: string) => string | null) | undefined, options: ParseTokensOptions, )
| 180 | } |
| 181 | |
| 182 | function parseTokensWithLimits( |
| 183 | filePath: string, |
| 184 | languageConfig: LanguageConfig, |
| 185 | readFile: ((filePath: string) => string | null) | undefined, |
| 186 | options: ParseTokensOptions, |
| 187 | ): ParsedTokensForScoring { |
| 188 | const { parser, query } = languageConfig |
| 189 | |
| 190 | try { |
| 191 | const maxBytes = options.maxBytes ?? MAX_PARSE_FILE_BYTES |
| 192 | const remainingBytes = options.remainingBytes ?? MAX_TOTAL_PARSE_BYTES |
| 193 | if (remainingBytes <= 0) { |
| 194 | return emptyParsedTokens(true) |
| 195 | } |
| 196 | |
| 197 | const source = loadSourceWithinLimits({ |
| 198 | filePath, |
| 199 | readFile, |
| 200 | maxBytes, |
| 201 | remainingBytes, |
| 202 | }) |
| 203 | if (!source) { |
| 204 | return emptyParsedTokens(true) |
| 205 | } |
| 206 | |
| 207 | if (!parser || !query) { |
| 208 | throw new Error('Parser or query not found') |
| 209 | } |
| 210 | |
| 211 | const parseResults = parseFile(parser, query, source.code) |
| 212 | const identifiers = Array.from(new Set(parseResults.identifier)) |
| 213 | const calls = Array.from(new Set(parseResults['call.identifier'])) |
| 214 | |
| 215 | if (DEBUG_PARSING) { |
| 216 | console.log(`\nParsing ${filePath}:`) |
| 217 | console.log('Identifiers:', identifiers) |
| 218 | console.log('Calls:', calls) |
| 219 | } |
| 220 | |
| 221 | return { |
| 222 | numLines: countLines(source.code), |
| 223 | identifiers: identifiers ?? [], |
| 224 | calls: calls ?? [], |
| 225 | bytes: source.bytes, |
| 226 | skipped: false, |
| 227 | } |
| 228 | } catch (e) { |
| 229 | if (DEBUG_PARSING) { |
| 230 | console.error(`Error parsing query: ${e}`) |
| 231 | console.log(filePath) |
| 232 | } |
| 233 | return emptyParsedTokens(false) |
| 234 | } |
| 235 | } |
| 236 | |
| 237 | function loadSourceWithinLimits(params: { |
| 238 | filePath: string |
no test coverage detected