Skip to content

Commit b96afe9

Browse files
authoredFeb 28, 2023
add missing eof line support (#11)
* add missing eof line support * run prettier * change to message line, update readme
1 parent 83bc2ef commit b96afe9

File tree

8 files changed

+250
-1
lines changed

8 files changed

+250
-1
lines changed
 

‎README.md

+116
Original file line numberDiff line numberDiff line change
@@ -468,6 +468,122 @@ index 0e05564..aa39060 100644
468468

469469
<!-- end:deleted-line-output -->
470470

471+
<!-- start:message-line-input -->
472+
473+
```diff
474+
diff --git a/rename.md b/rename.md
475+
index 0e05564..aa39060 100644
476+
--- a/rename.md
477+
+++ b/rename.md
478+
@@ -1,2 +1 @@
479+
newfile
480+
-newline
481+
+newline
482+
\ No newline at end of file
483+
diff --git a/rename2.md b/rename2.md
484+
index 0e05564..aa39060 100644
485+
--- a/rename2.md
486+
+++ b/rename2.md
487+
@@ -1,2 +1 @@
488+
newfile2
489+
-newline2
490+
+newline2
491+
\ No newline at end of file
492+
493+
```
494+
495+
<!-- end:message-line-input -->
496+
497+
<!-- start:message-line-output -->
498+
499+
```json
500+
{
501+
"type": "GitDiff",
502+
"files": [
503+
{
504+
"type": "ChangedFile",
505+
"chunks": [
506+
{
507+
"type": "Chunk",
508+
"toFileRange": {
509+
"start": 1,
510+
"lines": 1
511+
},
512+
"fromFileRange": {
513+
"start": 1,
514+
"lines": 2
515+
},
516+
"changes": [
517+
{
518+
"type": "UnchangedLine",
519+
"lineBefore": 1,
520+
"lineAfter": 1,
521+
"content": "newfile"
522+
},
523+
{
524+
"type": "DeletedLine",
525+
"lineBefore": 2,
526+
"content": "newline"
527+
},
528+
{
529+
"type": "AddedLine",
530+
"lineAfter": 2,
531+
"content": "newline"
532+
},
533+
{
534+
"type": "MessageLine",
535+
"content": "No newline at end of file"
536+
}
537+
]
538+
}
539+
],
540+
"path": "rename.md"
541+
},
542+
{
543+
"type": "ChangedFile",
544+
"chunks": [
545+
{
546+
"type": "Chunk",
547+
"toFileRange": {
548+
"start": 1,
549+
"lines": 1
550+
},
551+
"fromFileRange": {
552+
"start": 1,
553+
"lines": 2
554+
},
555+
"changes": [
556+
{
557+
"type": "UnchangedLine",
558+
"lineBefore": 1,
559+
"lineAfter": 1,
560+
"content": "newfile2"
561+
},
562+
{
563+
"type": "DeletedLine",
564+
"lineBefore": 2,
565+
"content": "newline2"
566+
},
567+
{
568+
"type": "AddedLine",
569+
"lineAfter": 2,
570+
"content": "newline2"
571+
},
572+
{
573+
"type": "MessageLine",
574+
"content": "No newline at end of file"
575+
}
576+
]
577+
}
578+
],
579+
"path": "rename2.md"
580+
}
581+
]
582+
}
583+
```
584+
585+
<!-- end:message-line-output -->
586+
471587
</details>
472588

473589
## AST Format

‎scripts/build-readme.js

+1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ const FIXTURES = [
1313
'conflict-file',
1414
'new-line',
1515
'deleted-line',
16+
'message-line',
1617
];
1718

1819
let readmeReplacer = replacer().content(README_INPUT);

‎src/__fixtures__/message-line

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
diff --git a/rename.md b/rename.md
2+
index 0e05564..aa39060 100644
3+
--- a/rename.md
4+
+++ b/rename.md
5+
@@ -1,2 +1 @@
6+
newfile
7+
-newline
8+
+newline
9+
\ No newline at end of file
10+
diff --git a/rename2.md b/rename2.md
11+
index 0e05564..aa39060 100644
12+
--- a/rename2.md
13+
+++ b/rename2.md
14+
@@ -1,2 +1 @@
15+
newfile2
16+
-newline2
17+
+newline2
18+
\ No newline at end of file
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`message-line parse \`message-line\` 1`] = `
4+
{
5+
"files": [
6+
{
7+
"chunks": [
8+
{
9+
"changes": [
10+
{
11+
"content": "newfile",
12+
"lineAfter": 1,
13+
"lineBefore": 1,
14+
"type": "UnchangedLine",
15+
},
16+
{
17+
"content": "newline",
18+
"lineBefore": 2,
19+
"type": "DeletedLine",
20+
},
21+
{
22+
"content": "newline",
23+
"lineAfter": 2,
24+
"type": "AddedLine",
25+
},
26+
{
27+
"content": "No newline at end of file",
28+
"type": "MessageLine",
29+
},
30+
],
31+
"fromFileRange": {
32+
"lines": 2,
33+
"start": 1,
34+
},
35+
"toFileRange": {
36+
"lines": 1,
37+
"start": 1,
38+
},
39+
"type": "Chunk",
40+
},
41+
],
42+
"path": "rename.md",
43+
"type": "ChangedFile",
44+
},
45+
{
46+
"chunks": [
47+
{
48+
"changes": [
49+
{
50+
"content": "newfile2",
51+
"lineAfter": 1,
52+
"lineBefore": 1,
53+
"type": "UnchangedLine",
54+
},
55+
{
56+
"content": "newline2",
57+
"lineBefore": 2,
58+
"type": "DeletedLine",
59+
},
60+
{
61+
"content": "newline2",
62+
"lineAfter": 2,
63+
"type": "AddedLine",
64+
},
65+
{
66+
"content": "No newline at end of file",
67+
"type": "MessageLine",
68+
},
69+
],
70+
"fromFileRange": {
71+
"lines": 2,
72+
"start": 1,
73+
},
74+
"toFileRange": {
75+
"lines": 1,
76+
"start": 1,
77+
},
78+
"type": "Chunk",
79+
},
80+
],
81+
"path": "rename2.md",
82+
"type": "ChangedFile",
83+
},
84+
],
85+
"type": "GitDiff",
86+
}
87+
`;

‎src/__tests__/message-line.test.ts

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import { getFixture } from './test-utils';
2+
import parseGitDiff from '../parse-git-diff';
3+
4+
describe('message-line', () => {
5+
const fixture = getFixture('message-line');
6+
7+
it('parse `message-line`', () => {
8+
expect(parseGitDiff(fixture)).toMatchSnapshot();
9+
});
10+
});

‎src/constants.ts

+1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ export const LineType = {
22
Added: 'AddedLine',
33
Deleted: 'DeletedLine',
44
Unchanged: 'UnchangedLine',
5+
Message: 'MessageLine',
56
} as const;
67

78
export const FileType = {

‎src/parse-git-diff.ts

+8
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,7 @@ const CHAR_TYPE_MAP: Record<string, LineType> = {
244244
'+': LineType.Added,
245245
'-': LineType.Deleted,
246246
' ': LineType.Unchanged,
247+
'\\': LineType.Message,
247248
};
248249

249250
function parseChanges(
@@ -291,6 +292,13 @@ function parseChanges(
291292
};
292293
break;
293294
}
295+
case LineType.Message: {
296+
change = {
297+
type,
298+
content: content.trim(),
299+
};
300+
break;
301+
}
294302
}
295303
changes.push(change);
296304
}

‎src/types.ts

+9-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,15 @@ export interface UnchangedLine extends BaseChange<typeof LineType.Unchanged> {
2121
lineAfter: number;
2222
}
2323

24-
export type AnyLineChange = AddedLine | DeletedLine | UnchangedLine;
24+
export interface MessageLine extends BaseChange<typeof LineType.Message> {
25+
content: string;
26+
}
27+
28+
export type AnyLineChange =
29+
| AddedLine
30+
| DeletedLine
31+
| UnchangedLine
32+
| MessageLine;
2533

2634
export interface ChunkRange {
2735
start: number;

0 commit comments

Comments
 (0)
Please sign in to comment.