Files
discord.js/packages/api-extractor/src/aedoc/PackageDocComment.ts
Qjuh 5c0fad3b2d build: package api-extractor and -model (#9920)
* fix(ExceptText): don't display import("d..-types/v10"). in return type

* Squashed 'packages/api-extractor-model/' content from commit 39ecb196c

git-subtree-dir: packages/api-extractor-model
git-subtree-split: 39ecb196ca210bdf84ba6c9cadb1bb93571849d7

* Squashed 'packages/api-extractor/' content from commit 341ad6c51

git-subtree-dir: packages/api-extractor
git-subtree-split: 341ad6c51b01656d4f73b74ad4bdb3095f9262c4

* feat(api-extractor): add api-extractor and -model

* fix: package.json docs script

* fix(SourcLink): use <> instead of function syntax

* fix: make packages private

* fix: rest params showing in docs, added labels

* fix: missed two files

* fix: cpy-cli & pnpm-lock

* fix: increase icon size

* fix: icon size again
2023-11-07 21:53:36 +01:00

71 lines
3.0 KiB
TypeScript

// Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license.
// See LICENSE in the project root for license information.
import * as ts from 'typescript';
import { ExtractorMessageId } from '../api/ExtractorMessageId.js';
import type { Collector } from '../collector/Collector.js';
export class PackageDocComment {
/**
* For the given source file, see if it starts with a TSDoc comment containing the `@packageDocumentation` tag.
*/
public static tryFindInSourceFile(sourceFile: ts.SourceFile, collector: Collector): ts.TextRange | undefined {
// The @packageDocumentation comment is special because it is not attached to an AST
// definition. Instead, it is part of the "trivia" tokens that the compiler treats
// as irrelevant white space.
//
// WARNING: If the comment doesn't precede an export statement, the compiler will omit
// it from the *.d.ts file, and API Extractor won't find it. If this happens, you need
// to rearrange your statements to ensure it is passed through.
//
// This implementation assumes that the "@packageDocumentation" will be in the first TSDoc comment
// that appears in the entry point *.d.ts file. We could possibly look in other places,
// but the above warning suggests enforcing a standardized layout. This design choice is open
// to feedback.
let packageCommentRange: ts.TextRange | undefined; // empty string
for (const commentRange of ts.getLeadingCommentRanges(sourceFile.text, sourceFile.getFullStart()) ?? []) {
if (commentRange.kind === ts.SyntaxKind.MultiLineCommentTrivia) {
const commentBody: string = sourceFile.text.slice(commentRange.pos, commentRange.end);
// Choose the first JSDoc-style comment
if (/^\s*\/\*\*/.test(commentBody)) {
// But only if it looks like it's trying to be @packageDocumentation
// (The TSDoc parser will validate this more rigorously)
if (/@packagedocumentation/i.test(commentBody)) {
packageCommentRange = commentRange;
}
break;
}
}
}
if (!packageCommentRange) {
// If we didn't find the @packageDocumentation tag in the expected place, is it in some
// wrong place? This sanity check helps people to figure out why there comment isn't working.
for (const statement of sourceFile.statements) {
const ranges: ts.CommentRange[] = [];
ranges.push(...(ts.getLeadingCommentRanges(sourceFile.text, statement.getFullStart()) ?? []));
ranges.push(...(ts.getTrailingCommentRanges(sourceFile.text, statement.getEnd()) ?? []));
for (const commentRange of ranges) {
const commentBody: string = sourceFile.text.slice(commentRange.pos, commentRange.end);
if (/@packagedocumentation/i.test(commentBody)) {
collector.messageRouter.addAnalyzerIssueForPosition(
ExtractorMessageId.MisplacedPackageTag,
'The @packageDocumentation comment must appear at the top of entry point *.d.ts file',
sourceFile,
commentRange.pos,
);
break;
}
}
}
}
return packageCommentRange;
}
}