Skip to content
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

feat: add support for jsconfig.json #199

Merged
merged 6 commits into from Apr 18, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
63 changes: 51 additions & 12 deletions src/__tests__/tsconfig-loader.test.ts
Expand Up @@ -107,27 +107,66 @@ describe("tsconfig-loader", () => {
describe("walkForTsConfig", () => {
it("should find tsconfig in starting directory", () => {
const pathToTsconfig = join("/root", "dir1", "tsconfig.json");
const res = walkForTsConfig(
join("/root", "dir1"),
(path) => path === pathToTsconfig
);
// assert.equal(res, pathToTsconfig);
const res = walkForTsConfig(join("/root", "dir1"), (path) => {
if (path === "/root/dir1") {
return ["tsconfig.json"];
} else {
return [];
}
});
expect(res).toBe(pathToTsconfig);
});

it("should find jsconfig in starting directory", () => {
const pathToJsconfig = join("/root", "dir1", "jsconfig.json");
const res = walkForTsConfig(join("/root", "dir1"), (path) => {
if (path === "/root/dir1") {
return ["jsconfig.json"];
} else {
return [];
}
});
expect(res).toBe(pathToJsconfig);
});

it("tsconfig.json take precedence over jsconfig.json when both exist", () => {
F3n67u marked this conversation as resolved.
Show resolved Hide resolved
const pathToTsconfig = join("/root/dir1", "tsconfig.json");
const res = walkForTsConfig(join("/root", "dir1"), (path) => {
if (path === "/root/dir1") {
return ["jsconfig.json", "tsconfig.json"];
} else {
return [];
}
});
expect(res).toBe(pathToTsconfig);
});

it("should find tsconfig in parent directory", () => {
const pathToTsconfig = join("/root", "tsconfig.json");
const res = walkForTsConfig(
join("/root", "dir1"),
(path) => path === pathToTsconfig
);
// assert.equal(res, pathToTsconfig);
const res = walkForTsConfig(join("/root", "dir1"), (path) => {
if (path === "/root") {
return ["tsconfig.json"];
} else {
return [];
}
});
expect(res).toBe(pathToTsconfig);
});

it("should find jsconfig in parent directory", () => {
const pathToTsconfig = join("/root", "jsconfig.json");
const res = walkForTsConfig(join("/root", "dir1"), (path) => {
if (path === "/root") {
return ["jsconfig.json"];
} else {
return [];
}
});
expect(res).toBe(pathToTsconfig);
});

it("should return undefined when reaching the top", () => {
const res = walkForTsConfig(join("/root", "dir1", "kalle"), () => false);
// assert.equal(res, undefined);
const res = walkForTsConfig(join("/root", "dir1", "kalle"), () => []);
expect(res).toBeUndefined();
});
});
Expand Down
17 changes: 9 additions & 8 deletions src/tsconfig-loader.ts
Expand Up @@ -90,25 +90,26 @@ function resolveConfigPath(cwd: string, filename?: string): string | undefined {
const configAbsolutePath = walkForTsConfig(cwd);
return configAbsolutePath ? path.resolve(configAbsolutePath) : undefined;
}

export function walkForTsConfig(
directory: string,
// eslint-disable-next-line no-shadow
existsSync: (path: string) => boolean = fs.existsSync
readdirSync: (path: string) => string[] = fs.readdirSync
): string | undefined {
const configPath = path.join(directory, "./tsconfig.json");
if (existsSync(configPath)) {
return configPath;
const files = readdirSync(directory);
const filesToCheck = ["tsconfig.json", "jsconfig.json"];
for (const fileToCheck of filesToCheck) {
if (files.indexOf(fileToCheck) !== -1) {
F3n67u marked this conversation as resolved.
Show resolved Hide resolved
return path.join(directory, fileToCheck);
}
}

const parentDirectory = path.join(directory, "../");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jonaskello Was there a good reason for using path.join instead of path.dirname here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • path.join('/root/dir', '../') return /root/
  • path.dirname('/root/dir') return /root(without the trailing directory separators)

the latter is normalized, easier to mock when we mock the fs.readdirSync, so I adjust path.join to path.dirname; though path.join(directory, '..') is fine either.

const parentDirectory = path.dirname(directory);

// If we reached the top
if (directory === parentDirectory) {
return undefined;
}

return walkForTsConfig(parentDirectory, existsSync);
return walkForTsConfig(parentDirectory, readdirSync);
}

export function loadTsconfig(
Expand Down