Skip to content

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

Closed
@yev-yev-yev

Description

@yev-yev-yev

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

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions