|
| 1 | +// Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license. |
| 2 | +// See LICENSE in the project root for license information. |
| 3 | + |
| 4 | +import * as ts from 'typescript'; |
| 5 | +import { AstSymbol } from './AstSymbol'; |
| 6 | +import { Span } from '../../utils/Span'; |
| 7 | + |
| 8 | +/** |
| 9 | + * Constructor parameters for AstDeclaration |
| 10 | + */ |
| 11 | +export interface IAstDeclarationParameters { |
| 12 | + readonly declaration: ts.Declaration; |
| 13 | + readonly astSymbol: AstSymbol; |
| 14 | + readonly parent: AstDeclaration | undefined; |
| 15 | +} |
| 16 | + |
| 17 | +/** |
| 18 | + * The AstDeclaration and AstSymbol classes are API Extractor's equivalent of the compiler's |
| 19 | + * ts.Declaration and ts.Symbol objects. They are created by the SymbolTable class. |
| 20 | + * |
| 21 | + * @remarks |
| 22 | + * The AstDeclaration represents one or more syntax components of a symbol. Usually there is |
| 23 | + * only one AstDeclaration per AstSymbol, but certain TypeScript constructs can have multiple |
| 24 | + * declarations (e.g. overloaded functions, declaration merging, etc). |
| 25 | + * |
| 26 | + * Because of this the AstDeclaration manages the parent/child nesting hierarchy (e.g. with |
| 27 | + * declaration merging, each declaration has its own children) and becomes the main focus |
| 28 | + * of analyzing AEDoc and emitting *.d.ts files. |
| 29 | + * |
| 30 | + * The AstDeclarations correspond to items from the compiler's ts.Node hierarchy, but |
| 31 | + * omitting/skipping any nodes that don't match the SymbolAnalyzer.isAstDeclaration() |
| 32 | + * criteria. This simplification makes the other API Extractor stages easier to implement. |
| 33 | + */ |
| 34 | +export class AstDeclaration { |
| 35 | + public readonly declaration: ts.Declaration; |
| 36 | + |
| 37 | + public readonly astSymbol: AstSymbol; |
| 38 | + |
| 39 | + /** |
| 40 | + * The parent, if this object is nested inside another AstDeclaration. |
| 41 | + */ |
| 42 | + public readonly parent: AstDeclaration | undefined; |
| 43 | + |
| 44 | + private readonly _analyzedChildren: AstDeclaration[] = []; |
| 45 | + |
| 46 | + private readonly _analyzedReferencedAstSymbolsSet: Set<AstSymbol> = new Set<AstSymbol>(); |
| 47 | + |
| 48 | + public constructor(parameters: IAstDeclarationParameters) { |
| 49 | + this.declaration = parameters.declaration; |
| 50 | + this.astSymbol = parameters.astSymbol; |
| 51 | + this.parent = parameters.parent; |
| 52 | + |
| 53 | + this.astSymbol._notifyDeclarationAttach(this); |
| 54 | + |
| 55 | + if (this.parent) { |
| 56 | + this.parent._notifyChildAttach(this); |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + /** |
| 61 | + * Returns the children for this AstDeclaration. |
| 62 | + * @remarks |
| 63 | + * The collection will be empty until AstSymbol.analyzed is true. |
| 64 | + */ |
| 65 | + public get children(): ReadonlyArray<AstDeclaration> { |
| 66 | + return this.astSymbol.analyzed ? this._analyzedChildren : []; |
| 67 | + } |
| 68 | + |
| 69 | + /** |
| 70 | + * Returns the AstSymbols referenced by this node. |
| 71 | + * @remarks |
| 72 | + * NOTE: The collection will be empty until AstSymbol.analyzed is true. |
| 73 | + * |
| 74 | + * Since we assume references are always collected by a traversal starting at the |
| 75 | + * root of the nesting declarations, this array omits the following items because they |
| 76 | + * would be redundant: |
| 77 | + * - symbols corresponding to parents of this declaration (e.g. a method that returns its own class) |
| 78 | + * - symbols already listed in the referencedAstSymbols property for parents of this declaration |
| 79 | + * (e.g. a method that returns its own class's base class) |
| 80 | + * - symbols that are referenced only by nested children of this declaration |
| 81 | + * (e.g. if a method returns an enum, this doesn't imply that the method's class references that enum) |
| 82 | + */ |
| 83 | + public get referencedAstSymbols(): ReadonlyArray<AstSymbol> { |
| 84 | + return this.astSymbol.analyzed ? [...this._analyzedReferencedAstSymbolsSet] : []; |
| 85 | + } |
| 86 | + |
| 87 | + /** |
| 88 | + * This is an internal callback used when the SymbolTable attaches a new |
| 89 | + * child AstDeclaration to this object. |
| 90 | + * @internal |
| 91 | + */ |
| 92 | + public _notifyChildAttach(child: AstDeclaration): void { |
| 93 | + if (child.parent !== this) { |
| 94 | + throw new Error('Program Bug: Invalid call to notifyChildAttach()'); |
| 95 | + } |
| 96 | + |
| 97 | + this._analyzedChildren.push(child); |
| 98 | + } |
| 99 | + |
| 100 | + /** |
| 101 | + * Returns a diagnostic dump of the tree, which reports the hierarchy of |
| 102 | + * AstDefinition objects. |
| 103 | + */ |
| 104 | + public getDump(indent: string = ''): string { |
| 105 | + const declarationKind: string = ts.SyntaxKind[this.declaration.kind]; |
| 106 | + let result: string = indent + `+ ${this.astSymbol.localName} (${declarationKind})`; |
| 107 | + if (this.astSymbol.nominal) { |
| 108 | + result += ' (nominal)'; |
| 109 | + } |
| 110 | + result += '\n'; |
| 111 | + |
| 112 | + for (const referencedAstSymbol of this._analyzedReferencedAstSymbolsSet.values()) { |
| 113 | + result += indent + ` ref: ${referencedAstSymbol.localName}\n`; |
| 114 | + } |
| 115 | + |
| 116 | + for (const child of this.children) { |
| 117 | + result += child.getDump(indent + ' '); |
| 118 | + } |
| 119 | + |
| 120 | + return result; |
| 121 | + } |
| 122 | + |
| 123 | + /** |
| 124 | + * Returns a diagnostic dump using Span.getDump(), which reports the detailed |
| 125 | + * compiler structure. |
| 126 | + */ |
| 127 | + public getSpanDump(indent: string = ''): string { |
| 128 | + const span: Span = new Span(this.declaration); |
| 129 | + return span.getDump(indent); |
| 130 | + } |
| 131 | + |
| 132 | + /** |
| 133 | + * This is an internal callback used when SymbolTable.analyze() discovers a new |
| 134 | + * type reference associated with this declaration. |
| 135 | + * @internal |
| 136 | + */ |
| 137 | + public _notifyReferencedAstSymbol(referencedAstSymbol: AstSymbol): void { |
| 138 | + if (this.astSymbol.analyzed) { |
| 139 | + throw new Error('Program Bug: notifyReferencedAstSymbol() called after analysis is already complete'); |
| 140 | + } |
| 141 | + |
| 142 | + for (let current: AstDeclaration | undefined = this; current; current = current.parent) { |
| 143 | + // Don't add references to symbols that are already referenced by a parent |
| 144 | + if (current._analyzedReferencedAstSymbolsSet.has(referencedAstSymbol)) { |
| 145 | + return; |
| 146 | + } |
| 147 | + // Don't add the symbols of parents either |
| 148 | + if (referencedAstSymbol === current.astSymbol) { |
| 149 | + return; |
| 150 | + } |
| 151 | + } |
| 152 | + |
| 153 | + this._analyzedReferencedAstSymbolsSet.add(referencedAstSymbol); |
| 154 | + } |
| 155 | + |
| 156 | + /** |
| 157 | + * Visits all the current declaration and all children recursively in a depth-first traversal, |
| 158 | + * and performs the specified action for each one. |
| 159 | + */ |
| 160 | + public forEachDeclarationRecursive(action: (astDeclaration: AstDeclaration) => void): void { |
| 161 | + action(this); |
| 162 | + for (const child of this.children) { |
| 163 | + child.forEachDeclarationRecursive(action); |
| 164 | + } |
| 165 | + } |
| 166 | +} |
0 commit comments