Skip to content

Commit a41ac2d

Browse files
committed
Correctly set kind for class/interface children
Closes #2150
1 parent ce7cda4 commit a41ac2d

File tree

8 files changed

+79
-24
lines changed

8 files changed

+79
-24
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050

5151
- TypeDoc will now ignore package.json files not containing a `name` field, #2190.
5252
- Fixed `@inheritDoc` on signatures (functions, methods, constructors, getters, setters) being unable to inherit from a non-signature.
53+
- Interfaces/classes created via extending a module will no longer contain variables/functions where the member should have been converted as properties/methods, #2150.
5354

5455
### Thanks!
5556

src/lib/converter/comments/discovery.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ const wantedKinds: Record<ReflectionKind, ts.SyntaxKind[]> = {
3030
ts.SyntaxKind.PropertySignature,
3131
],
3232
[ReflectionKind.Variable]: [
33+
// everything here should also be in ReflectionKind.Property
3334
ts.SyntaxKind.VariableDeclaration,
3435
ts.SyntaxKind.BindingElement,
3536
ts.SyntaxKind.ExportAssignment,
@@ -55,6 +56,11 @@ const wantedKinds: Record<ReflectionKind, ts.SyntaxKind[]> = {
5556
ts.SyntaxKind.PropertyAssignment,
5657
// class X { constructor(/** Comment */ readonly z: string) }
5758
ts.SyntaxKind.Parameter,
59+
// Variable values
60+
ts.SyntaxKind.VariableDeclaration,
61+
ts.SyntaxKind.BindingElement,
62+
ts.SyntaxKind.ExportAssignment,
63+
ts.SyntaxKind.PropertyAccessExpression,
5864
],
5965
[ReflectionKind.Method]: [
6066
ts.SyntaxKind.FunctionDeclaration,

src/lib/converter/context.ts

Lines changed: 12 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -61,20 +61,9 @@ export class Context {
6161
*/
6262
readonly scope: Reflection;
6363

64-
/** @internal */
65-
isConvertingTypeNode(): boolean {
66-
return this.convertingTypeNode;
67-
}
68-
69-
/** @internal */
70-
setConvertingTypeNode() {
71-
this.convertingTypeNode = true;
72-
}
73-
74-
/** @internal */
75-
shouldBeStatic = false;
76-
77-
private convertingTypeNode = false;
64+
convertingTypeNode = false; // Inherited by withScope
65+
convertingClassOrInterface = false; // Not inherited
66+
shouldBeStatic = false; // Not inherited
7867

7968
/**
8069
* Create a new Context instance.
@@ -100,13 +89,6 @@ export class Context {
10089
return this.converter.application.logger;
10190
}
10291

103-
/**
104-
* Return the compiler options.
105-
*/
106-
getCompilerOptions(): ts.CompilerOptions {
107-
return this.converter.application.options.getCompilerOptions();
108-
}
109-
11092
/**
11193
* Return the type declaration of the given node.
11294
*
@@ -175,6 +157,15 @@ export class Context {
175157
nameOverride ?? exportSymbol?.name ?? symbol?.name ?? "unknown"
176158
);
177159

160+
if (this.convertingClassOrInterface) {
161+
if (kind === ReflectionKind.Function) {
162+
kind = ReflectionKind.Method;
163+
}
164+
if (kind === ReflectionKind.Variable) {
165+
kind = ReflectionKind.Property;
166+
}
167+
}
168+
178169
const reflection = new DeclarationReflection(name, kind, this.scope);
179170
this.postReflectionCreation(reflection, symbol, exportSymbol);
180171

src/lib/converter/symbols.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -447,6 +447,7 @@ function convertClassOrInterface(
447447
if (classDeclaration) setModifiers(symbol, classDeclaration, reflection);
448448

449449
const reflectionContext = context.withScope(reflection);
450+
reflectionContext.convertingClassOrInterface = true;
450451

451452
const instanceType = context.checker.getDeclaredTypeOfSymbol(symbol);
452453
assert(instanceType.isClassOrInterface());
@@ -643,7 +644,7 @@ function convertProperty(
643644

644645
reflection.type = context.converter.convertType(
645646
context.withScope(reflection),
646-
(context.isConvertingTypeNode() ? parameterType : void 0) ??
647+
(context.convertingTypeNode ? parameterType : void 0) ??
647648
context.checker.getTypeOfSymbol(symbol)
648649
);
649650

src/lib/converter/types.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,7 @@ const constructorConverter: TypeConverter<ts.ConstructorTypeNode, ts.Type> = {
225225
context.scope
226226
);
227227
const rc = context.withScope(reflection);
228-
rc.setConvertingTypeNode();
228+
rc.convertingTypeNode = true;
229229

230230
context.registerReflection(reflection, symbol);
231231
context.trigger(ConverterEvents.CREATE_DECLARATION, reflection);
@@ -557,7 +557,7 @@ const typeLiteralConverter: TypeConverter<ts.TypeLiteralNode> = {
557557
context.scope
558558
);
559559
const rc = context.withScope(reflection);
560-
rc.setConvertingTypeNode();
560+
rc.convertingTypeNode = true;
561561

562562
context.registerReflection(reflection, symbol);
563563
context.trigger(ConverterEvents.CREATE_DECLARATION, reflection);
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import * as Int from "./int";
2+
3+
type Int = typeof Int;
4+
5+
export interface FileInt extends Int {}
6+
7+
export class FileClass {}
8+
export interface FileClass extends Int {}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
/** intFn doc */
2+
export function intFn() {}
3+
4+
/** intVar doc */
5+
export const intVar = 123;
6+
7+
/** constFn doc */
8+
export const constFn = () => {};

src/test/issueTests.ts

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -832,6 +832,46 @@ export const issueTests: {
832832
);
833833
},
834834

835+
gh2150(project) {
836+
const intFn = query(project, "FileInt.intFn");
837+
equal(intFn.kind, ReflectionKind.Method, "intFn interface method");
838+
equal(
839+
Comment.combineDisplayParts(intFn.signatures?.[0].comment?.summary),
840+
"intFn doc"
841+
);
842+
843+
const intProp = query(project, "FileInt.intVar");
844+
equal(intProp.kind, ReflectionKind.Property, "intVar interface prop");
845+
equal(
846+
Comment.combineDisplayParts(intProp.comment?.summary),
847+
"intVar doc"
848+
);
849+
850+
const constFn = query(project, "FileInt.constFn");
851+
equal(constFn.kind, ReflectionKind.Method, "constFn interface method");
852+
equal(
853+
Comment.combineDisplayParts(
854+
constFn.signatures?.[0].comment?.summary
855+
),
856+
"constFn doc"
857+
);
858+
859+
const intFn2 = query(project, "FileClass.intFn");
860+
equal(intFn2.kind, ReflectionKind.Method, "intFn class method");
861+
862+
const intProp2 = query(project, "FileClass.intVar");
863+
equal(intProp2.kind, ReflectionKind.Property, "intVar class prop");
864+
865+
const constFn2 = query(project, "FileClass.constFn");
866+
equal(constFn2.kind, ReflectionKind.Method, "constFn class method");
867+
equal(
868+
Comment.combineDisplayParts(
869+
constFn2.signatures?.[0].comment?.summary
870+
),
871+
"constFn doc"
872+
);
873+
},
874+
835875
pre2156(app) {
836876
app.options.setValue("excludeNotDocumented", true);
837877
},

0 commit comments

Comments
 (0)