Skip to content

Commit

Permalink
Detect duplicate nodes when discovering comments
Browse files Browse the repository at this point in the history
Closes #2359
Resolves #2437

Co-Authored-By: Sebastian Malton <sebastian@malton.name>
  • Loading branch information
Gerrit0 and Nokel81 committed Nov 11, 2023
1 parent c9103fc commit aba0ef8
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 7 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@
- Fixed default option values on options declared by plugins in packages mode, #2433.
- `gitRevision` will now be replaced in `sourceLinkTemplate`, #2434.
- Improved handling of function-modules created with `Object.assign`, #2436.
- TypeDoc will no longer warn about duplicate comments with warnings which point to a single comment, #2437
- Fixed an infinite loop when `skipLibCheck` is used to ignore some compiler errors, #2438.

### Thanks!

- @Nokel81
- @ocavue
- @swarnpallav

Expand Down
6 changes: 5 additions & 1 deletion scripts/testcase.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,11 @@ async function main() {
const tokens = lexer.lex(data.body);

const code = /** @type {marked.marked.Tokens.Code} */ (
tokens.find((tok) => tok.type === "code")
tokens.find(
(tok) =>
tok.type === "code" &&
["ts", "tsx", "js", "jsx"].includes(tok.lang || ""),
) || tokens.find((tok) => tok.type === "code")
);
if (!code) {
console.log("No codeblock found");
Expand Down
4 changes: 3 additions & 1 deletion src/lib/converter/comments/discovery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,14 +140,16 @@ export function discoverComment(
const reverse = !symbol.declarations?.some(ts.isSourceFile);

const discovered: DiscoveredComment[] = [];
const seen = new Set<ts.Node>();

for (const decl of symbol.declarations || []) {
const text = decl.getSourceFile().text;
if (wantedKinds[kind].includes(decl.kind)) {
const node = declarationToCommentNode(decl);
if (!node) {
if (!node || seen.has(node)) {
continue;
}
seen.add(node);

// Special behavior here! We temporarily put the implementation comment
// on the reflection which contains all the signatures. This lets us pull
Expand Down
15 changes: 10 additions & 5 deletions src/lib/converter/symbols.ts
Original file line number Diff line number Diff line change
Expand Up @@ -856,10 +856,11 @@ function convertVariable(
exportSymbol?: ts.Symbol,
) {
const declaration = symbol.getDeclarations()?.[0];
assert(declaration);

const comment = context.getComment(symbol, ReflectionKind.Variable);
const type = context.checker.getTypeOfSymbolAtLocation(symbol, declaration);
const type = declaration
? context.checker.getTypeOfSymbolAtLocation(symbol, declaration)
: context.checker.getTypeOfSymbol(symbol);

if (
isEnumLike(context.checker, type, declaration) &&
Expand All @@ -883,7 +884,7 @@ function convertVariable(
);

let typeNode: ts.TypeNode | undefined;
if (ts.isVariableDeclaration(declaration)) {
if (declaration && ts.isVariableDeclaration(declaration)) {
// Otherwise we might have destructuring
typeNode = declaration.type;
}
Expand All @@ -902,8 +903,12 @@ function convertVariable(
return ts.SymbolFlags.Property;
}

function isEnumLike(checker: ts.TypeChecker, type: ts.Type, location: ts.Node) {
if (!hasAllFlags(type.flags, ts.TypeFlags.Object)) {
function isEnumLike(
checker: ts.TypeChecker,
type: ts.Type,
location?: ts.Node,
) {
if (!location || !hasAllFlags(type.flags, ts.TypeFlags.Object)) {
return false;
}

Expand Down
18 changes: 18 additions & 0 deletions src/test/converter2/issues/gh2437.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
export interface TemplatedTypeBase {
/**
* Doc here
*/
prop?: string[];
}

export interface One extends TemplatedTypeBase {}

export interface Two extends TemplatedTypeBase {}

export type Type = One | Two;

export function isTemplateInstance(
type: Type,
): type is Type & { prop: string[] } {
return true;
}
5 changes: 5 additions & 0 deletions src/test/issues.c2.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1213,6 +1213,11 @@ describe("Issue Tests", () => {
);
});

it("Does not warn due to the diamond problem in comment discovery #2437", () => {
convert();
logger.expectNoOtherMessages();
});

it("Handles recursive aliases without looping infinitely #2438", () => {
const bad = query(convert(), "Bad");
equal(bad.kind, ReflectionKind.Interface);
Expand Down

0 comments on commit aba0ef8

Please sign in to comment.