( edits1: FileEdit[], edits2: FileEdit[], originalContent: string, )
| 662 | * This handles cases where edits might be different but produce the same outcome. |
| 663 | */ |
| 664 | export function areFileEditsEquivalent( |
| 665 | edits1: FileEdit[], |
| 666 | edits2: FileEdit[], |
| 667 | originalContent: string, |
| 668 | ): boolean { |
| 669 | // Fast path: check if edits are literally identical |
| 670 | if ( |
| 671 | edits1.length === edits2.length && |
| 672 | edits1.every((edit1, index) => { |
| 673 | const edit2 = edits2[index] |
| 674 | return ( |
| 675 | edit2 !== undefined && |
| 676 | edit1.old_string === edit2.old_string && |
| 677 | edit1.new_string === edit2.new_string && |
| 678 | edit1.replace_all === edit2.replace_all |
| 679 | ) |
| 680 | }) |
| 681 | ) { |
| 682 | return true |
| 683 | } |
| 684 | |
| 685 | // Try applying both sets of edits |
| 686 | let result1: { patch: StructuredPatchHunk[]; updatedFile: string } | null = |
| 687 | null |
| 688 | let error1: string | null = null |
| 689 | let result2: { patch: StructuredPatchHunk[]; updatedFile: string } | null = |
| 690 | null |
| 691 | let error2: string | null = null |
| 692 | |
| 693 | try { |
| 694 | result1 = getPatchForEdits({ |
| 695 | filePath: 'temp', |
| 696 | fileContents: originalContent, |
| 697 | edits: edits1, |
| 698 | }) |
| 699 | } catch (e) { |
| 700 | error1 = errorMessage(e) |
| 701 | } |
| 702 | |
| 703 | try { |
| 704 | result2 = getPatchForEdits({ |
| 705 | filePath: 'temp', |
| 706 | fileContents: originalContent, |
| 707 | edits: edits2, |
| 708 | }) |
| 709 | } catch (e) { |
| 710 | error2 = errorMessage(e) |
| 711 | } |
| 712 | |
| 713 | // If both threw errors, they're equal only if the errors are the same |
| 714 | if (error1 !== null && error2 !== null) { |
| 715 | // Normalize error messages for comparison |
| 716 | return error1 === error2 |
| 717 | } |
| 718 | |
| 719 | // If one threw an error and the other didn't, they're not equal |
| 720 | if (error1 !== null || error2 !== null) { |
| 721 | return false |
no test coverage detected