From 2062288c8e79349a3e3dbd31b7051474d2adf8b4 Mon Sep 17 00:00:00 2001 From: Gerrit Birkeland Date: Sat, 2 Oct 2021 10:55:54 -0600 Subject: [PATCH] Fix discovery of tsconfig.json when provided folder ends with .json Resolves #1712 --- CHANGELOG.md | 1 + src/lib/utils/options/readers/tsconfig.ts | 16 +++++++--------- .../readers/data/folder.json/tsconfig.json | 5 +++++ src/test/utils/options/readers/tsconfig.test.ts | 11 ++++++++++- 4 files changed, 23 insertions(+), 10 deletions(-) create mode 100644 src/test/utils/options/readers/data/folder.json/tsconfig.json diff --git a/CHANGELOG.md b/CHANGELOG.md index 2d9858f1a..31e362cc5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 `` 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! diff --git a/src/lib/utils/options/readers/tsconfig.ts b/src/lib/utils/options/readers/tsconfig.ts index 5d6b77353..4d024c29c 100644 --- a/src/lib/utils/options/readers/tsconfig.ts +++ b/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"; @@ -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}. @@ -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)) { diff --git a/src/test/utils/options/readers/data/folder.json/tsconfig.json b/src/test/utils/options/readers/data/folder.json/tsconfig.json new file mode 100644 index 000000000..8dc142972 --- /dev/null +++ b/src/test/utils/options/readers/data/folder.json/tsconfig.json @@ -0,0 +1,5 @@ +{ + "compilerOptions": { + "strict": true + } +} diff --git a/src/test/utils/options/readers/tsconfig.test.ts b/src/test/utils/options/readers/tsconfig.test.ts index eb2d0012b..f4319edd7 100644 --- a/src/test/utils/options/readers/tsconfig.test.ts +++ b/src/test/utils/options/readers/tsconfig.test.ts @@ -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()); @@ -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", @@ -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); + }); });