( lines: string[], input: string, )
| 347 | } |
| 348 | |
| 349 | function parseUpdateDiff( |
| 350 | lines: string[], |
| 351 | input: string, |
| 352 | ): { chunks: Chunk[]; fuzz: number } { |
| 353 | const parser: ParserState = { |
| 354 | lines: [...lines, END_PATCH], |
| 355 | index: 0, |
| 356 | fuzz: 0, |
| 357 | } |
| 358 | |
| 359 | const inputLines = input.split('\n') |
| 360 | const chunks: Chunk[] = [] |
| 361 | let cursor = 0 |
| 362 | |
| 363 | while (!isDone(parser, END_SECTION_MARKERS)) { |
| 364 | const current = parser.lines[parser.index] |
| 365 | const line = typeof current === 'string' ? current : '' |
| 366 | |
| 367 | let anchor = '' |
| 368 | const hasBareHeader = line === '@@' |
| 369 | const hasWrappedHeader = isWrappedAtHeader(line) |
| 370 | const hasAnchorHeader = line.startsWith('@@ ') && !hasWrappedHeader |
| 371 | const hasAnyHeader = hasBareHeader || hasWrappedHeader || hasAnchorHeader |
| 372 | |
| 373 | if (hasAnchorHeader) { |
| 374 | anchor = line.slice(3) |
| 375 | parser.index += 1 |
| 376 | } else if (hasBareHeader || hasWrappedHeader) { |
| 377 | parser.index += 1 |
| 378 | } |
| 379 | |
| 380 | if (!(hasAnyHeader || cursor === 0)) { |
| 381 | throw new Error(`Invalid Line:\n${parser.lines[parser.index]}`) |
| 382 | } |
| 383 | |
| 384 | if (anchor.trim()) { |
| 385 | cursor = advanceCursorToAnchor(anchor, inputLines, cursor, parser) |
| 386 | } |
| 387 | |
| 388 | const { nextContext, sectionChunks, endIndex, eof } = readSection( |
| 389 | parser.lines, |
| 390 | parser.index, |
| 391 | ) |
| 392 | |
| 393 | const { newIndex, fuzz } = findContext(inputLines, nextContext, cursor, eof) |
| 394 | |
| 395 | if (newIndex === -1) { |
| 396 | const nextContextText = nextContext.join('\n') |
| 397 | if (eof) { |
| 398 | throw new Error(`Invalid EOF Context ${cursor}:\n${nextContextText}`) |
| 399 | } |
| 400 | |
| 401 | throw new Error(`Invalid Context ${cursor}:\n${nextContextText}`) |
| 402 | } |
| 403 | |
| 404 | parser.fuzz += fuzz |
| 405 | for (const chunk of sectionChunks) { |
| 406 | chunks.push({ ...chunk, origIndex: chunk.origIndex + newIndex }) |
no test coverage detected