Skip to content

Commit

Permalink
fix(compiler): infinite loop in generateCodeFrame (vuejs#10547)
Browse files Browse the repository at this point in the history
  • Loading branch information
oguimbal committed Sep 24, 2019
1 parent d2db6af commit 954643a
Showing 1 changed file with 12 additions and 7 deletions.
19 changes: 12 additions & 7 deletions src/compiler/codeframe.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,23 @@ const range = 2
export function generateCodeFrame (
source: string,
start: number = 0,
end: number = source.length
end?: number
): string {
const lines = source.split(/\r?\n/)
const lines = source.split(/\n/)
.map(x => x[x.length - 1] === '\r' ? { line: x.substr(0, x.length - 1), increment: 2 } : { line: x, increment: 1})
if (typeof end === 'undefined') {
end = lines.reduce((s, x) => s + x.line.length + x.increment, 0)
}
let count = 0
const res = []
for (let i = 0; i < lines.length; i++) {
count += lines[i].length + 1
count += lines[i].line.length + 1
if (count >= start) {
for (let j = i - range; j <= i + range || end > count; j++) {
if (j < 0 || j >= lines.length) continue
res.push(`${j + 1}${repeat(` `, 3 - String(j + 1).length)}| ${lines[j]}`)
const lineLength = lines[j].length
if (j < 0) continue
if (j >= lines.length) break
res.push(`${j + 1}${repeat(` `, 3 - String(j + 1).length)}| ${lines[j].line}`)
const lineLength = lines[j].line.length
if (j === i) {
// push underline
const pad = start - (count - lineLength) + 1
Expand All @@ -27,7 +32,7 @@ export function generateCodeFrame (
const length = Math.min(end - count, lineLength)
res.push(` | ` + repeat(`^`, length))
}
count += lineLength + 1
count += lineLength + lines[j].increment
}
}
break
Expand Down

0 comments on commit 954643a

Please sign in to comment.