( source: string, path?: string, )
| 432 | } |
| 433 | |
| 434 | export function detectUserScriptSecretOptions( |
| 435 | source: string, |
| 436 | path?: string, |
| 437 | ): UserScriptSecretOptionDetection { |
| 438 | const sourceToInspect = |
| 439 | path && MARKDOWN_FILE_EXTENSION_REGEX.test(path) |
| 440 | ? (extractScriptFromMarkdown(source).code ?? "") |
| 441 | : source; |
| 442 | const names = new Set<string>(); |
| 443 | let foundSecretOptions = false; |
| 444 | const constants = collectStringConstants(sourceToInspect); |
| 445 | |
| 446 | for (const { start, end } of findOptionsObjectSpans(sourceToInspect)) { |
| 447 | let current = start; |
| 448 | |
| 449 | while (current < end) { |
| 450 | current = skipWhitespaceAndComments(sourceToInspect, current); |
| 451 | if (sourceToInspect[current] === ",") { |
| 452 | current += 1; |
| 453 | continue; |
| 454 | } |
| 455 | if (current >= end) break; |
| 456 | |
| 457 | const key = readOptionKey(sourceToInspect, current, constants); |
| 458 | if (!key) { |
| 459 | current += 1; |
| 460 | continue; |
| 461 | } |
| 462 | |
| 463 | current = skipWhitespaceAndComments(sourceToInspect, key.end); |
| 464 | if (sourceToInspect[current] !== ":") { |
| 465 | current += 1; |
| 466 | continue; |
| 467 | } |
| 468 | |
| 469 | current = skipWhitespaceAndComments(sourceToInspect, current + 1); |
| 470 | if (sourceToInspect[current] !== "{") { |
| 471 | current += 1; |
| 472 | continue; |
| 473 | } |
| 474 | |
| 475 | const valueEnd = findMatchingDelimiter( |
| 476 | sourceToInspect, |
| 477 | current, |
| 478 | "{", |
| 479 | "}", |
| 480 | ); |
| 481 | if (valueEnd === -1 || valueEnd > end) break; |
| 482 | |
| 483 | const body = sourceToInspect.slice(current + 1, valueEnd); |
| 484 | if (optionBodyDeclaresSecret(body)) { |
| 485 | foundSecretOptions = true; |
| 486 | if (key.name) names.add(key.name); |
| 487 | } |
| 488 | |
| 489 | current = valueEnd + 1; |
| 490 | } |
| 491 | } |
no test coverage detected