@@ -16,8 +16,11 @@ module.exports = function lint (args = {}, api, silent) {
16
16
rulesDirectory : args [ 'rules-dir' ]
17
17
}
18
18
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.
21
24
const vueFileCache = new Map ( )
22
25
const writeFileSync = fs . writeFileSync
23
26
@@ -50,18 +53,30 @@ module.exports = function lint (args = {}, api, silent) {
50
53
const program = tslint . Linter . createProgram ( api . resolve ( 'tsconfig.json' ) )
51
54
52
55
// 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
+ }
60
66
}
61
67
}
62
68
69
+ patchProgram ( program )
70
+
63
71
const linter = new tslint . Linter ( options , program )
64
72
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
+
65
80
const config = tslint . Configuration . findConfiguration ( api . resolve ( 'tslint.json' ) ) . results
66
81
// create a patched config that disables the blank lines rule,
67
82
// so that we get correct line numbers in error reports for *.vue files.
0 commit comments