(safe: string, open: number)
| 389 | * way (a function body, for the Elysia resolver). |
| 390 | */ |
| 391 | export function scanCall(safe: string, open: number): { close: number; structure: string } | null { |
| 392 | const oc = safe[open]; |
| 393 | const cc = oc === '{' ? '}' : oc === '[' ? ']' : ')'; |
| 394 | const out: string[] = []; |
| 395 | let depth = 0; |
| 396 | for (let i = open; i < safe.length; i++) { |
| 397 | const c = safe[i]!; |
| 398 | if ((c === '"' || c === "'") && !closesOnLine(safe, i)) { |
| 399 | // JSX text (`<p>Don't</p>`), not a string: an apostrophe with no |
| 400 | // closing quote on its line must not blank the `)` after it. |
| 401 | out.push(c); |
| 402 | continue; |
| 403 | } |
| 404 | if (c === '"' || c === "'" || c === '`') { |
| 405 | out.push(c); |
| 406 | for (i++; i < safe.length && safe[i] !== c; i++) { |
| 407 | if (c !== '`' && safe[i] === '\n') break; |
| 408 | if (safe[i] === '\\' && i + 1 < safe.length) { |
| 409 | out.push(' '); |
| 410 | i++; |
| 411 | } |
| 412 | out.push(safe[i] === '\n' ? '\n' : ' '); |
| 413 | } |
| 414 | if (i < safe.length) out.push(safe[i]!); |
| 415 | continue; |
| 416 | } |
| 417 | if (c === '/' && safe[i + 1] !== '/' && safe[i + 1] !== '*') { |
| 418 | const end = regexLiteralEndAt(safe, i); |
| 419 | if (end > i) { |
| 420 | out.push('/', ...' '.repeat(end - i - 1), '/'); |
| 421 | i = end; |
| 422 | continue; |
| 423 | } |
| 424 | } |
| 425 | out.push(c); |
| 426 | if (c === oc) depth++; |
| 427 | else if (c === cc) { |
| 428 | depth--; |
| 429 | if (depth === 0) return { close: i, structure: out.join('') }; |
| 430 | } |
| 431 | } |
| 432 | return null; |
| 433 | } |
| 434 | |
| 435 | /** Whether the quote at `i` has an unescaped closing quote on the same line. */ |
| 436 | export function closesOnLine(s: string, i: number): boolean { |
no test coverage detected