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

Commit

Permalink
fix: fix the null-ness, types, and bugs in worker
Browse files Browse the repository at this point in the history
  • Loading branch information
aminya committed Apr 5, 2021
1 parent dd4d646 commit 290dd0e
Showing 1 changed file with 15 additions and 12 deletions.
27 changes: 15 additions & 12 deletions lib/worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ let fallbackLinter: typeof Tslint.Linter;
let requireResolve: typeof import("resolve");


function resolveAndCacheLinter(fileDir: string, moduleDir?: string): Promise<typeof Tslint.Linter> {
function resolveAndCacheLinter(fileDir: string, moduleDir?: string): Promise<typeof Tslint.Linter | undefined> {
const basedir = moduleDir || fileDir;
return new Promise((resolve) => {
if (!requireResolve) {
Expand All @@ -35,16 +35,16 @@ function resolveAndCacheLinter(fileDir: string, moduleDir?: string): Promise<typ
tslintModuleName,
{ basedir },
(err, linterPath, pkg) => {
let linter: typeof Tslint.Linter;
if (!err && pkg && /^3|4|5|6\./.test(pkg.version)) {
let linter: typeof Tslint.Linter | undefined = undefined;
if (!err && linterPath !== undefined && pkg && /^3|4|5|6\./.test(pkg.version)) {
if (pkg.version.startsWith('3')) {
// eslint-disable-next-line import/no-dynamic-require
linter = shim(require('loophole').allowUnsafeNewFunction(() => require(linterPath) as typeof import("tslint")));
} else {
// eslint-disable-next-line import/no-dynamic-require
linter = require('loophole').allowUnsafeNewFunction(() => (require(linterPath) as typeof import("tslint")).Linter);
}
tslintCache.set(fileDir, linter);
tslintCache.set(fileDir, linter!);
}
resolve(linter);
},
Expand All @@ -69,7 +69,7 @@ function getNodePrefixPath(): Promise<string> {
});
}

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

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

async function getProgram(Linter: typeof Tslint.Linter, configurationPath: string): Promise<Ts.Program> {
let program: Ts.Program;
async function getProgram(Linter: typeof Tslint.Linter, configurationPath: string): Promise<Ts.Program | undefined> {
let program: Ts.Program | undefined = undefined;
const configurationDir = path.dirname(configurationPath);
const tsconfigPath = path.resolve(configurationDir, 'tsconfig.json');
try {
Expand All @@ -148,14 +148,17 @@ function getSeverity(failure: RuleFailure) {
* @param options {Object} Linter options
* @return Array of lint results
*/
async function lint(content: string, filePath: string, options: Tslint.ILinterOptions) {
async function lint(content: string, filePath: string | undefined, options: Tslint.ILinterOptions) {
if (filePath === null || filePath === undefined) {
return null;
}

let lintResult: Tslint.LintResult;
try {
const Linter = await getLinter(filePath);
if (!Linter) {
throw new Error(`tslint was not found for ${filePath}`)
}
const configurationPath = Linter.findConfigurationPath(null, filePath);
const configuration = Linter.loadConfigurationFromPath(configurationPath);

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

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

if (
// tslint@<5
!lintResult.failureCount
!(lintResult as any).failureCount
// tslint@>=5
&& !lintResult.errorCount
&& !lintResult.warningCount
&& !lintResult.infoCount
&& !(lintResult as any).infoCount // TODO is this still supported?
) {
return [];
}
Expand Down

0 comments on commit 290dd0e

Please sign in to comment.