|
| 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 | + |
| 6 | +import { AstModule, AstModuleExportInfo } from './AstModule'; |
| 7 | +import { AstSyntheticEntity } from './AstEntity'; |
| 8 | +import { Collector } from '../collector/Collector'; |
| 9 | + |
| 10 | +export interface IAstNamespaceImportOptions { |
| 11 | + readonly astModule: AstModule; |
| 12 | + readonly namespaceName: string; |
| 13 | + readonly declaration: ts.Declaration; |
| 14 | +} |
| 15 | + |
| 16 | +/** |
| 17 | + * `AstNamespaceImport` represents a namespace that is created implicitly by a statement |
| 18 | + * such as `import * as example from "./file";` |
| 19 | + * |
| 20 | + * @remarks |
| 21 | + * |
| 22 | + * A typical input looks like this: |
| 23 | + * ```ts |
| 24 | + * // Suppose that example.ts exports two functions f1() and f2(). |
| 25 | + * import * as example from "./file"; |
| 26 | + * export { example }; |
| 27 | + * ``` |
| 28 | + * |
| 29 | + * API Extractor's .d.ts rollup will transform it into an explicit namespace, like this: |
| 30 | + * ```ts |
| 31 | + * declare f1(): void; |
| 32 | + * declare f2(): void; |
| 33 | + * |
| 34 | + * declare namespace example { |
| 35 | + * export { |
| 36 | + * f1, |
| 37 | + * f2 |
| 38 | + * } |
| 39 | + * } |
| 40 | + * ``` |
| 41 | + * |
| 42 | + * The current implementation does not attempt to relocate f1()/f2() to be inside the `namespace` |
| 43 | + * because other type signatures may reference them directly (without using the namespace qualifier). |
| 44 | + * The `declare namespace example` is a synthetic construct represented by `AstNamespaceImport`. |
| 45 | + */ |
| 46 | +export class AstNamespaceImport extends AstSyntheticEntity { |
| 47 | + /** |
| 48 | + * Returns true if the AstSymbolTable.analyze() was called for this object. |
| 49 | + * See that function for details. |
| 50 | + */ |
| 51 | + public analyzed: boolean = false; |
| 52 | + |
| 53 | + /** |
| 54 | + * For example, if the original statement was `import * as example from "./file";` |
| 55 | + * then `astModule` refers to the `./file.d.ts` file. |
| 56 | + */ |
| 57 | + public readonly astModule: AstModule; |
| 58 | + |
| 59 | + /** |
| 60 | + * For example, if the original statement was `import * as example from "./file";` |
| 61 | + * then `namespaceName` would be `example`. |
| 62 | + */ |
| 63 | + public readonly namespaceName: string; |
| 64 | + |
| 65 | + /** |
| 66 | + * The original `ts.SyntaxKind.NamespaceImport` which can be used as a location for error messages. |
| 67 | + */ |
| 68 | + public readonly declaration: ts.Declaration; |
| 69 | + |
| 70 | + public constructor(options: IAstNamespaceImportOptions) { |
| 71 | + super(); |
| 72 | + this.astModule = options.astModule; |
| 73 | + this.namespaceName = options.namespaceName; |
| 74 | + this.declaration = options.declaration; |
| 75 | + } |
| 76 | + |
| 77 | + /** {@inheritdoc} */ |
| 78 | + public get localName(): string { |
| 79 | + // abstract |
| 80 | + return this.namespaceName; |
| 81 | + } |
| 82 | + |
| 83 | + public fetchAstModuleExportInfo(collector: Collector): AstModuleExportInfo { |
| 84 | + const astModuleExportInfo: AstModuleExportInfo = collector.astSymbolTable.fetchAstModuleExportInfo( |
| 85 | + this.astModule |
| 86 | + ); |
| 87 | + return astModuleExportInfo; |
| 88 | + } |
| 89 | +} |
0 commit comments