Skip to content

Commit 89927fd

Browse files
committed
feat: support for boolean project option
1 parent 30fcd9f commit 89927fd

File tree

2 files changed

+34
-3
lines changed

2 files changed

+34
-3
lines changed

src/index.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import type { ParserOptions } from "@typescript-eslint/parser";
22
import type { ProgramOptions } from "./ts";
33
import { TSServiceManager } from "./ts";
44
import * as tsEslintParser from "@typescript-eslint/parser";
5+
import { getProjectConfigFiles } from "./utils/get-project-config-files";
56

67
const DEFAULT_EXTRA_FILE_EXTENSIONS = [".vue", ".svelte", ".astro"];
78
const tsServiceManager = new TSServiceManager();
@@ -47,9 +48,7 @@ function* iterateOptions(options: ParserOptions): Iterable<ProgramOptions> {
4748
"Specify `parserOptions.project`. Otherwise there is no point in using this parser."
4849
);
4950
}
50-
for (const project of Array.isArray(options.project)
51-
? options.project
52-
: [options.project]) {
51+
for (const project of getProjectConfigFiles(options)) {
5352
yield {
5453
project,
5554
filePath: options.filePath,

src/utils/get-project-config-files.ts

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import type { ParserOptions } from "@typescript-eslint/parser";
2+
import fs from "fs";
3+
import path from "path";
4+
5+
export function getProjectConfigFiles(options: ParserOptions): string[] {
6+
const tsconfigRootDir =
7+
typeof options.tsconfigRootDir === "string"
8+
? options.tsconfigRootDir
9+
: process.cwd();
10+
if (options.project !== true) {
11+
return Array.isArray(options.project)
12+
? options.project
13+
: [options.project!];
14+
}
15+
16+
let directory = path.dirname(options.filePath!);
17+
const checkedDirectories = [directory];
18+
19+
do {
20+
const tsconfigPath = path.join(directory, "tsconfig.json");
21+
if (fs.existsSync(tsconfigPath)) {
22+
return [tsconfigPath];
23+
}
24+
25+
directory = path.dirname(directory);
26+
checkedDirectories.push(directory);
27+
} while (directory.length > 1 && directory.length >= tsconfigRootDir.length);
28+
29+
throw new Error(
30+
`project was set to \`true\` but couldn't find any tsconfig.json relative to '${options.filePath}' within '${tsconfigRootDir}'.`
31+
);
32+
}

0 commit comments

Comments
 (0)