From 00203d480803cf95cc85e912a676efdeb8ca6ac6 Mon Sep 17 00:00:00 2001 From: David Michon Date: Fri, 22 Nov 2024 22:22:05 +0000 Subject: [PATCH] [eslint-plugin-tsdoc] Leverage tsConfigRootDir setting --- .../tsdoc-rule-tsconfig-path_2024-11-22-22-21.json | 10 ++++++++++ eslint-plugin/src/ConfigCache.ts | 11 +++++++++-- eslint-plugin/src/index.ts | 7 +++++-- 3 files changed, 24 insertions(+), 4 deletions(-) create mode 100644 common/changes/eslint-plugin-tsdoc/tsdoc-rule-tsconfig-path_2024-11-22-22-21.json diff --git a/common/changes/eslint-plugin-tsdoc/tsdoc-rule-tsconfig-path_2024-11-22-22-21.json b/common/changes/eslint-plugin-tsdoc/tsdoc-rule-tsconfig-path_2024-11-22-22-21.json new file mode 100644 index 00000000..127d5d3f --- /dev/null +++ b/common/changes/eslint-plugin-tsdoc/tsdoc-rule-tsconfig-path_2024-11-22-22-21.json @@ -0,0 +1,10 @@ +{ + "changes": [ + { + "packageName": "eslint-plugin-tsdoc", + "comment": "Leverage `parserOptions.tsConfigRootDir` to reduce file system probing. This field is commonly used when eslint is configured with `@typescript-eslint/parser`.", + "type": "minor" + } + ], + "packageName": "eslint-plugin-tsdoc" +} \ No newline at end of file diff --git a/eslint-plugin/src/ConfigCache.ts b/eslint-plugin/src/ConfigCache.ts index 91548cc5..f4ed8ded 100644 --- a/eslint-plugin/src/ConfigCache.ts +++ b/eslint-plugin/src/ConfigCache.ts @@ -26,6 +26,7 @@ const CACHE_MAX_SIZE: number = 100; export class ConfigCache { // findConfigPathForFolder() result --> loaded tsdoc.json configuration private static _cachedConfigs: Map = new Map(); + private static _cachedPaths: Map = new Map(); /** * Node.js equivalent of performance.now(). @@ -35,11 +36,17 @@ export class ConfigCache { return seconds * 1000 + nanoseconds / 1000000; } - public static getForSourceFile(sourceFilePath: string): TSDocConfigFile { + public static getForSourceFile( + sourceFilePath: string, + tsConfigRootDir?: string | undefined + ): TSDocConfigFile { const sourceFileFolder: string = path.dirname(path.resolve(sourceFilePath)); // First, determine the file to be loaded. If not found, the configFilePath will be an empty string. - const configFilePath: string = TSDocConfigFile.findConfigPathForFolder(sourceFileFolder); + // If the eslint config has specified where the tsconfig file is, use that path directly without probing the filesystem. + const configFilePath: string = tsConfigRootDir + ? path.join(tsConfigRootDir, TSDocConfigFile.FILENAME) + : TSDocConfigFile.findConfigPathForFolder(sourceFileFolder); // If configFilePath is an empty string, then we'll use the folder of sourceFilePath as our cache key // (instead of an empty string) diff --git a/eslint-plugin/src/index.ts b/eslint-plugin/src/index.ts index 94b9e2b3..b0234d0a 100644 --- a/eslint-plugin/src/index.ts +++ b/eslint-plugin/src/index.ts @@ -41,13 +41,16 @@ const plugin: IPlugin = { } }, create: (context: eslint.Rule.RuleContext) => { - const sourceFilePath: string = context.getFilename(); + const sourceFilePath: string = context.filename; + // If eslint is configured with @typescript-eslint/parser, there is a parser option + // to explicitly specify where the tsconfig file is. Use that if available. + const tsConfigDir: string | undefined = context.parserOptions.tsconfigRootDir; Debug.log(`Linting: "${sourceFilePath}"`); const tsdocConfiguration: TSDocConfiguration = new TSDocConfiguration(); try { - const tsdocConfigFile: TSDocConfigFile = ConfigCache.getForSourceFile(sourceFilePath); + const tsdocConfigFile: TSDocConfigFile = ConfigCache.getForSourceFile(sourceFilePath, tsConfigDir); if (!tsdocConfigFile.fileNotFound) { if (tsdocConfigFile.hasErrors) { context.report({