Skip to content

Commit

Permalink
Inherit typedocOptions fields from extended tsconfig files
Browse files Browse the repository at this point in the history
Fixes #2334.
  • Loading branch information
Gerrit0 committed Dec 27, 2023
1 parent e23a924 commit a2739c7
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 4 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
### Bug Fixes

- Type parameters will now be resolved for arrow-methods on classes like regular class methods, #2320.
- TypeDoc now inherits `typedocOptions` fields from extended tsconfig files, #2334.
- Methods which return function types no longer have duplicated comments, #2336.
- Comments on function-like type aliases will now show up under the type alias, rather than nested within the type declaration, #2372.
- Fix crash when converting some complicated union/intersection types, #2451.
Expand Down
11 changes: 9 additions & 2 deletions src/lib/utils/options/readers/tsconfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,11 @@ import {
tsdocModifierTags,
} from "../tsdoc-defaults";
import { unique } from "../../array";
import { findTsConfigFile, readTsConfig } from "../../tsconfig";
import {
findTsConfigFile,
getTypeDocOptionsFromTsConfig,
readTsConfig,
} from "../../tsconfig";

function isSupportForTags(obj: unknown): obj is Record<`@${string}`, boolean> {
return (
Expand Down Expand Up @@ -95,8 +99,11 @@ export class TSConfigReader implements OptionsReader {
}

logger.diagnostics(parsed.errors);
if (parsed.errors.length) {
return;
}

const typedocOptions = parsed.raw?.typedocOptions ?? {};
const typedocOptions = getTypeDocOptionsFromTsConfig(fileToRead);
if (typedocOptions.options) {
logger.error(
[
Expand Down
42 changes: 41 additions & 1 deletion src/lib/utils/tsconfig.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import ts from "typescript";
import { isFile, isDir } from "./fs";
import { isFile, isDir, readFile } from "./fs";
import type { Logger } from "./loggers";
import { createRequire } from "module";

export function findTsConfigFile(path: string): string | undefined {
let fileToRead: string | undefined = path;
Expand All @@ -15,6 +16,45 @@ export function findTsConfigFile(path: string): string | undefined {
return fileToRead;
}

// We don't need recursive read checks because that would cause a diagnostic
// when reading the tsconfig for compiler options, which happens first, and we bail before
// doing this in that case.
export function getTypeDocOptionsFromTsConfig(file: string): any {
const readResult = ts.readConfigFile(file, readFile);

const result = {};

if (readResult.error) {
return result;
}

if ("extends" in readResult.config) {
const resolver = createRequire(file);
const extended = Array.isArray(readResult.config.extends)
? readResult.config.extends.map(String)
: [String(readResult.config.extends)];

for (const extendedFile of extended) {
let resolvedParent: string;
try {
resolvedParent = resolver.resolve(extendedFile);
} catch {
continue;
}
Object.assign(
result,
getTypeDocOptionsFromTsConfig(resolvedParent),
);
}
}

if ("typedocOptions" in readResult.config) {
Object.assign(result, readResult.config.typedocOptions);
}

return result;
}

const tsConfigCache: Record<string, ts.ParsedCommandLine> = {};

export function readTsConfig(
Expand Down
1 change: 0 additions & 1 deletion src/test/converter2/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@

"noImplicitAny": false,

// See #1524. We might force this to false eventually.
"skipLibCheck": true
},
"typedocOptions": {
Expand Down
18 changes: 18 additions & 0 deletions src/test/utils/options/readers/tsconfig.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,24 @@ describe("Options - TSConfigReader", () => {
equal(logger.hasErrors(), false);
});

it("Reads typedocOptions from extended tsconfig files", async () => {
const project = tempdirProject();
project.addFile("file.ts", "export const abc = 123");
project.addJsonFile("tsconfig.json", {
extends: ["./base.tsconfig.json"],
files: ["./file.ts"],
typedocOptions: { plugin: ["a"] },
});
project.addJsonFile("base.tsconfig.json", {
typedocOptions: { name: "a", plugin: ["b"] },
});

await readWithProject(project);
logger.expectNoOtherMessages();
equal(options.getValue("name"), "a");
equal(options.getValue("plugin"), ["a"]);
});

async function readTsconfig(tsconfig: object) {
const project = tempdirProject();
project.addFile("file.ts", "export const abc = 123");
Expand Down

0 comments on commit a2739c7

Please sign in to comment.