Skip to content

Commit

Permalink
Add support for "module" comments on global files
Browse files Browse the repository at this point in the history
Resolves #2165
  • Loading branch information
Gerrit0 committed Apr 21, 2023
1 parent 7e2871d commit e3d7658
Show file tree
Hide file tree
Showing 7 changed files with 81 additions and 3 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Unreleased

### Features

- Added support for discovering a "module" comment on global files, #2165.

### Bug Fixes

- Even more contrast fixes, #2248.
Expand Down
23 changes: 23 additions & 0 deletions src/lib/converter/comments/discovery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,29 @@ export interface DiscoveredComment {
jsDoc: ts.JSDoc | undefined;
}

export function discoverFileComment(
node: ts.SourceFile,
commentStyle: CommentStyle
) {
const text = node.text;

const comments = collectCommentRanges(
ts.getLeadingCommentRanges(text, node.pos)
);

const selectedDocComment = comments.find((ranges) =>
permittedRange(text, ranges, commentStyle)
);

if (selectedDocComment) {
return {
file: node,
ranges: selectedDocComment,
jsDoc: findJsDocForComment(node, selectedDocComment),
};
}
}

export function discoverComment(
symbol: ts.Symbol,
kind: ReflectionKind,
Expand Down
17 changes: 17 additions & 0 deletions src/lib/converter/comments/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { lexBlockComment } from "./blockLexer";
import {
DiscoveredComment,
discoverComment,
discoverFileComment,
discoverSignatureComment,
} from "./discovery";
import { lexLineComments } from "./lineLexer";
Expand Down Expand Up @@ -162,6 +163,22 @@ export function getComment(
return comment;
}

export function getFileComment(
file: ts.SourceFile,
config: CommentParserConfig,
logger: Logger,
commentStyle: CommentStyle,
checker: ts.TypeChecker | undefined
): Comment | undefined {
return getCommentImpl(
discoverFileComment(file, commentStyle),
config,
logger,
/* moduleComment */ true,
checker
);
}

function getConstructorParamPropertyComment(
symbol: ts.Symbol,
config: CommentParserConfig,
Expand Down
17 changes: 16 additions & 1 deletion src/lib/converter/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,12 @@ import type { Converter } from "./converter";
import { isNamedNode } from "./utils/nodes";
import { ConverterEvents } from "./converter-events";
import { resolveAliasedSymbol } from "./utils/symbols";
import { getComment, getJsDocComment, getSignatureComment } from "./comments";
import {
getComment,
getFileComment,
getJsDocComment,
getSignatureComment,
} from "./comments";
import { getHumanName } from "../utils/tsutils";

/**
Expand Down Expand Up @@ -265,6 +270,16 @@ export class Context {
);
}

getFileComment(node: ts.SourceFile) {
return getFileComment(
node,
this.converter.config,
this.logger,
this.converter.commentStyle,
this.converter.useTsLinkResolution ? this.checker : undefined
);
}

getJsDocComment(
declaration:
| ts.JSDocPropertyLikeTag
Expand Down
9 changes: 7 additions & 2 deletions src/lib/converter/converter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -368,8 +368,9 @@ export class Converter extends ChildableComponent<
// Special case for when we're giving a single entry point, we don't need to
// create modules for each entry. Register the project as this module.
context.project.registerReflection(context.project, symbol);
context.project.comment =
symbol && context.getComment(symbol, context.project.kind);
context.project.comment = symbol
? context.getComment(symbol, context.project.kind)
: context.getFileComment(node);
context.trigger(
Converter.EVENT_CREATE_DECLARATION,
context.project
Expand All @@ -383,6 +384,10 @@ export class Converter extends ChildableComponent<
entryName
);

if (!reflection.comment && !symbol) {
reflection.comment = context.getFileComment(node);
}

if (entryPoint.readmeFile) {
const readme = readFile(entryPoint.readmeFile);
const comment = this.parseRawComment(
Expand Down
6 changes: 6 additions & 0 deletions src/test/converter2/issues/gh2165.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
/**
* 'module' comment
* @module
*/

var globalFile = true;
8 changes: 8 additions & 0 deletions src/test/issues.c2.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1008,6 +1008,14 @@ describe("Issue Tests", () => {
);
});

it("#2165 module comments on global files", () => {
const project = convert();
equal(
Comment.combineDisplayParts(project.comment?.summary),
"'module' comment"
);
});

it("#2175", () => {
const project = convert();
const def = query(project, "default");
Expand Down

0 comments on commit e3d7658

Please sign in to comment.