Skip to content

Commit

Permalink
Fix discovery of tsconfig.json when provided folder ends with .json
Browse files Browse the repository at this point in the history
Resolves #1712
  • Loading branch information
Gerrit0 committed Oct 2, 2021
1 parent 455b36b commit 2062288
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 10 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -26,6 +26,7 @@
- TypeDoc will now only create one highlighter for rendering code, saving ~200-500ms for rendering time.
- For compatibility with JSDoc, TypeDoc will now strip `<caption>` elements from `@example` tags, resolves #1679.
- TypeScript's `emitDeclarationOnly` compiler option is now supported, resolves #1716.
- Fixed discovery of tsconfig.json when the provided path ends in `.json`, resolves #1712.
- Fixed a crash when converting the `globalThis` namespace, could only be caused by a plugin.

### Thanks!
Expand Down
16 changes: 7 additions & 9 deletions src/lib/utils/options/readers/tsconfig.ts
@@ -1,4 +1,4 @@
import { resolve, basename, join } from "path";
import { resolve, join } from "path";
import { existsSync, statSync } from "fs";

import * as ts from "typescript";
Expand All @@ -12,6 +12,10 @@ function isFile(file: string) {
return existsSync(file) && statSync(file).isFile();
}

function isDir(path: string) {
return existsSync(path) && statSync(path).isDirectory();
}

export class TSConfigReader implements OptionsReader {
/**
* Note: Runs after the {@link TypeDocReader}.
Expand All @@ -24,14 +28,8 @@ export class TSConfigReader implements OptionsReader {
const file = container.getValue("tsconfig");

let fileToRead: string | undefined = file;
if (!isFile(fileToRead)) {
fileToRead = ts.findConfigFile(
file,
isFile,
file.toLowerCase().endsWith(".json")
? basename(file)
: undefined
);
if (isDir(fileToRead)) {
fileToRead = ts.findConfigFile(file, isFile);
}

if (!fileToRead || !isFile(fileToRead)) {
Expand Down
5 changes: 5 additions & 0 deletions src/test/utils/options/readers/data/folder.json/tsconfig.json
@@ -0,0 +1,5 @@
{
"compilerOptions": {
"strict": true
}
}
11 changes: 10 additions & 1 deletion src/test/utils/options/readers/tsconfig.test.ts
Expand Up @@ -3,6 +3,7 @@ import { deepStrictEqual as equal } from "assert";

import { TSConfigReader } from "../../../../lib/utils/options/readers";
import { Logger, Options } from "../../../../lib/utils";
import { tmpdir } from "os";

describe("Options - TSConfigReader", () => {
const options = new Options(new Logger());
Expand All @@ -21,7 +22,7 @@ describe("Options - TSConfigReader", () => {

testError(
"Errors if the file cannot be found",
join(__dirname, "data/non-existent-file.json")
join(tmpdir(), "typedoc/non-existent-file.json")
);
testError(
"Errors if the data is invalid",
Expand Down Expand Up @@ -87,4 +88,12 @@ describe("Options - TSConfigReader", () => {
options.read(new Logger());
equal(options.getValue("excludeInternal"), false);
});

it("Correctly handles folder names ending with .json (#1712)", () => {
options.reset();
options.setValue("tsconfig", join(__dirname, "data/folder.json"));
options.setCompilerOptions([], { strict: false }, void 0);
options.read(new Logger());
equal(options.getCompilerOptions().strict, true);
});
});

0 comments on commit 2062288

Please sign in to comment.