From 0576e5fea617823757376b8dd6c37cd3f49f42ee Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matthias=20Bartelme=C3=9F?= Date: Mon, 22 Oct 2018 23:21:40 +0200 Subject: [PATCH] fix(tslint): Don't reread the input file on ts linting (close #2786) --- .../__tests__/tsPluginTSLint.spec.js | 25 +++++++++++++++++++ .../@vue/cli-plugin-typescript/lib/tslint.js | 10 +++++++- 2 files changed, 34 insertions(+), 1 deletion(-) diff --git a/packages/@vue/cli-plugin-typescript/__tests__/tsPluginTSLint.spec.js b/packages/@vue/cli-plugin-typescript/__tests__/tsPluginTSLint.spec.js index 013aef4756..a487e70241 100644 --- a/packages/@vue/cli-plugin-typescript/__tests__/tsPluginTSLint.spec.js +++ b/packages/@vue/cli-plugin-typescript/__tests__/tsPluginTSLint.spec.js @@ -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') +}) diff --git a/packages/@vue/cli-plugin-typescript/lib/tslint.js b/packages/@vue/cli-plugin-typescript/lib/tslint.js index d14d0d8d9b..fb5af94658 100644 --- a/packages/@vue/cli-plugin-typescript/lib/tslint.js +++ b/packages/@vue/cli-plugin-typescript/lib/tslint.js @@ -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.content }