2018-11-13 14:40:54 +00:00
|
|
|
/**
|
|
|
|
|
* @license
|
|
|
|
|
* Copyright Google Inc. 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 * as ts from 'typescript';
|
2019-03-20 13:47:58 +00:00
|
|
|
import {ReferencesRegistry} from '../../../src/ngtsc/annotations';
|
|
|
|
|
import {Reference} from '../../../src/ngtsc/imports';
|
|
|
|
|
import {Declaration, ReflectionHost} from '../../../src/ngtsc/reflection';
|
2018-11-13 14:40:54 +00:00
|
|
|
import {hasNameIdentifier} from '../utils';
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* This is a place for DecoratorHandlers to register references that they
|
|
|
|
|
* find in their analysis of the code.
|
|
|
|
|
*
|
|
|
|
|
* This registry is used to ensure that these references are publicly exported
|
|
|
|
|
* from libraries that are compiled by ngcc.
|
|
|
|
|
*/
|
|
|
|
|
export class NgccReferencesRegistry implements ReferencesRegistry {
|
|
|
|
|
private map = new Map<ts.Identifier, Declaration>();
|
|
|
|
|
|
|
|
|
|
constructor(private host: ReflectionHost) {}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Register one or more references in the registry.
|
|
|
|
|
* Only `ResolveReference` references are stored. Other types are ignored.
|
|
|
|
|
* @param references A collection of references to register.
|
|
|
|
|
*/
|
2018-12-13 11:52:20 -08:00
|
|
|
add(source: ts.Declaration, ...references: Reference<ts.Declaration>[]): void {
|
2018-11-13 14:40:54 +00:00
|
|
|
references.forEach(ref => {
|
feat(ivy): use fileNameToModuleName to emit imports when it's available (#28523)
The ultimate goal of this commit is to make use of fileNameToModuleName to
get the module specifier to use when generating an import, when that API is
available in the CompilerHost that ngtsc is created with.
As part of getting there, the way in which ngtsc tracks references and
generates import module specifiers is refactored considerably. References
are tracked with the Reference class, and previously ngtsc had several
different kinds of Reference. An AbsoluteReference represented a declaration
which needed to be imported via an absolute module specifier tracked in the
AbsoluteReference, and a RelativeReference represented a declaration from
the local program, imported via relative path or referred to directly by
identifier if possible. Thus, how to refer to a particular declaration was
encoded into the Reference type _at the time of creation of the Reference_.
This commit refactors that logic and reduces Reference to a single class
with no subclasses. A Reference represents a node being referenced, plus
context about how the node was located. This context includes a
"bestGuessOwningModule", the compiler's best guess at which absolute
module specifier has defined this reference. For example, if the compiler
arrives at the declaration of CommonModule via an import to @angular/common,
then any references obtained from CommonModule (e.g. NgIf) will also be
considered to be owned by @angular/common.
A ReferenceEmitter class and accompanying ReferenceEmitStrategy interface
are introduced. To produce an Expression referring to a given Reference'd
node, the ReferenceEmitter consults a sequence of ReferenceEmitStrategy
implementations.
Several different strategies are defined:
- LocalIdentifierStrategy: use local ts.Identifiers if available.
- AbsoluteModuleStrategy: if the Reference has a bestGuessOwningModule,
import the node via an absolute import from that module specifier.
- LogicalProjectStrategy: if the Reference is in the logical project
(is under the project rootDirs), import the node via a relative import.
- FileToModuleStrategy: use a FileToModuleHost to generate the module
specifier by which to import the node.
Depending on the availability of fileNameToModuleName in the CompilerHost,
then, a different collection of these strategies is used for compilation.
PR Close #28523
2019-02-01 17:24:21 -08:00
|
|
|
// Only store relative references. We are not interested in literals.
|
|
|
|
|
if (ref.bestGuessOwningModule === null && hasNameIdentifier(ref.node)) {
|
2018-11-13 14:40:54 +00:00
|
|
|
const declaration = this.host.getDeclarationOfIdentifier(ref.node.name);
|
|
|
|
|
if (declaration && hasNameIdentifier(declaration.node)) {
|
|
|
|
|
this.map.set(declaration.node.name, declaration);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Create and return a mapping for the registered resolved references.
|
|
|
|
|
* @returns A map of reference identifiers to reference declarations.
|
|
|
|
|
*/
|
|
|
|
|
getDeclarationMap(): Map<ts.Identifier, Declaration> { return this.map; }
|
|
|
|
|
}
|