1 | ;
|
2 | // Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license.
|
3 | // See LICENSE in the project root for license information.
|
4 | Object.defineProperty(exports, "__esModule", { value: true });
|
5 | exports.AstSymbol = void 0;
|
6 | const node_core_library_1 = require("@rushstack/node-core-library");
|
7 | const AstEntity_1 = require("./AstEntity");
|
8 | /**
|
9 | * The AstDeclaration and AstSymbol classes are API Extractor's equivalent of the compiler's
|
10 | * ts.Declaration and ts.Symbol objects. They are created by the `AstSymbolTable` class.
|
11 | *
|
12 | * @remarks
|
13 | * The AstSymbol represents the ts.Symbol information for an AstDeclaration. For example,
|
14 | * if a method has 3 overloads, each overloaded signature will have its own AstDeclaration,
|
15 | * but they will all share a common AstSymbol.
|
16 | *
|
17 | * For nested definitions, the AstSymbol has a unique parent (i.e. AstSymbol.rootAstSymbol),
|
18 | * but the parent/children for each AstDeclaration may be different. Consider this example:
|
19 | *
|
20 | * ```ts
|
21 | * export namespace N {
|
22 | * export function f(): void { }
|
23 | * }
|
24 | *
|
25 | * export interface N {
|
26 | * g(): void;
|
27 | * }
|
28 | * ```
|
29 | *
|
30 | * Note how the parent/child relationships are different for the symbol tree versus
|
31 | * the declaration tree, and the declaration tree has two roots:
|
32 | *
|
33 | * ```
|
34 | * AstSymbol tree: AstDeclaration tree:
|
35 | * - N - N (namespace)
|
36 | * - f - f
|
37 | * - g - N (interface)
|
38 | * - g
|
39 | * ```
|
40 | */
|
41 | class AstSymbol extends AstEntity_1.AstEntity {
|
42 | constructor(options) {
|
43 | super();
|
44 | // This flag is unused if this is not the root symbol.
|
45 | // Being "analyzed" is a property of the root symbol.
|
46 | this._analyzed = false;
|
47 | this.followedSymbol = options.followedSymbol;
|
48 | this.localName = options.localName;
|
49 | this.isExternal = options.isExternal;
|
50 | this.nominalAnalysis = options.nominalAnalysis;
|
51 | this.parentAstSymbol = options.parentAstSymbol;
|
52 | this.rootAstSymbol = options.rootAstSymbol || this;
|
53 | this._astDeclarations = [];
|
54 | }
|
55 | /**
|
56 | * The one or more declarations for this symbol.
|
57 | * @remarks
|
58 | * For example, if this symbol is a method, then the declarations might be
|
59 | * various method overloads. If this symbol is a namespace, then the declarations
|
60 | * might be separate namespace blocks with the same name that get combined via
|
61 | * declaration merging.
|
62 | */
|
63 | get astDeclarations() {
|
64 | return this._astDeclarations;
|
65 | }
|
66 | /**
|
67 | * Returns true if the AstSymbolTable.analyze() was called for this object.
|
68 | * See that function for details.
|
69 | * @remarks
|
70 | * AstSymbolTable.analyze() is always performed on the root AstSymbol. This function
|
71 | * returns true if-and-only-if the root symbol was analyzed.
|
72 | */
|
73 | get analyzed() {
|
74 | return this.rootAstSymbol._analyzed;
|
75 | }
|
76 | /**
|
77 | * This is an internal callback used when the AstSymbolTable attaches a new
|
78 | * AstDeclaration to this object.
|
79 | * @internal
|
80 | */
|
81 | _notifyDeclarationAttach(astDeclaration) {
|
82 | if (this.analyzed) {
|
83 | throw new node_core_library_1.InternalError('_notifyDeclarationAttach() called after analysis is already complete');
|
84 | }
|
85 | this._astDeclarations.push(astDeclaration);
|
86 | }
|
87 | /**
|
88 | * This is an internal callback used when the AstSymbolTable.analyze()
|
89 | * has processed this object.
|
90 | * @internal
|
91 | */
|
92 | _notifyAnalyzed() {
|
93 | if (this.parentAstSymbol) {
|
94 | throw new node_core_library_1.InternalError('_notifyAnalyzed() called for an AstSymbol which is not the root');
|
95 | }
|
96 | this._analyzed = true;
|
97 | }
|
98 | /**
|
99 | * Helper that calls AstDeclaration.forEachDeclarationRecursive() for each AstDeclaration.
|
100 | */
|
101 | forEachDeclarationRecursive(action) {
|
102 | for (const astDeclaration of this.astDeclarations) {
|
103 | astDeclaration.forEachDeclarationRecursive(action);
|
104 | }
|
105 | }
|
106 | }
|
107 | exports.AstSymbol = AstSymbol;
|
108 | //# sourceMappingURL=AstSymbol.js.map |
\ | No newline at end of file |