Skip to content
This repository was archived by the owner on Aug 7, 2023. It is now read-only.

Commit 290dd0e

Browse files
committed
fix: fix the null-ness, types, and bugs in worker
1 parent dd4d646 commit 290dd0e

File tree

1 file changed

+15
-12
lines changed

1 file changed

+15
-12
lines changed

lib/worker.ts

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ let fallbackLinter: typeof Tslint.Linter;
2525
let requireResolve: typeof import("resolve");
2626

2727

28-
function resolveAndCacheLinter(fileDir: string, moduleDir?: string): Promise<typeof Tslint.Linter> {
28+
function resolveAndCacheLinter(fileDir: string, moduleDir?: string): Promise<typeof Tslint.Linter | undefined> {
2929
const basedir = moduleDir || fileDir;
3030
return new Promise((resolve) => {
3131
if (!requireResolve) {
@@ -35,16 +35,16 @@ function resolveAndCacheLinter(fileDir: string, moduleDir?: string): Promise<typ
3535
tslintModuleName,
3636
{ basedir },
3737
(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)) {
4040
if (pkg.version.startsWith('3')) {
4141
// eslint-disable-next-line import/no-dynamic-require
4242
linter = shim(require('loophole').allowUnsafeNewFunction(() => require(linterPath) as typeof import("tslint")));
4343
} else {
4444
// eslint-disable-next-line import/no-dynamic-require
4545
linter = require('loophole').allowUnsafeNewFunction(() => (require(linterPath) as typeof import("tslint")).Linter);
4646
}
47-
tslintCache.set(fileDir, linter);
47+
tslintCache.set(fileDir, linter!);
4848
}
4949
resolve(linter);
5050
},
@@ -69,7 +69,7 @@ function getNodePrefixPath(): Promise<string> {
6969
});
7070
}
7171

72-
async function getLinter(filePath: string): Promise<typeof Tslint.Linter> {
72+
async function getLinter(filePath: string): Promise<typeof Tslint.Linter | undefined> {
7373
const basedir = path.dirname(filePath);
7474
if (tslintCache.has(basedir)) {
7575
return tslintCache.get(basedir);
@@ -96,7 +96,7 @@ async function getLinter(filePath: string): Promise<typeof Tslint.Linter> {
9696
}
9797
}
9898

99-
let prefix: string;
99+
let prefix: string | undefined = undefined;
100100
try {
101101
prefix = await getNodePrefixPath();
102102
} catch (err) {
@@ -121,8 +121,8 @@ async function getLinter(filePath: string): Promise<typeof Tslint.Linter> {
121121
return fallbackLinter;
122122
}
123123

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;
126126
const configurationDir = path.dirname(configurationPath);
127127
const tsconfigPath = path.resolve(configurationDir, 'tsconfig.json');
128128
try {
@@ -148,14 +148,17 @@ function getSeverity(failure: RuleFailure) {
148148
* @param options {Object} Linter options
149149
* @return Array of lint results
150150
*/
151-
async function lint(content: string, filePath: string, options: Tslint.ILinterOptions) {
151+
async function lint(content: string, filePath: string | undefined, options: Tslint.ILinterOptions) {
152152
if (filePath === null || filePath === undefined) {
153153
return null;
154154
}
155155

156156
let lintResult: Tslint.LintResult;
157157
try {
158158
const Linter = await getLinter(filePath);
159+
if (!Linter) {
160+
throw new Error(`tslint was not found for ${filePath}`)
161+
}
159162
const configurationPath = Linter.findConfigurationPath(null, filePath);
160163
const configuration = Linter.loadConfigurationFromPath(configurationPath);
161164

@@ -177,7 +180,7 @@ async function lint(content: string, filePath: string, options: Tslint.ILinterOp
177180
}
178181
}
179182

180-
let program: Ts.Program;
183+
let program: Ts.Program | undefined = undefined;
181184
if (config.enableSemanticRules && configurationPath) {
182185
program = await getProgram(Linter, configurationPath);
183186
}
@@ -197,11 +200,11 @@ async function lint(content: string, filePath: string, options: Tslint.ILinterOp
197200

198201
if (
199202
// tslint@<5
200-
!lintResult.failureCount
203+
!(lintResult as any).failureCount
201204
// tslint@>=5
202205
&& !lintResult.errorCount
203206
&& !lintResult.warningCount
204-
&& !lintResult.infoCount
207+
&& !(lintResult as any).infoCount // TODO is this still supported?
205208
) {
206209
return [];
207210
}

0 commit comments

Comments
 (0)