@@ -25,7 +25,7 @@ let fallbackLinter: typeof Tslint.Linter;
25
25
let requireResolve : typeof import ( "resolve" ) ;
26
26
27
27
28
- function resolveAndCacheLinter ( fileDir : string , moduleDir ?: string ) : Promise < typeof Tslint . Linter > {
28
+ function resolveAndCacheLinter ( fileDir : string , moduleDir ?: string ) : Promise < typeof Tslint . Linter | undefined > {
29
29
const basedir = moduleDir || fileDir ;
30
30
return new Promise ( ( resolve ) => {
31
31
if ( ! requireResolve ) {
@@ -35,16 +35,16 @@ function resolveAndCacheLinter(fileDir: string, moduleDir?: string): Promise<typ
35
35
tslintModuleName ,
36
36
{ basedir } ,
37
37
( err , linterPath , pkg ) => {
38
- let linter : typeof Tslint . Linter ;
39
- if ( ! err && pkg && / ^ 3 | 4 | 5 | 6 \. / . test ( pkg . version ) ) {
38
+ let linter : typeof Tslint . Linter | undefined = undefined ;
39
+ if ( ! err && linterPath !== undefined && pkg && / ^ 3 | 4 | 5 | 6 \. / . test ( pkg . version ) ) {
40
40
if ( pkg . version . startsWith ( '3' ) ) {
41
41
// eslint-disable-next-line import/no-dynamic-require
42
42
linter = shim ( require ( 'loophole' ) . allowUnsafeNewFunction ( ( ) => require ( linterPath ) as typeof import ( "tslint" ) ) ) ;
43
43
} else {
44
44
// eslint-disable-next-line import/no-dynamic-require
45
45
linter = require ( 'loophole' ) . allowUnsafeNewFunction ( ( ) => ( require ( linterPath ) as typeof import ( "tslint" ) ) . Linter ) ;
46
46
}
47
- tslintCache . set ( fileDir , linter ) ;
47
+ tslintCache . set ( fileDir , linter ! ) ;
48
48
}
49
49
resolve ( linter ) ;
50
50
} ,
@@ -69,7 +69,7 @@ function getNodePrefixPath(): Promise<string> {
69
69
} ) ;
70
70
}
71
71
72
- async function getLinter ( filePath : string ) : Promise < typeof Tslint . Linter > {
72
+ async function getLinter ( filePath : string ) : Promise < typeof Tslint . Linter | undefined > {
73
73
const basedir = path . dirname ( filePath ) ;
74
74
if ( tslintCache . has ( basedir ) ) {
75
75
return tslintCache . get ( basedir ) ;
@@ -96,7 +96,7 @@ async function getLinter(filePath: string): Promise<typeof Tslint.Linter> {
96
96
}
97
97
}
98
98
99
- let prefix : string ;
99
+ let prefix : string | undefined = undefined ;
100
100
try {
101
101
prefix = await getNodePrefixPath ( ) ;
102
102
} catch ( err ) {
@@ -121,8 +121,8 @@ async function getLinter(filePath: string): Promise<typeof Tslint.Linter> {
121
121
return fallbackLinter ;
122
122
}
123
123
124
- async function getProgram ( Linter : typeof Tslint . Linter , configurationPath : string ) : Promise < Ts . Program > {
125
- let program : Ts . Program ;
124
+ async function getProgram ( Linter : typeof Tslint . Linter , configurationPath : string ) : Promise < Ts . Program | undefined > {
125
+ let program : Ts . Program | undefined = undefined ;
126
126
const configurationDir = path . dirname ( configurationPath ) ;
127
127
const tsconfigPath = path . resolve ( configurationDir , 'tsconfig.json' ) ;
128
128
try {
@@ -148,14 +148,17 @@ function getSeverity(failure: RuleFailure) {
148
148
* @param options {Object} Linter options
149
149
* @return Array of lint results
150
150
*/
151
- async function lint ( content : string , filePath : string , options : Tslint . ILinterOptions ) {
151
+ async function lint ( content : string , filePath : string | undefined , options : Tslint . ILinterOptions ) {
152
152
if ( filePath === null || filePath === undefined ) {
153
153
return null ;
154
154
}
155
155
156
156
let lintResult : Tslint . LintResult ;
157
157
try {
158
158
const Linter = await getLinter ( filePath ) ;
159
+ if ( ! Linter ) {
160
+ throw new Error ( `tslint was not found for ${ filePath } ` )
161
+ }
159
162
const configurationPath = Linter . findConfigurationPath ( null , filePath ) ;
160
163
const configuration = Linter . loadConfigurationFromPath ( configurationPath ) ;
161
164
@@ -177,7 +180,7 @@ async function lint(content: string, filePath: string, options: Tslint.ILinterOp
177
180
}
178
181
}
179
182
180
- let program : Ts . Program ;
183
+ let program : Ts . Program | undefined = undefined ;
181
184
if ( config . enableSemanticRules && configurationPath ) {
182
185
program = await getProgram ( Linter , configurationPath ) ;
183
186
}
@@ -197,11 +200,11 @@ async function lint(content: string, filePath: string, options: Tslint.ILinterOp
197
200
198
201
if (
199
202
// tslint@<5
200
- ! lintResult . failureCount
203
+ ! ( lintResult as any ) . failureCount
201
204
// tslint@>=5
202
205
&& ! lintResult . errorCount
203
206
&& ! lintResult . warningCount
204
- && ! lintResult . infoCount
207
+ && ! ( lintResult as any ) . infoCount // TODO is this still supported?
205
208
) {
206
209
return [ ] ;
207
210
}
0 commit comments