Skip to content

Commit d0d7347

Browse files
authored
feat: support for boolean project option (#30)
* feat: support for boolean project option * update * Create .changeset/violet-peaches-juggle.md
1 parent 30fcd9f commit d0d7347

File tree

4 files changed

+40
-4
lines changed

4 files changed

+40
-4
lines changed

Diff for: .changeset/violet-peaches-juggle.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"typescript-eslint-parser-for-extra-files": minor
3+
---
4+
5+
feat: support for boolean project option

Diff for: src/index.ts

+2-3
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,

Diff for: src/utils/get-project-config-files.ts

+32
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+
}

Diff for: tests/fixtures/types/vue/vue-script-setup01/types.vue

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import { TypeFoo } from "../vue-script-setup02/source.vue"; // TypeFoo: any, Typ
1010
import { numberValue } from "./number"; // numberValue: 1, numberValue: 1
1111
let a: TypeFoo = $ref(1); // a: string, $ref(1): any
1212
let b: TypeFoo; // b: string
13-
defineProps<{ foo: string }>(); // defineProps<{ foo: string }>(): Readonly<{ foo: string; }>
13+
defineProps<{ foo: string }>(); // defineProps<{ foo: string }>(): Readonly<Omit<{ foo: string; }, never> & {}>
1414
console.log(numberValue); // console.log(numberValue): void
1515
</script>
1616

0 commit comments

Comments
 (0)