-
Notifications
You must be signed in to change notification settings - Fork 25k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(compiler-cli): introduce new implementation of `ImportManage…
…r` (#54983) This commit introduces a new implementation of `ImportManager` that has numerous benefits: - It allows efficient re-use of original source file imports. * either fully re-using original imports if matching * updating existing import declarations to include new symbols. - It allows efficient re-use of previous generated imports. - The manager can be used for schematics and migrations. The implementation is a rework of the import manager that we originally built for schematics in Angular Material, but this commit improved it to be more flexible, more readable, and "correct". In follow-ups we can use this for schematics/migrations. PR Close #54983
- Loading branch information
1 parent
365fd50
commit 3253576
Showing
13 changed files
with
1,464 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
50 changes: 50 additions & 0 deletions
50
...ages/compiler-cli/src/ngtsc/translator/src/import_manager/check_unique_identifier_name.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
/** | ||
* @license | ||
* Copyright Google LLC All Rights Reserved. | ||
* | ||
* Use of this source code is governed by an MIT-style license that can be | ||
* found in the LICENSE file at https://angular.io/license | ||
*/ | ||
|
||
import ts from 'typescript'; | ||
|
||
import type {ImportManagerConfig} from './import_manager'; | ||
|
||
/** Extension of `ts.SourceFile` with metadata fields that are marked as internal. */ | ||
interface SourceFileWithIdentifiers extends ts.SourceFile { | ||
/** List of all identifiers encountered while parsing the source file. */ | ||
identifiers?: Map<string, string>; | ||
} | ||
|
||
/** | ||
* Generates a helper for `ImportManagerConfig` to generate unique identifiers | ||
* for a given source file. | ||
*/ | ||
export function createGenerateUniqueIdentifierHelper(): | ||
ImportManagerConfig['generateUniqueIdentifier'] { | ||
const generatedIdentifiers = new Set<string>(); | ||
|
||
return (sourceFile: ts.SourceFile, symbolName: string) => { | ||
const sf = sourceFile as SourceFileWithIdentifiers; | ||
if (sf.identifiers === undefined) { | ||
throw new Error('Source file unexpectedly lacks map of parsed `identifiers`.'); | ||
} | ||
|
||
const isUniqueIdentifier = (name: string) => | ||
!sf.identifiers!.has(name) && !generatedIdentifiers.has(name); | ||
|
||
if (isUniqueIdentifier(symbolName)) { | ||
generatedIdentifiers.add(symbolName); | ||
return null; | ||
} | ||
|
||
let name = null; | ||
let counter = 1; | ||
do { | ||
name = `${symbolName}_${counter++}`; | ||
} while (!isUniqueIdentifier(name)); | ||
|
||
generatedIdentifiers.add(name); | ||
return ts.factory.createUniqueName(name, ts.GeneratedIdentifierFlags.Optimistic); | ||
}; | ||
} |
Oops, something went wrong.