Skip to content

Commit

Permalink
fix: Correct auto-ignore of node_modules and non-ts files, and do sli…
Browse files Browse the repository at this point in the history
…ghtly better on avoiding race condition
  • Loading branch information
andyrooger committed May 1, 2023
1 parent 86b660e commit 3596941
Showing 1 changed file with 20 additions and 7 deletions.
27 changes: 20 additions & 7 deletions src/eslint-adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,13 +100,15 @@ export class ESLintAdapter {
private readonly configProvider: ConfigProvider;
private readonly getSourceFile: (fileName: string) => ts.SourceFile | undefined;
private readonly ignoredFilepathMap: Map<string, boolean>;
private readonly eslint: ESLint;

public constructor({ logger, configProvider, getSourceFile }: ESLintAdapterOptions) {
this.linter = new Linter();
this.logger = logger;
this.configProvider = configProvider;
this.getSourceFile = getSourceFile;
this.ignoredFilepathMap = new Map();
this.eslint = new ESLint();
}

private convertToESLintSourceCode(src: ts.SourceFile, filename: string, options?: ParserOptions | null) {
Expand All @@ -132,7 +134,7 @@ export class ESLintAdapter {
}

private getESLintResult(fileName: string, sourceFile: ts.SourceFile) {
if (this.ignoredFilepathMap.get(fileName.replace(/\\/g, '/')) === true) return [];
if (this.shouldIgnoreFile(fileName)) return [];
const configArray = this.configProvider.getConfigArrayForFile(fileName);
const configFileContent = configArray.extractConfig(fileName).toCompatibleObjectAsConfigFileContent();
if (!isParserModuleNameValid(configFileContent.parser, path.join("@typescript-eslint", "parser"))) {
Expand All @@ -145,13 +147,24 @@ export class ESLintAdapter {
return this.linter.verify(sourceCode, configArray as any, { filename: fileName });
}

private shouldIgnoreFile(fileName: string) {
const normalized = fileName.replace(/\\/g, "/");
if (/node_modules\//i.test(normalized) || !/\.tsx?$/i.test(normalized)) {
return true;
}
const cached = this.ignoredFilepathMap.get(normalized);
if (cached !== undefined) {
return cached;
}
// don't know but we will next time
Promise.resolve(this.eslint.isPathIgnored(normalized)).then(ignored =>
this.ignoredFilepathMap.set(normalized, ignored),
);
return undefined;
}

public checkFileToBeIgnored(fileName: string) {
if (/node_modules[\\/]/i.test(fileName) || !/\.tsx?$/i.test(fileName)) return;
const normalized = fileName.replace(/\\/g, '/');
Promise.resolve()
.then(() => new ESLint())
.then(eslint => eslint.isPathIgnored(normalized))
.then(result => this.ignoredFilepathMap.set(normalized, result));
this.shouldIgnoreFile(fileName);
}

public getSemanticDiagnostics(
Expand Down

0 comments on commit 3596941

Please sign in to comment.