Skip to content

Commit 954643a

Browse files
committed
fix(compiler): infinite loop in generateCodeFrame (vuejs#10547)
1 parent d2db6af commit 954643a

File tree

1 file changed

+12
-7
lines changed

1 file changed

+12
-7
lines changed

src/compiler/codeframe.js

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,23 @@ const range = 2
55
export function generateCodeFrame (
66
source: string,
77
start: number = 0,
8-
end: number = source.length
8+
end?: number
99
): string {
10-
const lines = source.split(/\r?\n/)
10+
const lines = source.split(/\n/)
11+
.map(x => x[x.length - 1] === '\r' ? { line: x.substr(0, x.length - 1), increment: 2 } : { line: x, increment: 1})
12+
if (typeof end === 'undefined') {
13+
end = lines.reduce((s, x) => s + x.line.length + x.increment, 0)
14+
}
1115
let count = 0
1216
const res = []
1317
for (let i = 0; i < lines.length; i++) {
14-
count += lines[i].length + 1
18+
count += lines[i].line.length + 1
1519
if (count >= start) {
1620
for (let j = i - range; j <= i + range || end > count; j++) {
17-
if (j < 0 || j >= lines.length) continue
18-
res.push(`${j + 1}${repeat(` `, 3 - String(j + 1).length)}| ${lines[j]}`)
19-
const lineLength = lines[j].length
21+
if (j < 0) continue
22+
if (j >= lines.length) break
23+
res.push(`${j + 1}${repeat(` `, 3 - String(j + 1).length)}| ${lines[j].line}`)
24+
const lineLength = lines[j].line.length
2025
if (j === i) {
2126
// push underline
2227
const pad = start - (count - lineLength) + 1
@@ -27,7 +32,7 @@ export function generateCodeFrame (
2732
const length = Math.min(end - count, lineLength)
2833
res.push(` | ` + repeat(`^`, length))
2934
}
30-
count += lineLength + 1
35+
count += lineLength + lines[j].increment
3136
}
3237
}
3338
break

0 commit comments

Comments
 (0)