UNPKG

4.51 kBJavaScriptView Raw
1"use strict";
2// Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license.
3// See LICENSE in the project root for license information.
4var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
5 if (k2 === undefined) k2 = k;
6 var desc = Object.getOwnPropertyDescriptor(m, k);
7 if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
8 desc = { enumerable: true, get: function() { return m[k]; } };
9 }
10 Object.defineProperty(o, k2, desc);
11}) : (function(o, m, k, k2) {
12 if (k2 === undefined) k2 = k;
13 o[k2] = m[k];
14}));
15var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
16 Object.defineProperty(o, "default", { enumerable: true, value: v });
17}) : function(o, v) {
18 o["default"] = v;
19});
20var __importStar = (this && this.__importStar) || function (mod) {
21 if (mod && mod.__esModule) return mod;
22 var result = {};
23 if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
24 __setModuleDefault(result, mod);
25 return result;
26};
27Object.defineProperty(exports, "__esModule", { value: true });
28exports.PackageDocComment = void 0;
29const ts = __importStar(require("typescript"));
30class PackageDocComment {
31 /**
32 * For the given source file, see if it starts with a TSDoc comment containing the `@packageDocumentation` tag.
33 */
34 static tryFindInSourceFile(sourceFile, collector) {
35 // The @packageDocumentation comment is special because it is not attached to an AST
36 // definition. Instead, it is part of the "trivia" tokens that the compiler treats
37 // as irrelevant white space.
38 //
39 // WARNING: If the comment doesn't precede an export statement, the compiler will omit
40 // it from the *.d.ts file, and API Extractor won't find it. If this happens, you need
41 // to rearrange your statements to ensure it is passed through.
42 //
43 // This implementation assumes that the "@packageDocumentation" will be in the first TSDoc comment
44 // that appears in the entry point *.d.ts file. We could possibly look in other places,
45 // but the above warning suggests enforcing a standardized layout. This design choice is open
46 // to feedback.
47 let packageCommentRange = undefined; // empty string
48 for (const commentRange of ts.getLeadingCommentRanges(sourceFile.text, sourceFile.getFullStart()) || []) {
49 if (commentRange.kind === ts.SyntaxKind.MultiLineCommentTrivia) {
50 const commentBody = sourceFile.text.substring(commentRange.pos, commentRange.end);
51 // Choose the first JSDoc-style comment
52 if (/^\s*\/\*\*/.test(commentBody)) {
53 // But only if it looks like it's trying to be @packageDocumentation
54 // (The TSDoc parser will validate this more rigorously)
55 if (/\@packageDocumentation/i.test(commentBody)) {
56 packageCommentRange = commentRange;
57 }
58 break;
59 }
60 }
61 }
62 if (!packageCommentRange) {
63 // If we didn't find the @packageDocumentation tag in the expected place, is it in some
64 // wrong place? This sanity check helps people to figure out why there comment isn't working.
65 for (const statement of sourceFile.statements) {
66 const ranges = [];
67 ranges.push(...(ts.getLeadingCommentRanges(sourceFile.text, statement.getFullStart()) || []));
68 ranges.push(...(ts.getTrailingCommentRanges(sourceFile.text, statement.getEnd()) || []));
69 for (const commentRange of ranges) {
70 const commentBody = sourceFile.text.substring(commentRange.pos, commentRange.end);
71 if (/\@packageDocumentation/i.test(commentBody)) {
72 collector.messageRouter.addAnalyzerIssueForPosition("ae-misplaced-package-tag" /* MisplacedPackageTag */, 'The @packageDocumentation comment must appear at the top of entry point *.d.ts file', sourceFile, commentRange.pos);
73 break;
74 }
75 }
76 }
77 }
78 return packageCommentRange;
79 }
80}
81exports.PackageDocComment = PackageDocComment;
82//# sourceMappingURL=PackageDocComment.js.map
\No newline at end of file