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

Commit

Permalink
fix: import types from tslint and ts + access private member
Browse files Browse the repository at this point in the history
  • Loading branch information
aminya committed Apr 5, 2021
1 parent 663cadd commit f3541b4
Showing 1 changed file with 15 additions and 16 deletions.
31 changes: 15 additions & 16 deletions lib/worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import ChildProcess from 'child_process';
import getPath from 'consistent-path';
import type { ConfigSchema } from "./config"
import type { emit } from 'node:cluster';
import type * as Tslint from "tslint";
import type * as Ts from "typescript";

process.title = 'linter-tslint worker';

Expand All @@ -17,16 +19,15 @@ const config: ConfigSchema = {
useLocalTslint: false,
};

type TsLintLinter = typeof import("tslint").Linter;
let fallbackLinter: TsLintLinter;
let fallbackLinter: Tslint.Linter;
let requireResolve: typeof import("resolve");

/**
* Shim for TSLint v3 interoperability
* @param {Function} Linter TSLint v3 linter
* @return {Function} TSLint v4-compatible linter
*/
function shim(Linter: Function): TsLintLinter {
function shim(Linter: Function): Tslint.Linter {
function LinterShim(options) {
this.options = options;
this.results = {};
Expand All @@ -51,7 +52,7 @@ function shim(Linter: Function): TsLintLinter {
return LinterShim;
}

function resolveAndCacheLinter(fileDir: string, moduleDir?: string): Promise<TsLintLinter> {
function resolveAndCacheLinter(fileDir: string, moduleDir?: string): Promise<Tslint.Linter> {
const basedir = moduleDir || fileDir;
return new Promise((resolve) => {
if (!requireResolve) {
Expand All @@ -61,7 +62,7 @@ function resolveAndCacheLinter(fileDir: string, moduleDir?: string): Promise<TsL
tslintModuleName,
{ basedir },
(err, linterPath, pkg) => {
let linter: TsLintLinter;
let linter: Tslint.Linter;
if (!err && pkg && /^3|4|5\./.test(pkg.version)) {
if (pkg.version.startsWith('3')) {
// eslint-disable-next-line import/no-dynamic-require
Expand Down Expand Up @@ -95,7 +96,7 @@ function getNodePrefixPath(): Promise<string> {
});
}

async function getLinter(filePath: string): Promise<TsLintLinter> {
async function getLinter(filePath: string): Promise<Tslint.Linter> {
const basedir = path.dirname(filePath);
if (tslintCache.has(basedir)) {
return tslintCache.get(basedir);
Expand Down Expand Up @@ -147,10 +148,8 @@ async function getLinter(filePath: string): Promise<TsLintLinter> {
return fallbackLinter;
}

type TsProgram = ReturnType<TsLintLinter["createProgram"]>

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

let lintResult: TsLintLinter;
let lintResult: Tslint.Linter;
try {
const Linter = await getLinter(filePath);
const configurationPath = Linter.findConfigurationPath(null, filePath);
Expand All @@ -205,16 +204,16 @@ async function lint(content: string, filePath: string, options: object) {
}
}

let program: TsProgram;
let program: Ts.Program;
if (config.enableSemanticRules && configurationPath) {
program = await getProgram(Linter, configurationPath);
}

const linter = new Linter(({
const linter = new Linter({
formatter: 'json',
rulesDirectory,
...options,
}), program);
}, program);

linter.lint(filePath, content, configuration);
lintResult = linter.getResult();
Expand All @@ -234,7 +233,7 @@ async function lint(content: string, filePath: string, options: object) {
return [];
}

return lintResult.failures.map((failure) => {
return lintResult["failures"].map((failure: Tslint.RuleFailure) => {
const ruleUri = getRuleUri(failure.getRuleName());
const startPosition = failure.getStartPosition().getLineAndCharacter();
const endPosition = failure.getEndPosition().getLineAndCharacter();
Expand Down

0 comments on commit f3541b4

Please sign in to comment.