Skip to content

Don't reread the input file on ts linting #2787

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 18, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.content
}
Expand Down