Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Improve file ignoring #543

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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) === 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 (fileName.indexOf("node_modules" + path.sep) !== -1) return;
if (!fileName.endsWith(".ts") && !fileName.endsWith(".tsx")) return;
Promise.resolve()
.then(() => new ESLint())
.then(eslint => eslint.isPathIgnored(fileName))
.then(result => this.ignoredFilepathMap.set(fileName, result));
this.shouldIgnoreFile(fileName);
}

public getSemanticDiagnostics(
Expand Down