index.js
1.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
module.exports = diff => {
const lines = diff.split('\n')
const trim = []
// first, scan through to find the --- and +++ lines
// --- expected
// +++ actual
let header = 0
for (let i = 0; i < lines.length && header === 0; i++) {
const line = lines[i]
trim.push(line)
if (/^---/.test(line) && /^\+\+\+/.test(lines[i - 1]) ||
/^\+\+\+/.test(line) && /^---/.test(lines[i - 1])) {
header = i + 1
}
}
let actualLine = 0
let expectLine = 0
// context line is like:
// @@ -${expLine},${expLineInc} +${objLine},${objLinesInc} ${context}
let context = '' // last line that was not indented
for (let i = header; i < lines.length; i++) {
const line = lines[i]
actualLine ++
expectLine ++
if (/^ [^ \t\s]/.test(line) && line.trim()) {
context = line.slice(1).trim()
if (context.length > 30)
context = context.slice(0, 27) + '...'
continue
}
if (/^[-+]/.test(line)) {
// start of a diff section
const sectionStart = Math.max(i - 3, header) // line offset to start section
const actualStart = Math.max(actualLine - 3, 1)
const expectStart = Math.max(expectLine - 3, 1)
let trailingContext = 0
while (i < lines.length && trailingContext < 3) {
const line = lines[i++]
if (!line)
continue
const pm = line.match(/(^[-+ ])/)[1]
if (pm === ' ') {
trailingContext ++
expectLine ++
actualLine ++
} else {
trailingContext = 0
if (pm === '-')
expectLine ++
else
actualLine ++
}
}
const prefix = `@@ -${
expectStart
},${
expectLine - expectStart
} +${
actualStart
},${
actualLine - actualStart
} @@ ${context}`
trim.push(prefix, ...lines.slice(sectionStart, i))
}
}
return trim.join('\n')
}