Description
When an external tool is using vue-template-compiler to grab the script block, { pad: true }
fills in preceding non-script lines with //
so that line+column-oriented tools can get the line numbers right.
However, this doesn't work as well for character-oriented tools like the Typescript language service, which treat positions as a single index into a single string. It would be better if { pad: true }
replaced the contents of all non-script blocks with spaces.
For example:
import { parseComponent } from 'vue-template-compiler'
const desc = parseComponent(`<template>
blah
</template>
<script>
export default {
data: { example: 12 }
// etc etc
}
</script>`, { pad: true })
console.log(desc.script.content)
Current behaviour prints:
//
//
//
export default {
data: ...
Desired behaviour prints:
......................................................
.......................................
....
...........
........
export default {
data: ...
except instead of .
, it should print
.
Motivation
For background, I'm working on a Vue plugin for the Typescript language service so that editors like VS Code, Visual Studio, Sublime, emacs, etc, can get Javascript and Typescript completions inside .vue files. The next version of Typescript will support full completions on Vue and Vue options objects with no type annotations, and the plugin will make that work inside .vue
files as well as .js
and .ts
files.
I tried using the start/end
offsets returned from parseComponent
, which kind of works, but Typescript has previously only used offsets for non-critical parsing like JSDoc. I'd like to use the space-replacing technique since I know it has worked in the past, for example in Visual Studio's handling of Javascript embedded in HTML.
Implementation
Replace the body of padContent
in src/sfc/parser.js
with return content.slice(0, block.start).replace(/./g, ' ')
. This correctly avoids replacing both \n
and \r\n
, at least on node 7.4 on Linux.
I'll send a PR as soon as I get Vue building and have added a test.