UNPKG

6.71 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.CollectorEntity = void 0;
29const ts = __importStar(require("typescript"));
30const AstSymbol_1 = require("../analyzer/AstSymbol");
31const Collector_1 = require("./Collector");
32const node_core_library_1 = require("@rushstack/node-core-library");
33/**
34 * This is a data structure used by the Collector to track an AstEntity that may be emitted in the *.d.ts file.
35 *
36 * @remarks
37 * The additional contextual state beyond AstSymbol is:
38 * - Whether it's an export of this entry point or not
39 * - The nameForEmit, which may get renamed by DtsRollupGenerator._makeUniqueNames()
40 * - The export name (or names, if the same symbol is exported multiple times)
41 */
42class CollectorEntity {
43 constructor(astEntity) {
44 this._exportNames = new Set();
45 this._exportNamesSorted = false;
46 this._singleExportName = undefined;
47 this._nameForEmit = undefined;
48 this._sortKey = undefined;
49 this._astNamespaceImports = new Set();
50 this.astEntity = astEntity;
51 }
52 /**
53 * The declaration name that will be emitted in a .d.ts rollup. For non-exported declarations,
54 * Collector._makeUniqueNames() may need to rename the declaration to avoid conflicts with other declarations
55 * in that module.
56 */
57 get nameForEmit() {
58 return this._nameForEmit;
59 }
60 set nameForEmit(value) {
61 this._nameForEmit = value;
62 this._sortKey = undefined; // invalidate the cached value
63 }
64 /**
65 * If this symbol is exported from the entry point, the list of export names.
66 *
67 * @remarks
68 * Note that a given symbol may be exported more than once:
69 * ```
70 * class X { }
71 * export { X }
72 * export { X as Y }
73 * ```
74 */
75 get exportNames() {
76 if (!this._exportNamesSorted) {
77 node_core_library_1.Sort.sortSet(this._exportNames);
78 this._exportNamesSorted = true;
79 }
80 return this._exportNames;
81 }
82 /**
83 * If exportNames contains only one string, then singleExportName is that string.
84 * In all other cases, it is undefined.
85 */
86 get singleExportName() {
87 return this._singleExportName;
88 }
89 /**
90 * This is true if exportNames contains only one string, and the declaration can be exported using the inline syntax
91 * such as "export class X { }" instead of "export { X }".
92 */
93 get shouldInlineExport() {
94 // We don't inline an AstImport
95 if (this.astEntity instanceof AstSymbol_1.AstSymbol) {
96 // We don't inline a symbol with more than one exported name
97 if (this._singleExportName !== undefined && this._singleExportName !== ts.InternalSymbolName.Default) {
98 // We can't inline a symbol whose emitted name is different from the export name
99 if (this._nameForEmit === undefined || this._nameForEmit === this._singleExportName) {
100 return true;
101 }
102 }
103 }
104 return false;
105 }
106 /**
107 * Returns true if this symbol is an export for the entry point being analyzed.
108 */
109 get exported() {
110 return this.exportNames.size > 0;
111 }
112 /**
113 * Indicates that it is possible for a consumer of the API to access this declaration, either by importing
114 * it directly, or via some other alias such as a member of a namespace. If a collector entity is not consumable,
115 * then API Extractor will report a ExtractorMessageId.ForgottenExport warning.
116 *
117 * @remarks
118 * Generally speaking, an API item is consumable if:
119 *
120 * - The collector encounters it while crawling the entry point, and it is a root symbol
121 * (i.e. there is a corresponding a CollectorEntity)
122 *
123 * - AND it is exported by the entry point
124 *
125 * However a special case occurs with `AstNamespaceImport` which produces a rollup like this:
126 *
127 * ```ts
128 * declare interface IForgottenExport { }
129 *
130 * declare function member(): IForgottenExport;
131 *
132 * declare namespace ns {
133 * export {
134 * member
135 * }
136 * }
137 * export { ns }
138 * ```
139 *
140 * In this example, `IForgottenExport` is not consumable. Whereas `member()` is consumable as `ns.member()`
141 * even though `member()` itself is not exported.
142 */
143 get consumable() {
144 return this.exported || this._astNamespaceImports.size > 0;
145 }
146 /**
147 * Associates this entity with a `AstNamespaceImport`.
148 */
149 addAstNamespaceImports(astNamespaceImport) {
150 this._astNamespaceImports.add(astNamespaceImport);
151 }
152 /**
153 * Adds a new exportName to the exportNames set.
154 */
155 addExportName(exportName) {
156 if (!this._exportNames.has(exportName)) {
157 this._exportNamesSorted = false;
158 this._exportNames.add(exportName);
159 if (this._exportNames.size === 1) {
160 this._singleExportName = exportName;
161 }
162 else {
163 this._singleExportName = undefined;
164 }
165 }
166 }
167 /**
168 * A sorting key used by DtsRollupGenerator._makeUniqueNames()
169 */
170 getSortKey() {
171 if (!this._sortKey) {
172 this._sortKey = Collector_1.Collector.getSortKeyIgnoringUnderscore(this.nameForEmit || this.astEntity.localName);
173 }
174 return this._sortKey;
175 }
176}
177exports.CollectorEntity = CollectorEntity;
178//# sourceMappingURL=CollectorEntity.js.map
\No newline at end of file