kpdecker / jsdiff

A javascript text differencing implementation.
BSD 3-Clause "New" or "Revised" License
7.92k stars 496 forks source link

Bug w/ `createPatch()` when modifying file ending without new line #531

Closed yev-yev-yev closed 1 month ago

yev-yev-yev commented 2 months ago

Minimum code to reproduce

const { createPatch } = require('diff');

const oldContent = `1st line.\n2nd line.\n3rd line.`;
const newContent = `Z11 thing.\nA New thing.\n2nd line.\nNEW LINE.\n3rd line.\n\nSOMETHING ELSE.`;

const diff = createPatch(
  'a.txt',
  oldContent,
  newContent,
  undefined,
  undefined,
  { context: 3 },
);

console.log(diff);

Actual Result

===================================================================
--- a.txt
+++ a.txt
@@ -1,3 +1,7 @@
-1st line.
+Z11 thing.
+A New thing.
\ No newline at end of file
 2nd line.
-3rd line.
+NEW LINE.
+3rd line.
+
+SOMETHING ELSE.
\ No newline at end of file

Expected Result

===================================================================
--- a.txt
+++ a.txt
@@ -1,3 +1,7 @@
-1st line.
+Z11 thing.
+A New thing.
 2nd line.
-3rd line.
\ No newline at end of file
+NEW LINE.
+3rd line.
+
+SOMETHING ELSE.
\ No newline at end of file