Skip to content

Commit 0ea3768

Browse files
Copiloticlanton
andcommitted
Add @module comment support to namespace declarations
- Added @module tag definition to TSDoc configuration - Created ModuleDocComment helper to extract @module comments - Modified DtsRollupGenerator to apply @module comments to namespaces - Added logic to skip @module comments on regular declarations - Created test case moduleNamespaceComment to verify functionality Co-authored-by: iclanton <[email protected]>
1 parent 67477f0 commit 0ea3768

File tree

67 files changed

+730
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

67 files changed

+730
-0
lines changed

apps/api-extractor/extends/tsdoc-base.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,10 @@
2929
"tagName": "@internalRemarks",
3030
"syntaxKind": "block"
3131
},
32+
{
33+
"tagName": "@module",
34+
"syntaxKind": "modifier"
35+
},
3236
{
3337
"tagName": "@preapproved",
3438
"syntaxKind": "modifier"
@@ -51,6 +55,7 @@
5155
"@internal": true,
5256
"@label": true,
5357
"@link": true,
58+
"@module": true,
5459
"@override": true,
5560
"@packageDocumentation": true,
5661
"@param": true,
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license.
2+
// See LICENSE in the project root for license information.
3+
4+
import * as ts from 'typescript';
5+
6+
export class ModuleDocComment {
7+
/**
8+
* For the given source file, see if it starts with a TSDoc comment containing the `@module` tag.
9+
*/
10+
public static tryFindInSourceFile(sourceFile: ts.SourceFile): ts.TextRange | undefined {
11+
// The @module comment is special because it is not attached to an AST
12+
// definition. Instead, it is part of the "trivia" tokens that the compiler treats
13+
// as irrelevant white space.
14+
//
15+
// This implementation assumes that the "@module" will be in the first TSDoc comment
16+
// that appears in the source file.
17+
let moduleCommentRange: ts.TextRange | undefined = undefined;
18+
19+
for (const commentRange of ts.getLeadingCommentRanges(sourceFile.text, sourceFile.getFullStart()) || []) {
20+
if (commentRange.kind === ts.SyntaxKind.MultiLineCommentTrivia) {
21+
const commentBody: string = sourceFile.text.substring(commentRange.pos, commentRange.end);
22+
23+
// Choose the first JSDoc-style comment
24+
if (/^\s*\/\*\*/.test(commentBody)) {
25+
// But only if it looks like it's trying to be @module
26+
// (The TSDoc parser will validate this more rigorously)
27+
if (/\@module/i.test(commentBody)) {
28+
moduleCommentRange = commentRange;
29+
}
30+
break;
31+
}
32+
}
33+
}
34+
35+
return moduleCommentRange;
36+
}
37+
}

apps/api-extractor/src/generators/DtsRollupGenerator.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import { AstNamespaceImport } from '../analyzer/AstNamespaceImport';
2222
import type { IAstModuleExportInfo } from '../analyzer/AstModule';
2323
import { SourceFileLocationFormatter } from '../analyzer/SourceFileLocationFormatter';
2424
import type { AstEntity } from '../analyzer/AstEntity';
25+
import { ModuleDocComment } from '../aedoc/ModuleDocComment';
2526

2627
/**
2728
* Used with DtsRollupGenerator.writeTypingsFile()
@@ -180,6 +181,18 @@ export class DtsRollupGenerator {
180181
// signatures may reference them directly (without using the namespace qualifier).
181182

182183
writer.ensureSkippedLine();
184+
185+
// Check if the source file has a @module comment and emit it before the namespace declaration
186+
const sourceFile: ts.SourceFile = astEntity.astModule.sourceFile;
187+
const moduleCommentRange: ts.TextRange | undefined = ModuleDocComment.tryFindInSourceFile(sourceFile);
188+
if (moduleCommentRange) {
189+
const moduleComment: string = sourceFile.text.substring(
190+
moduleCommentRange.pos,
191+
moduleCommentRange.end
192+
);
193+
writer.writeLine(moduleComment);
194+
}
195+
183196
if (entity.shouldInlineExport) {
184197
writer.write('export ');
185198
}
@@ -265,6 +278,12 @@ export class DtsRollupGenerator {
265278
span.modification.skipAll();
266279
}
267280

281+
// If the @module comment seems to be attached to one of the regular API items,
282+
// omit it. It gets emitted with the namespace declaration.
283+
if (span.node.getText().match(/(?:\s|\*)@module(?:\s|\*)/gi)) {
284+
span.modification.skipAll();
285+
}
286+
268287
// For now, we don't transform JSDoc comment nodes at all
269288
recurseChildren = false;
270289
break;

build-tests/api-extractor-scenarios/etc/ambientNameConflict/api-extractor-scenarios.api.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,10 @@
138138
"tagName": "@internalRemarks",
139139
"syntaxKind": "block"
140140
},
141+
{
142+
"tagName": "@module",
143+
"syntaxKind": "modifier"
144+
},
141145
{
142146
"tagName": "@preapproved",
143147
"syntaxKind": "modifier"
@@ -171,6 +175,7 @@
171175
"@virtual": true,
172176
"@betaDocumentation": true,
173177
"@internalRemarks": true,
178+
"@module": true,
174179
"@preapproved": true
175180
},
176181
"reportUnsupportedHtmlElements": false

build-tests/api-extractor-scenarios/etc/ambientNameConflict2/api-extractor-scenarios.api.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,10 @@
138138
"tagName": "@internalRemarks",
139139
"syntaxKind": "block"
140140
},
141+
{
142+
"tagName": "@module",
143+
"syntaxKind": "modifier"
144+
},
141145
{
142146
"tagName": "@preapproved",
143147
"syntaxKind": "modifier"
@@ -171,6 +175,7 @@
171175
"@virtual": true,
172176
"@betaDocumentation": true,
173177
"@internalRemarks": true,
178+
"@module": true,
174179
"@preapproved": true
175180
},
176181
"reportUnsupportedHtmlElements": false

build-tests/api-extractor-scenarios/etc/ancillaryDeclarations/api-extractor-scenarios.api.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,10 @@
138138
"tagName": "@internalRemarks",
139139
"syntaxKind": "block"
140140
},
141+
{
142+
"tagName": "@module",
143+
"syntaxKind": "modifier"
144+
},
141145
{
142146
"tagName": "@preapproved",
143147
"syntaxKind": "modifier"
@@ -171,6 +175,7 @@
171175
"@virtual": true,
172176
"@betaDocumentation": true,
173177
"@internalRemarks": true,
178+
"@module": true,
174179
"@preapproved": true
175180
},
176181
"reportUnsupportedHtmlElements": false

build-tests/api-extractor-scenarios/etc/apiItemKinds/api-extractor-scenarios.api.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,10 @@
138138
"tagName": "@internalRemarks",
139139
"syntaxKind": "block"
140140
},
141+
{
142+
"tagName": "@module",
143+
"syntaxKind": "modifier"
144+
},
141145
{
142146
"tagName": "@preapproved",
143147
"syntaxKind": "modifier"
@@ -171,6 +175,7 @@
171175
"@virtual": true,
172176
"@betaDocumentation": true,
173177
"@internalRemarks": true,
178+
"@module": true,
174179
"@preapproved": true
175180
},
176181
"reportUnsupportedHtmlElements": false

build-tests/api-extractor-scenarios/etc/bundledPackages/api-extractor-scenarios.api.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,10 @@
138138
"tagName": "@internalRemarks",
139139
"syntaxKind": "block"
140140
},
141+
{
142+
"tagName": "@module",
143+
"syntaxKind": "modifier"
144+
},
141145
{
142146
"tagName": "@preapproved",
143147
"syntaxKind": "modifier"
@@ -171,6 +175,7 @@
171175
"@virtual": true,
172176
"@betaDocumentation": true,
173177
"@internalRemarks": true,
178+
"@module": true,
174179
"@preapproved": true
175180
},
176181
"reportUnsupportedHtmlElements": false

build-tests/api-extractor-scenarios/etc/bundlerModuleResolution/api-extractor-scenarios.api.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,10 @@
138138
"tagName": "@internalRemarks",
139139
"syntaxKind": "block"
140140
},
141+
{
142+
"tagName": "@module",
143+
"syntaxKind": "modifier"
144+
},
141145
{
142146
"tagName": "@preapproved",
143147
"syntaxKind": "modifier"
@@ -171,6 +175,7 @@
171175
"@virtual": true,
172176
"@betaDocumentation": true,
173177
"@internalRemarks": true,
178+
"@module": true,
174179
"@preapproved": true
175180
},
176181
"reportUnsupportedHtmlElements": false

build-tests/api-extractor-scenarios/etc/circularImport/api-extractor-scenarios.api.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,10 @@
138138
"tagName": "@internalRemarks",
139139
"syntaxKind": "block"
140140
},
141+
{
142+
"tagName": "@module",
143+
"syntaxKind": "modifier"
144+
},
141145
{
142146
"tagName": "@preapproved",
143147
"syntaxKind": "modifier"
@@ -171,6 +175,7 @@
171175
"@virtual": true,
172176
"@betaDocumentation": true,
173177
"@internalRemarks": true,
178+
"@module": true,
174179
"@preapproved": true
175180
},
176181
"reportUnsupportedHtmlElements": false

0 commit comments

Comments
 (0)