Skip to content

Commit

Permalink
fix(tslint): don't reread the input file on ts linting (close #2786) (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
fourplusone authored and sodatea committed Feb 18, 2019
1 parent fccb114 commit 364f28f
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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')
})
10 changes: 9 additions & 1 deletion packages/@vue/cli-plugin-typescript/lib/tslint.js
Original file line number Diff line number Diff line change
Expand Up @@ -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}`
}
Expand All @@ -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
}
Expand Down

0 comments on commit 364f28f

Please sign in to comment.