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

Commit f20ac36

Browse files
committed
fix: add extends manually if Tslint fails to do so
1 parent 8a9404b commit f20ac36

File tree

1 file changed

+38
-4
lines changed

1 file changed

+38
-4
lines changed

lib/worker.ts

Lines changed: 38 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import { shim } from "./compat-shim";
1010
import { defaultConfig } from "./config"
1111
import type { ConfigSchema } from "./config"
1212
import type { emit } from 'node:cluster';
13-
import type * as Tslint from "tslint";
13+
import * as Tslint from "tslint";
1414
import type * as Ts from "typescript";
1515
import type { JobMessage, ConfigMessage } from "./workerHelper"
1616
import { RuleFailure } from 'tslint';
@@ -141,6 +141,41 @@ function getSeverity(failure: RuleFailure) {
141141
return ['info', 'warning', 'error'].includes(severity) ? severity : 'warning';
142142
}
143143

144+
function loadTslintConfig(Linter: typeof Tslint.Linter, filePath: string) {
145+
let configurationPath: string | undefined
146+
let configuration: Tslint.Configuration.IConfigurationFile | undefined
147+
if (typeof Linter.findConfiguration === "function") {
148+
const { path, results } = Linter.findConfiguration(null, filePath)
149+
configurationPath = path
150+
configuration = results
151+
} else {
152+
configurationPath = Linter.findConfigurationPath(null, filePath);
153+
configuration = Linter.loadConfigurationFromPath(configurationPath);
154+
}
155+
// Load extends using `require` - this is a bug in Tslint
156+
let configExtends = configuration?.extends ?? []
157+
if (configurationPath !== undefined && configExtends !== undefined && configExtends.length === 0) {
158+
try {
159+
const configurationJson = Tslint.Configuration.readConfigurationFile?.(configurationPath)
160+
const extendsJson = configurationJson.extends
161+
if (typeof extendsJson === "string") {
162+
configExtends = [...configExtends, extendsJson]
163+
} else if (extendsJson !== undefined) {
164+
configExtends = [...configExtends, ...extendsJson]
165+
}
166+
} catch { /* ignore error */ }
167+
}
168+
return {
169+
configurationPath,
170+
configuration: configuration !== undefined
171+
? {
172+
...configuration,
173+
extends: configExtends
174+
}
175+
: configuration
176+
}
177+
}
178+
144179
/**
145180
* Lint the provided TypeScript content
146181
* @param content {string} The content of the TypeScript file
@@ -159,10 +194,9 @@ async function lint(content: string, filePath: string | undefined, options: Tsli
159194
if (!Linter) {
160195
throw new Error(`tslint was not found for ${filePath}`)
161196
}
162-
const configurationPath = Linter.findConfigurationPath(null, filePath);
163-
const configuration = Linter.loadConfigurationFromPath(configurationPath);
197+
const { configurationPath, configuration } = loadTslintConfig(Linter, filePath)
164198

165-
let { rulesDirectory } = configuration;
199+
let rulesDirectory = configuration?.rulesDirectory;
166200
if (rulesDirectory && configurationPath) {
167201
const configurationDir = path.dirname(configurationPath);
168202
if (!Array.isArray(rulesDirectory)) {

0 commit comments

Comments
 (0)