Skip to content

Commit 1357984

Browse files
committed
Only take comments from exports for references and namespaces
Ref: #1901
1 parent f754312 commit 1357984

File tree

7 files changed

+88
-15
lines changed

7 files changed

+88
-15
lines changed

CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ These TODOs will be resolved before a full release. ([GitHub project](https://gi
2828
- Listeners to `Converter.EVENT_CREATE_TYPE_PARAMETER` and `Converter.EVENT_CREATE_DECLARATION` will now never be passed a `ts.Node` as their third argument.
2929
- Constant variables which are interpreted as functions will no longer have the `ReflectionFlag.Const` flag set.
3030
- Removed deprecated `removeReaderByName`, `addDeclarations` and `removeDeclarationByName` methods on `Options`.
31+
- Comments on export declarations will only overrides comments for references and namespaces, #1901.
3132

3233
### Features
3334

@@ -46,6 +47,7 @@ These TODOs will be resolved before a full release. ([GitHub project](https://gi
4647
- Improved comment discovery on destructured exported functions, #1770.
4748
- Links which refer to members within a reference reflection will now correctly resolve to the referenced reflection's member, #1770.
4849
- Correctly detect optional parameters in JavaScript projects using JSDoc, #1804.
50+
- JS exports defined as `exports.foo = ...` will now be converted as variables rather than properties.
4951

5052
### Thanks!
5153

@@ -81,7 +83,6 @@ These TODOs will be resolved before a full release. ([GitHub project](https://gi
8183
- Fixed `removeReflection` not completely removing reflections from the project, #1898.
8284
- Fixed `@hidden` / `@ignore` / `@exclude` comments on default exports with no associated variable, #1903.
8385
- `makeRecursiveVisitor` will now correctly call the `intersection` callback, #1910.
84-
- JS exports defined as `exports.foo = ...` will now be converted as variables rather than properties.
8586

8687
### Thanks!
8788

src/lib/converter/comments/discovery.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ const wantedKinds: Record<ReflectionKind, ts.SyntaxKind[]> = {
1414
ts.SyntaxKind.ModuleDeclaration,
1515
ts.SyntaxKind.SourceFile,
1616
ts.SyntaxKind.BindingElement,
17+
ts.SyntaxKind.ExportSpecifier,
1718
],
1819
[ReflectionKind.Enum]: [
1920
ts.SyntaxKind.EnumDeclaration,

src/lib/converter/context.ts

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -209,9 +209,23 @@ export class Context {
209209
const name = getHumanName(
210210
nameOverride ?? exportSymbol?.name ?? symbol?.name ?? "unknown"
211211
);
212+
212213
const reflection = new DeclarationReflection(name, kind, this.scope);
214+
this.postReflectionCreation(reflection, symbol, exportSymbol);
213215

214-
if (exportSymbol) {
216+
return reflection;
217+
}
218+
219+
postReflectionCreation(
220+
reflection: Reflection,
221+
symbol: ts.Symbol | undefined,
222+
exportSymbol: ts.Symbol | undefined
223+
) {
224+
if (
225+
exportSymbol &&
226+
reflection.kind &
227+
(ReflectionKind.SomeModule | ReflectionKind.Reference)
228+
) {
215229
reflection.comment = getComment(
216230
exportSymbol,
217231
reflection.kind,
@@ -232,18 +246,18 @@ export class Context {
232246
reflection.setFlag(ReflectionFlag.Static);
233247
}
234248

235-
reflection.escapedName = symbol?.escapedName;
249+
if (reflection instanceof DeclarationReflection) {
250+
reflection.escapedName = symbol?.escapedName;
251+
this.addChild(reflection);
252+
}
236253

237-
this.addChild(reflection);
238254
if (symbol && this.converter.isExternal(symbol)) {
239255
reflection.setFlag(ReflectionFlag.External);
240256
}
241257
if (exportSymbol) {
242258
this.registerReflection(reflection, exportSymbol);
243259
}
244260
this.registerReflection(reflection, symbol);
245-
246-
return reflection;
247261
}
248262

249263
finalizeDeclarationReflection(reflection: DeclarationReflection) {

src/lib/converter/symbols.ts

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -358,7 +358,7 @@ function createTypeParamReflection(
358358
defaultType,
359359
context.scope
360360
);
361-
context.registerReflection(paramRefl, param.symbol);
361+
context.postReflectionCreation(paramRefl, param.symbol, void 0);
362362
context.trigger(ConverterEvents.CREATE_TYPE_PARAMETER, paramRefl);
363363
return paramRefl;
364364
}
@@ -753,10 +753,8 @@ function convertConstructSignatures(context: Context, symbol: ts.Symbol) {
753753
ReflectionKind.Constructor,
754754
context.scope
755755
);
756-
context.addChild(constructMember);
757-
context.registerReflection(constructMember, symbol);
758-
759-
context.trigger(ConverterEvents.CREATE_DECLARATION, constructMember);
756+
context.postReflectionCreation(constructMember, symbol, void 0);
757+
context.finalizeDeclarationReflection(constructMember);
760758

761759
const constructContext = context.withScope(constructMember);
762760

@@ -802,10 +800,8 @@ function createAlias(
802800
target,
803801
context.scope
804802
);
805-
context.addChild(ref);
806-
context.registerReflection(ref, symbol);
807-
808-
context.trigger(ConverterEvents.CREATE_DECLARATION, ref);
803+
context.postReflectionCreation(ref, symbol, exportSymbol);
804+
context.finalizeDeclarationReflection(ref);
809805
}
810806

811807
function convertVariable(

src/test/behaviorTests.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,22 @@ export const behaviorTests: Record<
102102
]);
103103
},
104104

105+
exportComments(project) {
106+
const abc = query(project, "abc");
107+
equal(abc.kind, ReflectionKind.Variable);
108+
equal(Comment.combineDisplayParts(abc.comment?.summary), "abc");
109+
110+
const abcRef = query(project, "abcRef");
111+
equal(abcRef.kind, ReflectionKind.Reference);
112+
equal(
113+
Comment.combineDisplayParts(abcRef.comment?.summary),
114+
"export abc"
115+
);
116+
117+
const foo = query(project, "foo");
118+
equal(Comment.combineDisplayParts(foo.comment?.summary), "export foo");
119+
},
120+
105121
inheritDocBasic(project) {
106122
const target = query(project, "InterfaceTarget");
107123
const comment = new Comment(

src/test/converter/exports/specs.json

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,14 @@
4747
"kind": 16777216,
4848
"kindString": "Reference",
4949
"flags": {},
50+
"comment": {
51+
"summary": [
52+
{
53+
"kind": "text",
54+
"text": "This is a comment for Mod that overwrites the one specified in \"mod\""
55+
}
56+
]
57+
},
5058
"sources": [
5159
{
5260
"fileName": "export.ts",
@@ -107,6 +115,14 @@
107115
"kind": 16777216,
108116
"kindString": "Reference",
109117
"flags": {},
118+
"comment": {
119+
"summary": [
120+
{
121+
"kind": "text",
122+
"text": "An export of a local under a different name."
123+
}
124+
]
125+
},
110126
"sources": [
111127
{
112128
"fileName": "mod.ts",
@@ -657,6 +673,14 @@
657673
"kind": 16777216,
658674
"kindString": "Reference",
659675
"flags": {},
676+
"comment": {
677+
"summary": [
678+
{
679+
"kind": "text",
680+
"text": "An export of a local under a different name."
681+
}
682+
]
683+
},
660684
"sources": [
661685
{
662686
"fileName": "mod.ts",
@@ -672,6 +696,14 @@
672696
"kind": 16777216,
673697
"kindString": "Reference",
674698
"flags": {},
699+
"comment": {
700+
"summary": [
701+
{
702+
"kind": "text",
703+
"text": "An export with a module specifier that comes from this file."
704+
}
705+
]
706+
},
675707
"sources": [
676708
{
677709
"fileName": "mod.ts",
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
/** abc */
2+
const abc = 123;
3+
4+
/** export abc */
5+
export { abc, abc as abcRef };
6+
7+
/** foo */
8+
namespace foo {
9+
export const abc = 123;
10+
}
11+
12+
/** export foo */
13+
export { foo };

0 commit comments

Comments
 (0)