Skip to content

Commit e3d7658

Browse files
committed
Add support for "module" comments on global files
Resolves #2165
1 parent 7e2871d commit e3d7658

File tree

7 files changed

+81
-3
lines changed

7 files changed

+81
-3
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Unreleased
22

3+
### Features
4+
5+
- Added support for discovering a "module" comment on global files, #2165.
6+
37
### Bug Fixes
48

59
- Even more contrast fixes, #2248.

src/lib/converter/comments/discovery.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,29 @@ export interface DiscoveredComment {
101101
jsDoc: ts.JSDoc | undefined;
102102
}
103103

104+
export function discoverFileComment(
105+
node: ts.SourceFile,
106+
commentStyle: CommentStyle
107+
) {
108+
const text = node.text;
109+
110+
const comments = collectCommentRanges(
111+
ts.getLeadingCommentRanges(text, node.pos)
112+
);
113+
114+
const selectedDocComment = comments.find((ranges) =>
115+
permittedRange(text, ranges, commentStyle)
116+
);
117+
118+
if (selectedDocComment) {
119+
return {
120+
file: node,
121+
ranges: selectedDocComment,
122+
jsDoc: findJsDocForComment(node, selectedDocComment),
123+
};
124+
}
125+
}
126+
104127
export function discoverComment(
105128
symbol: ts.Symbol,
106129
kind: ReflectionKind,

src/lib/converter/comments/index.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import { lexBlockComment } from "./blockLexer";
99
import {
1010
DiscoveredComment,
1111
discoverComment,
12+
discoverFileComment,
1213
discoverSignatureComment,
1314
} from "./discovery";
1415
import { lexLineComments } from "./lineLexer";
@@ -162,6 +163,22 @@ export function getComment(
162163
return comment;
163164
}
164165

166+
export function getFileComment(
167+
file: ts.SourceFile,
168+
config: CommentParserConfig,
169+
logger: Logger,
170+
commentStyle: CommentStyle,
171+
checker: ts.TypeChecker | undefined
172+
): Comment | undefined {
173+
return getCommentImpl(
174+
discoverFileComment(file, commentStyle),
175+
config,
176+
logger,
177+
/* moduleComment */ true,
178+
checker
179+
);
180+
}
181+
165182
function getConstructorParamPropertyComment(
166183
symbol: ts.Symbol,
167184
config: CommentParserConfig,

src/lib/converter/context.ts

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,12 @@ import type { Converter } from "./converter";
1414
import { isNamedNode } from "./utils/nodes";
1515
import { ConverterEvents } from "./converter-events";
1616
import { resolveAliasedSymbol } from "./utils/symbols";
17-
import { getComment, getJsDocComment, getSignatureComment } from "./comments";
17+
import {
18+
getComment,
19+
getFileComment,
20+
getJsDocComment,
21+
getSignatureComment,
22+
} from "./comments";
1823
import { getHumanName } from "../utils/tsutils";
1924

2025
/**
@@ -265,6 +270,16 @@ export class Context {
265270
);
266271
}
267272

273+
getFileComment(node: ts.SourceFile) {
274+
return getFileComment(
275+
node,
276+
this.converter.config,
277+
this.logger,
278+
this.converter.commentStyle,
279+
this.converter.useTsLinkResolution ? this.checker : undefined
280+
);
281+
}
282+
268283
getJsDocComment(
269284
declaration:
270285
| ts.JSDocPropertyLikeTag

src/lib/converter/converter.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -368,8 +368,9 @@ export class Converter extends ChildableComponent<
368368
// Special case for when we're giving a single entry point, we don't need to
369369
// create modules for each entry. Register the project as this module.
370370
context.project.registerReflection(context.project, symbol);
371-
context.project.comment =
372-
symbol && context.getComment(symbol, context.project.kind);
371+
context.project.comment = symbol
372+
? context.getComment(symbol, context.project.kind)
373+
: context.getFileComment(node);
373374
context.trigger(
374375
Converter.EVENT_CREATE_DECLARATION,
375376
context.project
@@ -383,6 +384,10 @@ export class Converter extends ChildableComponent<
383384
entryName
384385
);
385386

387+
if (!reflection.comment && !symbol) {
388+
reflection.comment = context.getFileComment(node);
389+
}
390+
386391
if (entryPoint.readmeFile) {
387392
const readme = readFile(entryPoint.readmeFile);
388393
const comment = this.parseRawComment(
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
/**
2+
* 'module' comment
3+
* @module
4+
*/
5+
6+
var globalFile = true;

src/test/issues.c2.test.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1008,6 +1008,14 @@ describe("Issue Tests", () => {
10081008
);
10091009
});
10101010

1011+
it("#2165 module comments on global files", () => {
1012+
const project = convert();
1013+
equal(
1014+
Comment.combineDisplayParts(project.comment?.summary),
1015+
"'module' comment"
1016+
);
1017+
});
1018+
10111019
it("#2175", () => {
10121020
const project = convert();
10131021
const def = query(project, "default");

0 commit comments

Comments
 (0)