Skip to content

Commit

Permalink
[eslint-plugin-tsdoc] Leverage tsConfigRootDir setting
Browse files Browse the repository at this point in the history
  • Loading branch information
dmichon-msft committed Nov 22, 2024
1 parent 0362e09 commit 00203d4
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -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"
}
11 changes: 9 additions & 2 deletions eslint-plugin/src/ConfigCache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ const CACHE_MAX_SIZE: number = 100;
export class ConfigCache {
// findConfigPathForFolder() result --> loaded tsdoc.json configuration
private static _cachedConfigs: Map<string, ICachedConfig> = new Map<string, ICachedConfig>();
private static _cachedPaths: Map<string, string> = new Map();

/**
* Node.js equivalent of performance.now().
Expand All @@ -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)
Expand Down
7 changes: 5 additions & 2 deletions eslint-plugin/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand Down

0 comments on commit 00203d4

Please sign in to comment.