Skip to content

Commit 364f28f

Browse files
fourplusonehaoqunjiang
authored andcommittedFeb 18, 2019
fix(tslint): don't reread the input file on ts linting (close #2786) (#2787)
1 parent fccb114 commit 364f28f

File tree

2 files changed

+34
-1
lines changed

2 files changed

+34
-1
lines changed
 

‎packages/@vue/cli-plugin-typescript/__tests__/tsPluginTSLint.spec.js

+25
Original file line numberDiff line numberDiff line change
@@ -88,3 +88,28 @@ test('should ignore issues in node_modules', async () => {
8888
await run('vue-cli-service lint')
8989
expect(await read('node_modules/bad.ts')).toMatch(updatedMain)
9090
})
91+
92+
test('should be able to fix mixed line endings', async () => {
93+
const project = await create('ts-lint-mixed-line-endings', {
94+
plugins: {
95+
'@vue/cli-plugin-typescript': {
96+
tsLint: true
97+
}
98+
}
99+
})
100+
101+
const { write, run } = project
102+
103+
const b64 = 'PHRlbXBsYXRlPjwvdGVtcGxhdGU+DQoNCjxzY3JpcHQgbGFuZz0idHMiPg0KZXhwb3J0IGRlZmF1bHQgY2xhc3MgVGVzdCAgew0KICBnZXQgYXNzaWduZWUoKSB7DQogICAgdmFyIGl0ZW1zOnt0ZXh0OnN0cmluZzsgdmFsdWU6c3RyaW5nIHwgbnVtYmVyIHwgbnVsbH1bXSA9IFtdOw0KICAgIHJldHVybiBpdGVtczsNCiAgfQ0KDQp9DQo8L3NjcmlwdD4NCg0K'
104+
const buf = Buffer.from(b64, 'base64')
105+
106+
await write('src/bad.vue', buf)
107+
108+
// Try twice to fix the file.
109+
// For now, it will fail the first time, which corresponds to the behaviour of tslint.
110+
try {
111+
await run('vue-cli-service lint -- src/bad.vue')
112+
} catch (e) { }
113+
114+
await run('vue-cli-service lint -- src/bad.vue')
115+
})

‎packages/@vue/cli-plugin-typescript/lib/tslint.js

+9-1
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ module.exports = function lint (args = {}, api, silent) {
2929
if (isVueFile(file)) {
3030
const parts = vueFileCache.get(path.normalize(file))
3131
if (parts) {
32+
parts.content = content;
3233
const { before, after } = parts
3334
content = `${before}\n${content.trim()}\n${after}`
3435
}
@@ -42,12 +43,19 @@ module.exports = function lint (args = {}, api, silent) {
4243
}
4344

4445
const parseTSFromVueFile = file => {
46+
47+
// If the file has already been cached, don't read the file again. Use the cache instead.
48+
if (vueFileCache.has(file)) {
49+
return vueFileCache.get(file).content;
50+
}
51+
4552
const content = fs.readFileSync(file, 'utf-8')
4653
const { script } = vueCompiler.parseComponent(content, { pad: 'line' })
4754
if (script && /^tsx?$/.test(script.lang)) {
4855
vueFileCache.set(file, {
4956
before: content.slice(0, script.start),
50-
after: content.slice(script.end)
57+
after: content.slice(script.end),
58+
content: script.content,
5159
})
5260
return script
5361
}

0 commit comments

Comments
 (0)
Please sign in to comment.