-
-
Notifications
You must be signed in to change notification settings - Fork 33.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(compiler): output codeframe in browser compiler
- Loading branch information
Showing
4 changed files
with
144 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
/* @flow */ | ||
|
||
const range = 2 | ||
|
||
export function generateCodeFrame ( | ||
source: string, | ||
start: number = 0, | ||
end: number = source.length | ||
): string { | ||
const lines = source.split(/\r?\n/) | ||
let count = 0 | ||
const res = [] | ||
for (let i = 0; i < lines.length; i++) { | ||
count += lines[i].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 === i) { | ||
// push underline | ||
const pad = start - (count - lineLength) + 1 | ||
const length = end > count ? lineLength - pad : end - start | ||
res.push(` | ` + repeat(` `, pad) + repeat(`^`, length)) | ||
} else if (j > i) { | ||
if (end > count) { | ||
const length = Math.min(end - count, lineLength) | ||
res.push(` | ` + repeat(`^`, length)) | ||
} | ||
count += lineLength + 1 | ||
} | ||
} | ||
break | ||
} | ||
} | ||
return res.join('\n') | ||
} | ||
|
||
function repeat (str, n) { | ||
let result = '' | ||
while (true) { // eslint-disable-line | ||
if (n & 1) result += str | ||
n >>>= 1 | ||
if (n <= 0) break | ||
str += str | ||
} | ||
return result | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
import { generateCodeFrame } from 'compiler/codeframe' | ||
|
||
describe('codeframe', () => { | ||
const source = ` | ||
<div> | ||
<template key="one"></template> | ||
<ul> | ||
<li v-for="foobar">hi</li> | ||
</ul> | ||
<template key="two"></template> | ||
</div> | ||
`.trim() | ||
|
||
it('line near top', () => { | ||
const keyStart = source.indexOf(`key="one"`) | ||
const keyEnd = keyStart + `key="one"`.length | ||
expect(generateCodeFrame(source, keyStart, keyEnd)).toBe(` | ||
1 | <div> | ||
2 | <template key="one"></template> | ||
| ^^^^^^^^^ | ||
3 | <ul> | ||
4 | <li v-for="foobar">hi</li> | ||
`.trim()) | ||
}) | ||
|
||
it('line in middle', () => { | ||
// should cover 5 lines | ||
const forStart = source.indexOf(`v-for=`) | ||
const forEnd = forStart + `v-for="foobar"`.length | ||
expect(generateCodeFrame(source, forStart, forEnd)).toBe(` | ||
2 | <template key="one"></template> | ||
3 | <ul> | ||
4 | <li v-for="foobar">hi</li> | ||
| ^^^^^^^^^^^^^^ | ||
5 | </ul> | ||
6 | <template key="two"></template> | ||
`.trim()) | ||
}) | ||
|
||
it('line near bottom', () => { | ||
const keyStart = source.indexOf(`key="two"`) | ||
const keyEnd = keyStart + `key="two"`.length | ||
expect(generateCodeFrame(source, keyStart, keyEnd)).toBe(` | ||
4 | <li v-for="foobar">hi</li> | ||
5 | </ul> | ||
6 | <template key="two"></template> | ||
| ^^^^^^^^^ | ||
7 | </div> | ||
`.trim()) | ||
}) | ||
|
||
it('multi-line highlights', () => { | ||
const source = ` | ||
<div attr="some | ||
multiline | ||
attr | ||
"> | ||
</div> | ||
`.trim() | ||
|
||
const attrStart = source.indexOf(`attr=`) | ||
const attrEnd = source.indexOf(`">`) + 1 | ||
expect(generateCodeFrame(source, attrStart, attrEnd)).toBe(` | ||
1 | <div attr="some | ||
| ^^^^^^^^^^ | ||
2 | multiline | ||
| ^^^^^^^^^^^ | ||
3 | attr | ||
| ^^^^ | ||
4 | "> | ||
| ^ | ||
`.trim()) | ||
}) | ||
}) |