fix(tslint): don't reread the input file on ts linting (close #2786) (#2787)

This commit is contained in:
Matthias Bartelmeß
2019-02-18 17:40:35 +01:00
committed by Haoqun Jiang
parent fccb1143e5
commit 364f28f6d7
2 changed files with 34 additions and 1 deletions

View File

@@ -88,3 +88,28 @@ test('should ignore issues in node_modules', async () => {
await run('vue-cli-service lint')
expect(await read('node_modules/bad.ts')).toMatch(updatedMain)
})
test('should be able to fix mixed line endings', async () => {
const project = await create('ts-lint-mixed-line-endings', {
plugins: {
'@vue/cli-plugin-typescript': {
tsLint: true
}
}
})
const { write, run } = project
const b64 = 'PHRlbXBsYXRlPjwvdGVtcGxhdGU+DQoNCjxzY3JpcHQgbGFuZz0idHMiPg0KZXhwb3J0IGRlZmF1bHQgY2xhc3MgVGVzdCAgew0KICBnZXQgYXNzaWduZWUoKSB7DQogICAgdmFyIGl0ZW1zOnt0ZXh0OnN0cmluZzsgdmFsdWU6c3RyaW5nIHwgbnVtYmVyIHwgbnVsbH1bXSA9IFtdOw0KICAgIHJldHVybiBpdGVtczsNCiAgfQ0KDQp9DQo8L3NjcmlwdD4NCg0K'
const buf = Buffer.from(b64, 'base64')
await write('src/bad.vue', buf)
// Try twice to fix the file.
// For now, it will fail the first time, which corresponds to the behaviour of tslint.
try {
await run('vue-cli-service lint -- src/bad.vue')
} catch (e) { }
await run('vue-cli-service lint -- src/bad.vue')
})

View File

@@ -29,6 +29,7 @@ module.exports = function lint (args = {}, api, silent) {
if (isVueFile(file)) {
const parts = vueFileCache.get(path.normalize(file))
if (parts) {
parts.content = content;
const { before, after } = parts
content = `${before}\n${content.trim()}\n${after}`
}
@@ -42,12 +43,19 @@ module.exports = function lint (args = {}, api, silent) {
}
const parseTSFromVueFile = file => {
// If the file has already been cached, don't read the file again. Use the cache instead.
if (vueFileCache.has(file)) {
return vueFileCache.get(file).content;
}
const content = fs.readFileSync(file, 'utf-8')
const { script } = vueCompiler.parseComponent(content, { pad: 'line' })
if (script && /^tsx?$/.test(script.lang)) {
vueFileCache.set(file, {
before: content.slice(0, script.start),
after: content.slice(script.end)
after: content.slice(script.end),
content: script.content,
})
return script
}