From 86b660ec8ccab7b94a8efb3ef9ca79b9c12eae76 Mon Sep 17 00:00:00 2001 From: andyrooger <420834+andyrooger@users.noreply.github.com> Date: Mon, 1 May 2023 23:31:23 +0100 Subject: [PATCH 1/2] fix: Correct file ignore check for node_modules on Windows --- src/eslint-adapter.ts | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/eslint-adapter.ts b/src/eslint-adapter.ts index d29095af..5e71188b 100644 --- a/src/eslint-adapter.ts +++ b/src/eslint-adapter.ts @@ -132,7 +132,7 @@ export class ESLintAdapter { } private getESLintResult(fileName: string, sourceFile: ts.SourceFile) { - if (this.ignoredFilepathMap.get(fileName) === true) return []; + if (this.ignoredFilepathMap.get(fileName.replace(/\\/g, '/')) === true) return []; const configArray = this.configProvider.getConfigArrayForFile(fileName); const configFileContent = configArray.extractConfig(fileName).toCompatibleObjectAsConfigFileContent(); if (!isParserModuleNameValid(configFileContent.parser, path.join("@typescript-eslint", "parser"))) { @@ -146,12 +146,12 @@ export class ESLintAdapter { } public checkFileToBeIgnored(fileName: string) { - if (fileName.indexOf("node_modules" + path.sep) !== -1) return; - if (!fileName.endsWith(".ts") && !fileName.endsWith(".tsx")) return; + 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(fileName)) - .then(result => this.ignoredFilepathMap.set(fileName, result)); + .then(eslint => eslint.isPathIgnored(normalized)) + .then(result => this.ignoredFilepathMap.set(normalized, result)); } public getSemanticDiagnostics( From 3596941c15f4bce00b97b331b92d3637a8791d93 Mon Sep 17 00:00:00 2001 From: andyrooger <420834+andyrooger@users.noreply.github.com> Date: Mon, 1 May 2023 23:32:12 +0100 Subject: [PATCH 2/2] fix: Correct auto-ignore of node_modules and non-ts files, and do slightly better on avoiding race condition --- src/eslint-adapter.ts | 27 ++++++++++++++++++++------- 1 file changed, 20 insertions(+), 7 deletions(-) diff --git a/src/eslint-adapter.ts b/src/eslint-adapter.ts index 5e71188b..6cb4e90d 100644 --- a/src/eslint-adapter.ts +++ b/src/eslint-adapter.ts @@ -100,6 +100,7 @@ export class ESLintAdapter { private readonly configProvider: ConfigProvider; private readonly getSourceFile: (fileName: string) => ts.SourceFile | undefined; private readonly ignoredFilepathMap: Map; + private readonly eslint: ESLint; public constructor({ logger, configProvider, getSourceFile }: ESLintAdapterOptions) { this.linter = new Linter(); @@ -107,6 +108,7 @@ export class ESLintAdapter { this.configProvider = configProvider; this.getSourceFile = getSourceFile; this.ignoredFilepathMap = new Map(); + this.eslint = new ESLint(); } private convertToESLintSourceCode(src: ts.SourceFile, filename: string, options?: ParserOptions | null) { @@ -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"))) { @@ -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(