Skip to content

Commit 8e63e82

Browse files
committed
ci: fix tslint refactor
1 parent 6c966f4 commit 8e63e82

File tree

1 file changed

+24
-9
lines changed
  • packages/@vue/cli-plugin-typescript/lib

1 file changed

+24
-9
lines changed

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

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,11 @@ module.exports = function lint (args = {}, api, silent) {
1616
rulesDirectory: args['rules-dir']
1717
}
1818

19-
// hack to make tslint --fix work for *.vue files
20-
// this works because (luckily) tslint lints synchronously
19+
// hack to make tslint --fix work for *.vue files:
20+
// we save the non-script parts to a cache right before
21+
// linting the file, and patch fs.writeFileSync to combine the fixed script
22+
// back with the non-script parts.
23+
// this works because (luckily) tslint lints synchronously.
2124
const vueFileCache = new Map()
2225
const writeFileSync = fs.writeFileSync
2326

@@ -50,18 +53,30 @@ module.exports = function lint (args = {}, api, silent) {
5053
const program = tslint.Linter.createProgram(api.resolve('tsconfig.json'))
5154

5255
// patch getSourceFile for *.vue files
53-
const getSourceFile = program.getSourceFile
54-
program.getSourceFile = function (file, languageVersion, onError) {
55-
if (isVueFile(file)) {
56-
const script = parseTSFromVueFile(file)
57-
return ts.createSourceFile(file, script, languageVersion, true)
58-
} else {
59-
return getSourceFile.call(this, file, languageVersion, onError)
56+
// so that it returns the <script> block only
57+
const patchProgram = program => {
58+
const getSourceFile = program.getSourceFile
59+
program.getSourceFile = function (file, languageVersion, onError) {
60+
if (isVueFile(file)) {
61+
const script = parseTSFromVueFile(file)
62+
return ts.createSourceFile(file, script, languageVersion, true)
63+
} else {
64+
return getSourceFile.call(this, file, languageVersion, onError)
65+
}
6066
}
6167
}
6268

69+
patchProgram(program)
70+
6371
const linter = new tslint.Linter(options, program)
6472

73+
// patch linter.updateProgram to ensure every program has correct getSourceFile
74+
const updateProgram = linter.updateProgram
75+
linter.updateProgram = function (...args) {
76+
updateProgram.call(this, ...args)
77+
patchProgram(this.program)
78+
}
79+
6580
const config = tslint.Configuration.findConfiguration(api.resolve('tslint.json')).results
6681
// create a patched config that disables the blank lines rule,
6782
// so that we get correct line numbers in error reports for *.vue files.

0 commit comments

Comments
 (0)