Skip to content

Commit 00203d4

Browse files
committed
[eslint-plugin-tsdoc] Leverage tsConfigRootDir setting
1 parent 0362e09 commit 00203d4

File tree

3 files changed

+24
-4
lines changed

3 files changed

+24
-4
lines changed
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"changes": [
3+
{
4+
"packageName": "eslint-plugin-tsdoc",
5+
"comment": "Leverage `parserOptions.tsConfigRootDir` to reduce file system probing. This field is commonly used when eslint is configured with `@typescript-eslint/parser`.",
6+
"type": "minor"
7+
}
8+
],
9+
"packageName": "eslint-plugin-tsdoc"
10+
}

eslint-plugin/src/ConfigCache.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ const CACHE_MAX_SIZE: number = 100;
2626
export class ConfigCache {
2727
// findConfigPathForFolder() result --> loaded tsdoc.json configuration
2828
private static _cachedConfigs: Map<string, ICachedConfig> = new Map<string, ICachedConfig>();
29+
private static _cachedPaths: Map<string, string> = new Map();
2930

3031
/**
3132
* Node.js equivalent of performance.now().
@@ -35,11 +36,17 @@ export class ConfigCache {
3536
return seconds * 1000 + nanoseconds / 1000000;
3637
}
3738

38-
public static getForSourceFile(sourceFilePath: string): TSDocConfigFile {
39+
public static getForSourceFile(
40+
sourceFilePath: string,
41+
tsConfigRootDir?: string | undefined
42+
): TSDocConfigFile {
3943
const sourceFileFolder: string = path.dirname(path.resolve(sourceFilePath));
4044

4145
// First, determine the file to be loaded. If not found, the configFilePath will be an empty string.
42-
const configFilePath: string = TSDocConfigFile.findConfigPathForFolder(sourceFileFolder);
46+
// If the eslint config has specified where the tsconfig file is, use that path directly without probing the filesystem.
47+
const configFilePath: string = tsConfigRootDir
48+
? path.join(tsConfigRootDir, TSDocConfigFile.FILENAME)
49+
: TSDocConfigFile.findConfigPathForFolder(sourceFileFolder);
4350

4451
// If configFilePath is an empty string, then we'll use the folder of sourceFilePath as our cache key
4552
// (instead of an empty string)

eslint-plugin/src/index.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,13 +41,16 @@ const plugin: IPlugin = {
4141
}
4242
},
4343
create: (context: eslint.Rule.RuleContext) => {
44-
const sourceFilePath: string = context.getFilename();
44+
const sourceFilePath: string = context.filename;
45+
// If eslint is configured with @typescript-eslint/parser, there is a parser option
46+
// to explicitly specify where the tsconfig file is. Use that if available.
47+
const tsConfigDir: string | undefined = context.parserOptions.tsconfigRootDir;
4548
Debug.log(`Linting: "${sourceFilePath}"`);
4649

4750
const tsdocConfiguration: TSDocConfiguration = new TSDocConfiguration();
4851

4952
try {
50-
const tsdocConfigFile: TSDocConfigFile = ConfigCache.getForSourceFile(sourceFilePath);
53+
const tsdocConfigFile: TSDocConfigFile = ConfigCache.getForSourceFile(sourceFilePath, tsConfigDir);
5154
if (!tsdocConfigFile.fileNotFound) {
5255
if (tsdocConfigFile.hasErrors) {
5356
context.report({

0 commit comments

Comments
 (0)