diff --git a/src/compiler/codeframe.js b/src/compiler/codeframe.js index 513d318fcc1..8246abed36e 100644 --- a/src/compiler/codeframe.js +++ b/src/compiler/codeframe.js @@ -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 @@ -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