UNPKG

10.9 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.AstDeclaration = void 0;
29const ts = __importStar(require("typescript"));
30const Span_1 = require("./Span");
31const node_core_library_1 = require("@rushstack/node-core-library");
32/**
33 * The AstDeclaration and AstSymbol classes are API Extractor's equivalent of the compiler's
34 * ts.Declaration and ts.Symbol objects. They are created by the `AstSymbolTable` class.
35 *
36 * @remarks
37 * The AstDeclaration represents one or more syntax components of a symbol. Usually there is
38 * only one AstDeclaration per AstSymbol, but certain TypeScript constructs can have multiple
39 * declarations (e.g. overloaded functions, merged declarations, etc.).
40 *
41 * Because of this, the `AstDeclaration` manages the parent/child nesting hierarchy (e.g. with
42 * declaration merging, each declaration has its own children) and becomes the main focus
43 * of analyzing AEDoc and emitting *.d.ts files.
44 *
45 * The AstDeclarations correspond to items from the compiler's ts.Node hierarchy, but
46 * omitting/skipping any nodes that don't match the AstDeclaration.isSupportedSyntaxKind()
47 * criteria. This simplification makes the other API Extractor stages easier to implement.
48 */
49class AstDeclaration {
50 constructor(options) {
51 // NOTE: This array becomes immutable after astSymbol.analyze() sets astSymbol.analyzed=true
52 this._analyzedChildren = [];
53 this._analyzedReferencedAstEntitiesSet = new Set();
54 // Reverse lookup used by findChildrenWithName()
55 this._childrenByName = undefined;
56 this.declaration = options.declaration;
57 this.astSymbol = options.astSymbol;
58 this.parent = options.parent;
59 this.astSymbol._notifyDeclarationAttach(this);
60 if (this.parent) {
61 this.parent._notifyChildAttach(this);
62 }
63 this.modifierFlags = ts.getCombinedModifierFlags(this.declaration);
64 // Check for ECMAScript private fields, for example:
65 //
66 // class Person { #name: string; }
67 //
68 const declarationName = ts.getNameOfDeclaration(this.declaration);
69 if (declarationName) {
70 if (ts.isPrivateIdentifier(declarationName)) {
71 // eslint-disable-next-line no-bitwise
72 this.modifierFlags |= ts.ModifierFlags.Private;
73 }
74 }
75 }
76 /**
77 * Returns the children for this AstDeclaration.
78 * @remarks
79 * The collection will be empty until AstSymbol.analyzed is true.
80 */
81 get children() {
82 return this.astSymbol.analyzed ? this._analyzedChildren : [];
83 }
84 /**
85 * Returns the AstEntity objects referenced by this node.
86 * @remarks
87 * NOTE: The collection will be empty until AstSymbol.analyzed is true.
88 *
89 * Since we assume references are always collected by a traversal starting at the
90 * root of the nesting declarations, this array omits the following items because they
91 * would be redundant:
92 * - symbols corresponding to parents of this declaration (e.g. a method that returns its own class)
93 * - symbols already listed in the referencedAstSymbols property for parents of this declaration
94 * (e.g. a method that returns its own class's base class)
95 * - symbols that are referenced only by nested children of this declaration
96 * (e.g. if a method returns an enum, this doesn't imply that the method's class references that enum)
97 */
98 get referencedAstEntities() {
99 return this.astSymbol.analyzed ? [...this._analyzedReferencedAstEntitiesSet] : [];
100 }
101 /**
102 * This is an internal callback used when the AstSymbolTable attaches a new
103 * child AstDeclaration to this object.
104 * @internal
105 */
106 _notifyChildAttach(child) {
107 if (child.parent !== this) {
108 throw new node_core_library_1.InternalError('Invalid call to notifyChildAttach()');
109 }
110 if (this.astSymbol.analyzed) {
111 throw new node_core_library_1.InternalError('_notifyChildAttach() called after analysis is already complete');
112 }
113 this._analyzedChildren.push(child);
114 }
115 /**
116 * Returns a diagnostic dump of the tree, which reports the hierarchy of
117 * AstDefinition objects.
118 */
119 getDump(indent = '') {
120 const declarationKind = ts.SyntaxKind[this.declaration.kind];
121 let result = indent + `+ ${this.astSymbol.localName} (${declarationKind})`;
122 if (this.astSymbol.nominalAnalysis) {
123 result += ' (nominal)';
124 }
125 result += '\n';
126 for (const referencedAstEntity of this._analyzedReferencedAstEntitiesSet.values()) {
127 result += indent + ` ref: ${referencedAstEntity.localName}\n`;
128 }
129 for (const child of this.children) {
130 result += child.getDump(indent + ' ');
131 }
132 return result;
133 }
134 /**
135 * Returns a diagnostic dump using Span.getDump(), which reports the detailed
136 * compiler structure.
137 */
138 getSpanDump(indent = '') {
139 const span = new Span_1.Span(this.declaration);
140 return span.getDump(indent);
141 }
142 /**
143 * This is an internal callback used when AstSymbolTable.analyze() discovers a new
144 * type reference associated with this declaration.
145 * @internal
146 */
147 _notifyReferencedAstEntity(referencedAstEntity) {
148 if (this.astSymbol.analyzed) {
149 throw new node_core_library_1.InternalError('_notifyReferencedAstEntity() called after analysis is already complete');
150 }
151 for (let current = this; current; current = current.parent) {
152 // Don't add references to symbols that are already referenced by a parent
153 if (current._analyzedReferencedAstEntitiesSet.has(referencedAstEntity)) {
154 return;
155 }
156 // Don't add the symbols of parents either
157 if (referencedAstEntity === current.astSymbol) {
158 return;
159 }
160 }
161 this._analyzedReferencedAstEntitiesSet.add(referencedAstEntity);
162 }
163 /**
164 * Visits all the current declaration and all children recursively in a depth-first traversal,
165 * and performs the specified action for each one.
166 */
167 forEachDeclarationRecursive(action) {
168 action(this);
169 for (const child of this.children) {
170 child.forEachDeclarationRecursive(action);
171 }
172 }
173 /**
174 * Returns the list of child declarations whose `AstSymbol.localName` matches the provided `name`.
175 *
176 * @remarks
177 * This is an efficient O(1) lookup.
178 */
179 findChildrenWithName(name) {
180 // The children property returns:
181 //
182 // return this.astSymbol.analyzed ? this._analyzedChildren : [];
183 //
184 if (!this.astSymbol.analyzed || this._analyzedChildren.length === 0) {
185 return [];
186 }
187 if (this._childrenByName === undefined) {
188 // Build the lookup table
189 const childrenByName = new Map();
190 for (const child of this._analyzedChildren) {
191 const childName = child.astSymbol.localName;
192 let array = childrenByName.get(childName);
193 if (array === undefined) {
194 array = [];
195 childrenByName.set(childName, array);
196 }
197 array.push(child);
198 }
199 this._childrenByName = childrenByName;
200 }
201 return this._childrenByName.get(name) || [];
202 }
203 /**
204 * This function determines which ts.Node kinds will generate an AstDeclaration.
205 * These correspond to the definitions that we can add AEDoc to.
206 */
207 static isSupportedSyntaxKind(kind) {
208 // (alphabetical order)
209 switch (kind) {
210 case ts.SyntaxKind.CallSignature:
211 case ts.SyntaxKind.ClassDeclaration:
212 case ts.SyntaxKind.ConstructSignature: // Example: "new(x: number): IMyClass"
213 case ts.SyntaxKind.Constructor: // Example: "constructor(x: number)"
214 case ts.SyntaxKind.EnumDeclaration:
215 case ts.SyntaxKind.EnumMember:
216 case ts.SyntaxKind.FunctionDeclaration: // Example: "(x: number): number"
217 case ts.SyntaxKind.GetAccessor:
218 case ts.SyntaxKind.SetAccessor:
219 case ts.SyntaxKind.IndexSignature: // Example: "[key: string]: string"
220 case ts.SyntaxKind.InterfaceDeclaration:
221 case ts.SyntaxKind.MethodDeclaration:
222 case ts.SyntaxKind.MethodSignature:
223 case ts.SyntaxKind.ModuleDeclaration: // Used for both "module" and "namespace" declarations
224 case ts.SyntaxKind.PropertyDeclaration:
225 case ts.SyntaxKind.PropertySignature:
226 case ts.SyntaxKind.TypeAliasDeclaration: // Example: "type Shape = Circle | Square"
227 case ts.SyntaxKind.VariableDeclaration:
228 return true;
229 // NOTE: Prior to TypeScript 3.7, in the emitted .d.ts files, the compiler would merge a GetAccessor/SetAccessor
230 // pair into a single PropertyDeclaration.
231 // NOTE: In contexts where a source file is treated as a module, we do create "nominal analysis"
232 // AstSymbol objects corresponding to a ts.SyntaxKind.SourceFile node. However, a source file
233 // is NOT considered a nesting structure, and it does NOT act as a root for the declarations
234 // appearing in the file. This is because the *.d.ts generator is in the business of rolling up
235 // source files, and thus wants to ignore them in general.
236 }
237 return false;
238 }
239}
240exports.AstDeclaration = AstDeclaration;
241//# sourceMappingURL=AstDeclaration.js.map
\No newline at end of file