Last active
November 25, 2017 16:39
-
-
Save gzzhanghao/363fac4a201306f5fccf903e35bc652a to your computer and use it in GitHub Desktop.
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
// Output: | |
/* | |
`key` does not work on <slot> because slots are abstract outlets and can possibly expand into multiple elements. Use the key on a wrapping element instead. | |
1 | | |
2 | <div> | |
> 3 | <slot key="asdf" /> | |
| ^^^^^^^^^^ | |
4 | </div> | |
5 | | |
*/ | |
const compiler = require('../vue/packages/vue-template-compiler') | |
const { codeFrameColumns } = require('@babel/code-frame') | |
const LINE_REGEX = /\r\n|\n|\u2028|\u2029/g | |
const code = ` | |
<div> | |
<slot key="asdf" /> | |
</div> | |
` | |
compiler.compile(code, { outputSourceRange: true }).errors.forEach(({ msg, start, end }) => { | |
console.log(msg) | |
if (start == null) { | |
return | |
} | |
console.log(codeFrameColumns(code, { start: idx2loc(start), end: idx2loc(end) })) | |
console.log() | |
}) | |
function idx2loc(idx) { | |
if (idx != null) { | |
const lines = code.slice(0, idx).split(LINE_REGEX) | |
return { line: lines.length, column: lines[lines.length - 1].length + 1 } | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment