UNPKG

2.52 kBTypeScriptView Raw
1/**
2 * @license
3 * Copyright Google LLC All Rights Reserved.
4 *
5 * Use of this source code is governed by an MIT-style license that can be
6 * found in the LICENSE file at https://angular.io/license
7 */
8/// <amd-module name="@angular/core/schematics/utils/import_manager" />
9import ts from 'typescript';
10/** Update recorder for managing imports. */
11export interface ImportManagerUpdateRecorder {
12 addNewImport(start: number, importText: string): void;
13 updateExistingImport(namedBindings: ts.NamedImports, newNamedBindings: string): void;
14}
15/**
16 * Import manager that can be used to add TypeScript imports to given source
17 * files. The manager ensures that multiple transformations are applied properly
18 * without shifted offsets and that similar existing import declarations are re-used.
19 */
20export declare class ImportManager {
21 private getUpdateRecorder;
22 private printer;
23 /** Map of import declarations that need to be updated to include the given symbols. */
24 private updatedImports;
25 /** Map of source-files and their previously used identifier names. */
26 private usedIdentifierNames;
27 /**
28 * Array of previously resolved symbol imports. Cache can be re-used to return
29 * the same identifier without checking the source-file again.
30 */
31 private importCache;
32 constructor(getUpdateRecorder: (sf: ts.SourceFile) => ImportManagerUpdateRecorder, printer: ts.Printer);
33 /**
34 * Adds an import to the given source-file and returns the TypeScript
35 * identifier that can be used to access the newly imported symbol.
36 */
37 addImportToSourceFile(sourceFile: ts.SourceFile, symbolName: string | null, moduleName: string, typeImport?: boolean): ts.Expression;
38 /**
39 * Stores the collected import changes within the appropriate update recorders. The
40 * updated imports can only be updated *once* per source-file because previous updates
41 * could otherwise shift the source-file offsets.
42 */
43 recordChanges(): void;
44 /** Gets an unique identifier with a base name for the given source file. */
45 private _getUniqueIdentifier;
46 /**
47 * Checks whether the specified identifier name is used within the given
48 * source file.
49 */
50 private isUniqueIdentifierName;
51 private _recordUsedIdentifier;
52 /**
53 * Determines the full end of a given node. By default the end position of a node is
54 * before all trailing comments. This could mean that generated imports shift comments.
55 */
56 private _getEndPositionOfNode;
57}