Skip to content

feat: support for boolean project option #30

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Mar 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/violet-peaches-juggle.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"typescript-eslint-parser-for-extra-files": minor
---

feat: support for boolean project option
5 changes: 2 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import type { ParserOptions } from "@typescript-eslint/parser";
import type { ProgramOptions } from "./ts";
import { TSServiceManager } from "./ts";
import * as tsEslintParser from "@typescript-eslint/parser";
import { getProjectConfigFiles } from "./utils/get-project-config-files";

const DEFAULT_EXTRA_FILE_EXTENSIONS = [".vue", ".svelte", ".astro"];
const tsServiceManager = new TSServiceManager();
Expand Down Expand Up @@ -47,9 +48,7 @@ function* iterateOptions(options: ParserOptions): Iterable<ProgramOptions> {
"Specify `parserOptions.project`. Otherwise there is no point in using this parser."
);
}
for (const project of Array.isArray(options.project)
? options.project
: [options.project]) {
for (const project of getProjectConfigFiles(options)) {
yield {
project,
filePath: options.filePath,
Expand Down
32 changes: 32 additions & 0 deletions src/utils/get-project-config-files.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import type { ParserOptions } from "@typescript-eslint/parser";
import fs from "fs";
import path from "path";

export function getProjectConfigFiles(options: ParserOptions): string[] {
const tsconfigRootDir =
typeof options.tsconfigRootDir === "string"
? options.tsconfigRootDir
: process.cwd();
if (options.project !== true) {
return Array.isArray(options.project)
? options.project
: [options.project!];
}

let directory = path.dirname(options.filePath!);
const checkedDirectories = [directory];

do {
const tsconfigPath = path.join(directory, "tsconfig.json");
if (fs.existsSync(tsconfigPath)) {
return [tsconfigPath];
}

directory = path.dirname(directory);
checkedDirectories.push(directory);
} while (directory.length > 1 && directory.length >= tsconfigRootDir.length);

throw new Error(
`project was set to \`true\` but couldn't find any tsconfig.json relative to '${options.filePath}' within '${tsconfigRootDir}'.`
);
}
2 changes: 1 addition & 1 deletion tests/fixtures/types/vue/vue-script-setup01/types.vue
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { TypeFoo } from "../vue-script-setup02/source.vue"; // TypeFoo: any, Typ
import { numberValue } from "./number"; // numberValue: 1, numberValue: 1
let a: TypeFoo = $ref(1); // a: string, $ref(1): any
let b: TypeFoo; // b: string
defineProps<{ foo: string }>(); // defineProps<{ foo: string }>(): Readonly<{ foo: string; }>
defineProps<{ foo: string }>(); // defineProps<{ foo: string }>(): Readonly<Omit<{ foo: string; }, never> & {}>
console.log(numberValue); // console.log(numberValue): void
</script>

Expand Down