Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions release-notes.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

- [#697](https://github.com/kpdecker/jsdiff/pull/697) *`diffJson` now correctly handles JSON objects containing a key named `__proto__`*. (Previously, the returned diff would be as if the `__proto__` key did not exist on either of the objects being diffed.)
- [#700](https://github.com/kpdecker/jsdiff/pull/700) *`diffJson` now correctly handles JSON objects containing a non-callable property named `toJSON`* - i.e. it gives such a property no special behaviour whatsoever, just as `JSON.stringify` doesn't. Previously, such properties caused an error to be thrown. (*Callable* `toJSON` properties continue to get the same special behaviour that `JSON.stringify` gives them.)
- [#701](https://github.com/kpdecker/jsdiff/pull/701) *`applyPatch` with `autoConvertLineEndings` on will no longer consider a stray `\r` character occurring at the end of a file without a terminating `\n` character to be a Windows line ending*, and so will no longer strip it when converting from Windows to Unix line endings or fail to apply a Unix-style patch to a Windows file when the patch introduces such a stray `\r`.

## 9.0.0

Expand Down
2 changes: 1 addition & 1 deletion src/patch/line-endings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ export function isUnix(patch: StructuredPatch | StructuredPatch[]): boolean {
return !patch.some(
index => index.hunks.some(
hunk => hunk.lines.some(
line => !line.startsWith('\\') && line.endsWith('\r')
(line, i) => !line.startsWith('\\') && line.endsWith('\r') && !hunk.lines[i + 1]?.startsWith('\\')
)
)
);
Expand Down
13 changes: 13 additions & 0 deletions test/patch/apply.js
Original file line number Diff line number Diff line change
Expand Up @@ -1422,6 +1422,19 @@ describe('patch/apply', function() {
.to.equal('');
});

it('should correctly apply a Unix patch whose final added line ends with a literal \\r (no newline at EOF) to a Windows file', () => {
// The patch is Unix-style (no \\r\\n line endings), but the added line's content ends with a
// literal '\\r' because the new file has no trailing newline. autoConvertLineEndings must
// recognise the patch as Unix (not Windows), convert it to match the CRLF source, and apply
// it correctly — without dropping the literal '\\r'. Previously, isUnix() returned false for
// such a patch, so no conversion was attempted and applyPatch returned false.
const oldFileUnix = 'line1\nline2\n';
const newFileUnix = 'line1\nline3\r'; // final line has literal CR and no trailing newline
const patch = structuredPatch('test', 'test', oldFileUnix, newFileUnix, undefined, undefined, {context: 0});
const oldFileWin = 'line1\r\nline2\r\n';
expect(applyPatch(oldFileWin, patch)).to.equal('line1\r\nline3\r');
});

it('should automatically convert a patch with Unix file endings to Windows when patching a Windows file', () => {
const oldFile = 'foo\r\nbar\r\nbaz\r\nqux\r\n';
const diffFile =
Expand Down
15 changes: 15 additions & 0 deletions test/patch/line-endings.js
Original file line number Diff line number Diff line change
Expand Up @@ -148,4 +148,19 @@ describe('isUnix', () => {
);
expect(isUnix(patch)).to.equal(true);
});

it('should still return true if only the last line in a file has a CR and there is a no newline at EOF indicator', () => {
const patch = parsePatch(
'Index: test\n'
+ '===================================================================\n'
+ '--- test\theader1\n'
+ '+++ test\theader2\n'
+ '@@ -1,2 +1,3 @@\n'
+ ' line2\n'
+ ' line3\n'
+ '+line4\r\n'
+ '\\ No newline at end of file\n'
);
expect(isUnix(patch)).to.equal(true);
});
});