refactor(ivy): extract selector scope logic to a new ngtsc package (#28852)

This commit splits apart selector_scope.ts in ngtsc and extracts the logic
into two separate classes, the LocalModuleScopeRegistry and the
DtsModuleScopeResolver. The logic is cleaned up significantly and new tests
are added to verify behavior.

LocalModuleScopeRegistry implements the NgModule semantics for compilation
scopes, and handles NgModules declared in the current compilation unit.
DtsModuleScopeResolver implements simpler logic for export scopes and
handles NgModules declared in .d.ts files.

This is done in preparation for the addition of re-export logic to solve
StrictDeps issues.

PR Close #28852
This commit is contained in:
Alex Rickabaugh
2019-02-19 12:05:03 -08:00
committed by Ben Lesh
parent fafabc0b92
commit 15c065f9a0
28 changed files with 1105 additions and 830 deletions

View File

@ -16,6 +16,7 @@ ts_library(
"//packages/compiler-cli/src/ngtsc/partial_evaluator",
"//packages/compiler-cli/src/ngtsc/reflection",
"//packages/compiler-cli/src/ngtsc/routing",
"//packages/compiler-cli/src/ngtsc/scope",
"//packages/compiler-cli/src/ngtsc/transform",
"//packages/compiler-cli/src/ngtsc/typecheck",
"//packages/compiler-cli/src/ngtsc/util",

View File

@ -16,4 +16,3 @@ export {InjectableDecoratorHandler} from './src/injectable';
export {NgModuleDecoratorHandler} from './src/ng_module';
export {PipeDecoratorHandler} from './src/pipe';
export {NoopReferencesRegistry, ReferencesRegistry} from './src/references_registry';
export {CompilationScope, SelectorScopeRegistry} from './src/selector_scope';

View File

@ -12,9 +12,10 @@ import * as ts from 'typescript';
import {CycleAnalyzer} from '../../cycles';
import {ErrorCode, FatalDiagnosticError} from '../../diagnostics';
import {ModuleResolver, Reference} from '../../imports';
import {ModuleResolver, Reference, ReferenceEmitter} from '../../imports';
import {EnumValue, PartialEvaluator} from '../../partial_evaluator';
import {Decorator, ReflectionHost, filterToMembersWithDecorator, reflectObjectLiteral} from '../../reflection';
import {LocalModuleScopeRegistry, ScopeDirective, extractDirectiveGuards} from '../../scope';
import {AnalysisOutput, CompileResult, DecoratorHandler, DetectResult, HandlerPrecedence} from '../../transform';
import {TypeCheckContext} from '../../typecheck';
import {tsSourceMapBug29300Fixed} from '../../util/src/ts_source_map_bug_29300';
@ -22,8 +23,7 @@ import {tsSourceMapBug29300Fixed} from '../../util/src/ts_source_map_bug_29300';
import {ResourceLoader} from './api';
import {extractDirectiveMetadata, extractQueriesFromDecorator, parseFieldArrayValue, queriesFromFields} from './directive';
import {generateSetClassMetadataCall} from './metadata';
import {ScopeDirective, SelectorScopeRegistry} from './selector_scope';
import {extractDirectiveGuards, isAngularCore, isAngularCoreReference, unwrapExpression} from './util';
import {isAngularCore, isAngularCoreReference, unwrapExpression} from './util';
const EMPTY_MAP = new Map<string, Expression>();
const EMPTY_ARRAY: any[] = [];
@ -41,10 +41,11 @@ export class ComponentDecoratorHandler implements
DecoratorHandler<ComponentHandlerData, Decorator> {
constructor(
private reflector: ReflectionHost, private evaluator: PartialEvaluator,
private scopeRegistry: SelectorScopeRegistry, private isCore: boolean,
private scopeRegistry: LocalModuleScopeRegistry, private isCore: boolean,
private resourceLoader: ResourceLoader, private rootDirs: string[],
private defaultPreserveWhitespaces: boolean, private i18nUseExternalIds: boolean,
private moduleResolver: ModuleResolver, private cycleAnalyzer: CycleAnalyzer) {}
private moduleResolver: ModuleResolver, private cycleAnalyzer: CycleAnalyzer,
private refEmitter: ReferenceEmitter) {}
private literalCache = new Map<Decorator, ts.ObjectLiteralExpression>();
private elementSchemaRegistry = new DomElementSchemaRegistry();
@ -222,14 +223,13 @@ export class ComponentDecoratorHandler implements
`Errors parsing template: ${template.errors.map(e => e.toString()).join(', ')}`);
}
// If the component has a selector, it should be registered with the `SelectorScopeRegistry` so
// when this component appears in an `@NgModule` scope, its selector can be determined.
// If the component has a selector, it should be registered with the `LocalModuleScopeRegistry`
// so that when this component appears in an `@NgModule` scope, its selector can be determined.
if (metadata.selector !== null) {
const ref = new Reference(node);
this.scopeRegistry.registerDirective(node, {
this.scopeRegistry.registerDirective({
ref,
name: node.name !.text,
directive: ref,
selector: metadata.selector,
exportAs: metadata.exportAs,
inputs: metadata.inputs,
@ -313,10 +313,13 @@ export class ComponentDecoratorHandler implements
}
typeCheck(ctx: TypeCheckContext, node: ts.Declaration, meta: ComponentHandlerData): void {
const scope = this.scopeRegistry.lookupCompilationScopeAsRefs(node);
const matcher = new SelectorMatcher<ScopeDirective<any>>();
if (!ts.isClassDeclaration(node)) {
return;
}
const scope = this.scopeRegistry.getScopeForComponent(node);
const matcher = new SelectorMatcher<ScopeDirective>();
if (scope !== null) {
for (const meta of scope.directives) {
for (const meta of scope.compilation.directives) {
matcher.addSelectables(CssSelector.parse(meta.selector), meta);
}
ctx.addTemplate(node as ts.ClassDeclaration, meta.parsedTemplate, matcher);
@ -324,26 +327,33 @@ export class ComponentDecoratorHandler implements
}
resolve(node: ts.ClassDeclaration, analysis: ComponentHandlerData): void {
const context = node.getSourceFile();
// Check whether this component was registered with an NgModule. If so, it should be compiled
// under that module's compilation scope.
const scope = this.scopeRegistry.lookupCompilationScope(node);
const scope = this.scopeRegistry.getScopeForComponent(node);
let metadata = analysis.meta;
if (scope !== null) {
// Replace the empty components and directives from the analyze() step with a fully expanded
// scope. This is possible now because during resolve() the whole compilation unit has been
// fully analyzed.
const {pipes, containsForwardDecls} = scope;
const directives =
scope.directives.map(dir => ({selector: dir.selector, expression: dir.directive}));
const directives = scope.compilation.directives.map(
dir => ({selector: dir.selector, expression: this.refEmitter.emit(dir.ref, context)}));
const pipes = new Map<string, Expression>();
for (const pipe of scope.compilation.pipes) {
pipes.set(pipe.name, this.refEmitter.emit(pipe.ref, context));
}
// Scan through the references of the `scope.directives` array and check whether
// any import which needs to be generated for the directive would create a cycle.
const origin = node.getSourceFile();
const cycleDetected =
scope.directives.some(meta => this._isCyclicImport(meta.directive, origin)) ||
Array.from(scope.pipes.values()).some(pipe => this._isCyclicImport(pipe, origin));
const cycleDetected = directives.some(dir => this._isCyclicImport(dir.expression, origin)) ||
Array.from(pipes.values()).some(pipe => this._isCyclicImport(pipe, origin));
if (!cycleDetected) {
const wrapDirectivesAndPipesInClosure: boolean = !!containsForwardDecls;
const wrapDirectivesAndPipesInClosure =
directives.some(
dir => isExpressionForwardReference(dir.expression, node.name !, origin)) ||
Array.from(pipes.values())
.some(pipe => isExpressionForwardReference(pipe, node.name !, origin));
metadata.directives = directives;
metadata.pipes = pipes;
metadata.wrapDirectivesAndPipesInClosure = wrapDirectivesAndPipesInClosure;
@ -446,3 +456,17 @@ function getTemplateRange(templateExpr: ts.Expression) {
endPos: templateExpr.getEnd() - 1,
};
}
function isExpressionForwardReference(
expr: Expression, context: ts.Node, contextSource: ts.SourceFile): boolean {
if (isWrappedTsNodeExpr(expr)) {
const node = ts.getOriginalNode(expr.node);
return node.getSourceFile() === contextSource && context.pos < node.pos;
} else {
return false;
}
}
function isWrappedTsNodeExpr(expr: Expression): expr is WrappedNodeExpr<ts.Node> {
return expr instanceof WrappedNodeExpr;
}

View File

@ -13,11 +13,12 @@ import {ErrorCode, FatalDiagnosticError} from '../../diagnostics';
import {Reference} from '../../imports';
import {EnumValue, PartialEvaluator} from '../../partial_evaluator';
import {ClassMember, ClassMemberKind, Decorator, ReflectionHost, filterToMembersWithDecorator, reflectObjectLiteral} from '../../reflection';
import {LocalModuleScopeRegistry} from '../../scope/src/local';
import {extractDirectiveGuards} from '../../scope/src/util';
import {AnalysisOutput, CompileResult, DecoratorHandler, DetectResult, HandlerPrecedence} from '../../transform';
import {generateSetClassMetadataCall} from './metadata';
import {SelectorScopeRegistry} from './selector_scope';
import {extractDirectiveGuards, getValidConstructorDependencies, isAngularCore, unwrapExpression, unwrapForwardRef} from './util';
import {getValidConstructorDependencies, isAngularCore, unwrapExpression, unwrapForwardRef} from './util';
const EMPTY_OBJECT: {[key: string]: string} = {};
@ -29,7 +30,7 @@ export class DirectiveDecoratorHandler implements
DecoratorHandler<DirectiveHandlerData, Decorator> {
constructor(
private reflector: ReflectionHost, private evaluator: PartialEvaluator,
private scopeRegistry: SelectorScopeRegistry, private isCore: boolean) {}
private scopeRegistry: LocalModuleScopeRegistry, private isCore: boolean) {}
readonly precedence = HandlerPrecedence.PRIMARY;
@ -58,9 +59,8 @@ export class DirectiveDecoratorHandler implements
// when this directive appears in an `@NgModule` scope, its selector can be determined.
if (analysis && analysis.selector !== null) {
const ref = new Reference(node);
this.scopeRegistry.registerDirective(node, {
this.scopeRegistry.registerDirective({
ref,
directive: ref,
name: node.name !.text,
selector: analysis.selector,
exportAs: analysis.exportAs,

View File

@ -14,12 +14,12 @@ import {Reference, ReferenceEmitter} from '../../imports';
import {PartialEvaluator, ResolvedValue} from '../../partial_evaluator';
import {Decorator, ReflectionHost, reflectObjectLiteral, typeNodeToValueExpr} from '../../reflection';
import {NgModuleRouteAnalyzer} from '../../routing';
import {LocalModuleScopeRegistry} from '../../scope';
import {AnalysisOutput, CompileResult, DecoratorHandler, DetectResult, HandlerPrecedence} from '../../transform';
import {getSourceFile} from '../../util/src/typescript';
import {generateSetClassMetadataCall} from './metadata';
import {ReferencesRegistry} from './references_registry';
import {SelectorScopeRegistry} from './selector_scope';
import {getValidConstructorDependencies, isAngularCore, toR3Reference, unwrapExpression} from './util';
export interface NgModuleAnalysis {
@ -37,9 +37,9 @@ export interface NgModuleAnalysis {
export class NgModuleDecoratorHandler implements DecoratorHandler<NgModuleAnalysis, Decorator> {
constructor(
private reflector: ReflectionHost, private evaluator: PartialEvaluator,
private scopeRegistry: SelectorScopeRegistry, private referencesRegistry: ReferencesRegistry,
private isCore: boolean, private routeAnalyzer: NgModuleRouteAnalyzer|null,
private refEmitter: ReferenceEmitter) {}
private scopeRegistry: LocalModuleScopeRegistry,
private referencesRegistry: ReferencesRegistry, private isCore: boolean,
private routeAnalyzer: NgModuleRouteAnalyzer|null, private refEmitter: ReferenceEmitter) {}
readonly precedence = HandlerPrecedence.PRIMARY;
@ -114,9 +114,10 @@ export class NgModuleDecoratorHandler implements DecoratorHandler<NgModuleAnalys
bootstrap = this.resolveTypeList(expr, bootstrapMeta, 'bootstrap');
}
// Register this module's information with the SelectorScopeRegistry. This ensures that during
// the compile() phase, the module's metadata is available for selector scope computation.
this.scopeRegistry.registerModule(node, {declarations, imports, exports});
// Register this module's information with the LocalModuleScopeRegistry. This ensures that
// during the compile() phase, the module's metadata is available for selector scope
// computation.
this.scopeRegistry.registerNgModule(node, {declarations, imports, exports});
const valueContext = node.getSourceFile();
@ -183,16 +184,15 @@ export class NgModuleDecoratorHandler implements DecoratorHandler<NgModuleAnalys
}
const context = getSourceFile(node);
for (const decl of analysis.declarations) {
if (this.scopeRegistry.requiresRemoteScope(decl.node)) {
const scope = this.scopeRegistry.lookupCompilationScopeAsRefs(decl.node);
if (this.scopeRegistry.getRequiresRemoteScope(decl.node)) {
const scope =
this.scopeRegistry.getScopeOfModule(ts.getOriginalNode(node) as ts.Declaration);
if (scope === null) {
continue;
}
const directives: Expression[] = [];
const pipes: Expression[] = [];
scope.directives.forEach(
(directive, _) => { directives.push(this.refEmitter.emit(directive.ref, context) !); });
scope.pipes.forEach(pipe => pipes.push(this.refEmitter.emit(pipe, context) !));
const directives = scope.compilation.directives.map(
directive => this.refEmitter.emit(directive.ref, context));
const pipes = scope.compilation.pipes.map(pipe => this.refEmitter.emit(pipe.ref, context));
const directiveArray = new LiteralArrayExpr(directives);
const pipesArray = new LiteralArrayExpr(pipes);
const declExpr = this.refEmitter.emit(decl, context) !;

View File

@ -10,12 +10,13 @@ import {LiteralExpr, R3PipeMetadata, Statement, WrappedNodeExpr, compilePipeFrom
import * as ts from 'typescript';
import {ErrorCode, FatalDiagnosticError} from '../../diagnostics';
import {Reference} from '../../imports';
import {PartialEvaluator} from '../../partial_evaluator';
import {Decorator, ReflectionHost, reflectObjectLiteral} from '../../reflection';
import {LocalModuleScopeRegistry} from '../../scope/src/local';
import {AnalysisOutput, CompileResult, DecoratorHandler, DetectResult, HandlerPrecedence} from '../../transform';
import {generateSetClassMetadataCall} from './metadata';
import {SelectorScopeRegistry} from './selector_scope';
import {getValidConstructorDependencies, isAngularCore, unwrapExpression} from './util';
export interface PipeHandlerData {
@ -26,7 +27,7 @@ export interface PipeHandlerData {
export class PipeDecoratorHandler implements DecoratorHandler<PipeHandlerData, Decorator> {
constructor(
private reflector: ReflectionHost, private evaluator: PartialEvaluator,
private scopeRegistry: SelectorScopeRegistry, private isCore: boolean) {}
private scopeRegistry: LocalModuleScopeRegistry, private isCore: boolean) {}
readonly precedence = HandlerPrecedence.PRIMARY;
@ -78,7 +79,8 @@ export class PipeDecoratorHandler implements DecoratorHandler<PipeHandlerData, D
throw new FatalDiagnosticError(
ErrorCode.VALUE_HAS_WRONG_TYPE, pipeNameExpr, `@Pipe.name must be a string`);
}
this.scopeRegistry.registerPipe(clazz, pipeName);
const ref = new Reference(clazz);
this.scopeRegistry.registerPipe({ref, name: pipeName});
let pure = true;
if (pipe.has('pure')) {

View File

@ -1,562 +0,0 @@
/**
* @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 {Expression, WrappedNodeExpr} from '@angular/compiler';
import * as ts from 'typescript';
import {Reference, ReferenceEmitter} from '../../imports';
import {ReflectionHost, reflectIdentifierOfDeclaration, reflectNameOfDeclaration, reflectTypeEntityToDeclaration} from '../../reflection';
import {TypeCheckableDirectiveMeta} from '../../typecheck';
import {extractDirectiveGuards} from './util';
/**
* Metadata extracted for a given NgModule that can be used to compute selector scopes.
*/
export interface ModuleData {
declarations: Reference<ts.Declaration>[];
imports: Reference<ts.Declaration>[];
exports: Reference<ts.Declaration>[];
}
/**
* Transitively expanded maps of directives and pipes visible to a component being compiled in the
* context of some module.
*/
export interface CompilationScope<T> {
directives: ScopeDirective<T>[];
pipes: Map<string, T>;
containsForwardDecls?: boolean;
}
export interface ScopeDirective<T> extends TypeCheckableDirectiveMeta {
selector: string;
directive: T;
}
/**
* Both transitively expanded scopes for a given NgModule.
*/
interface SelectorScopes {
/**
* Set of components, directives, and pipes visible to all components being compiled in the
* context of some module.
*/
compilation: Reference<ts.Declaration>[];
/**
* Set of components, directives, and pipes added to the compilation scope of any module importing
* some module.
*/
exported: Reference<ts.Declaration>[];
}
/**
* Registry which records and correlates static analysis information of Angular types.
*
* Once a compilation unit's information is fed into the SelectorScopeRegistry, it can be asked to
* produce transitive `CompilationScope`s for components.
*/
export class SelectorScopeRegistry {
/**
* Map of modules declared in the current compilation unit to their (local) metadata.
*/
private _moduleToData = new Map<ts.Declaration, ModuleData>();
/**
* Map of modules to their cached `CompilationScope`s.
*/
private _compilationScopeCache = new Map<ts.Declaration, CompilationScope<Reference>>();
/**
* Map of components/directives to their metadata.
*/
private _directiveToMetadata = new Map<ts.Declaration, ScopeDirective<Reference>>();
/**
* Map of pipes to their name.
*/
private _pipeToName = new Map<ts.Declaration, string>();
/**
* Components that require remote scoping.
*/
private _requiresRemoteScope = new Set<ts.Declaration>();
/**
* Map of components/directives/pipes to their module.
*/
private _declararedTypeToModule = new Map<ts.Declaration, ts.Declaration>();
constructor(
private checker: ts.TypeChecker, private reflector: ReflectionHost,
private refEmitter: ReferenceEmitter) {}
/**
* Register a module's metadata with the registry.
*/
registerModule(node: ts.Declaration, data: ModuleData): void {
node = ts.getOriginalNode(node) as ts.Declaration;
if (this._moduleToData.has(node)) {
throw new Error(`Module already registered: ${reflectNameOfDeclaration(node)}`);
}
this._moduleToData.set(node, data);
// Register all of the module's declarations in the context map as belonging to this module.
data.declarations.forEach(decl => {
this._declararedTypeToModule.set(ts.getOriginalNode(decl.node) as ts.Declaration, node);
});
}
/**
* Register the metadata of a component or directive with the registry.
*/
registerDirective(node: ts.Declaration, metadata: ScopeDirective<Reference>): void {
node = ts.getOriginalNode(node) as ts.Declaration;
if (this._directiveToMetadata.has(node)) {
throw new Error(
`Selector already registered: ${reflectNameOfDeclaration(node)} ${metadata.selector}`);
}
this._directiveToMetadata.set(node, metadata);
}
/**
* Register the name of a pipe with the registry.
*/
registerPipe(node: ts.Declaration, name: string): void {
node = ts.getOriginalNode(node) as ts.Declaration;
this._pipeToName.set(node, name);
}
/**
* Mark a component (identified by its `ts.Declaration`) as requiring its `directives` scope to be
* set remotely, from the file of the @NgModule which declares the component.
*/
setComponentAsRequiringRemoteScoping(component: ts.Declaration): void {
this._requiresRemoteScope.add(component);
}
/**
* Check whether the given component requires its `directives` scope to be set remotely.
*/
requiresRemoteScope(component: ts.Declaration): boolean {
return this._requiresRemoteScope.has(ts.getOriginalNode(component) as ts.Declaration);
}
lookupCompilationScopeAsRefs(node: ts.Declaration): CompilationScope<Reference>|null {
node = ts.getOriginalNode(node) as ts.Declaration;
// If the component has no associated module, then it has no compilation scope.
if (!this._declararedTypeToModule.has(node)) {
return null;
}
const module = this._declararedTypeToModule.get(node) !;
// Compilation scope computation is somewhat expensive, so it's cached. Check the cache for
// the module.
if (this._compilationScopeCache.has(module)) {
// The compilation scope was cached.
const scope = this._compilationScopeCache.get(module) !;
// The scope as cached is in terms of References, not Expressions. Converting between them
// requires knowledge of the context file (in this case, the component node's source file).
return scope;
}
// This is the first time the scope for this module is being computed.
const directives: ScopeDirective<Reference<ts.Declaration>>[] = [];
const pipes = new Map<string, Reference<ts.Declaration>>();
// Tracks which declarations already appear in the `CompilationScope`.
const seenSet = new Set<ts.Declaration>();
// Process the declaration scope of the module, and lookup the selector of every declared type.
// The initial value of ngModuleImportedFrom is 'null' which signifies that the NgModule
// was not imported from a .d.ts source.
for (const ref of this
.lookupScopesOrDie(
module !, /* ngModuleImportedFrom */ null, node.getSourceFile().fileName)
.compilation) {
const node = ts.getOriginalNode(ref.node) as ts.Declaration;
// Track whether this `ts.Declaration` has been seen before.
if (seenSet.has(node)) {
continue;
} else {
seenSet.add(node);
}
// Either the node represents a directive or a pipe. Look for both.
const metadata = this.lookupDirectiveMetadata(ref);
// Only directives/components with selectors get added to the scope.
if (metadata !== null) {
directives.push({...metadata, directive: ref});
} else {
const name = this.lookupPipeName(node);
if (name !== null) {
pipes.set(name, ref);
}
}
}
const scope: CompilationScope<Reference> = {directives, pipes};
// Many components may be compiled in the same scope, so cache it.
this._compilationScopeCache.set(node, scope);
// Convert References to Expressions in the context of the component's source file.
return scope;
}
/**
* Produce the compilation scope of a component, which is determined by the module that declares
* it.
*/
lookupCompilationScope(node: ts.Declaration): CompilationScope<Expression>|null {
const scope = this.lookupCompilationScopeAsRefs(node);
return scope !== null ? convertScopeToExpressions(scope, node, this.refEmitter) : null;
}
private lookupScopesOrDie(
node: ts.Declaration, ngModuleImportedFrom: string|null,
resolutionContext: string): SelectorScopes {
const result = this.lookupScopes(node, ngModuleImportedFrom, resolutionContext);
if (result === null) {
throw new Error(`Module not found: ${reflectNameOfDeclaration(node)}`);
}
return result;
}
/**
* Lookup `SelectorScopes` for a given module.
*
* This function assumes that if the given module was imported from an absolute path
* (`ngModuleImportedFrom`) then all of its declarations are exported at that same path, as well
* as imports and exports from other modules that are relatively imported.
*/
private lookupScopes(
node: ts.Declaration, ngModuleImportedFrom: string|null,
resolutionContext: string): SelectorScopes|null {
let data: ModuleData|null = null;
// Either this module was analyzed directly, or has a precompiled ngModuleDef.
if (this._moduleToData.has(node)) {
// The module was analyzed before, and thus its data is available.
data = this._moduleToData.get(node) !;
} else {
// The module wasn't analyzed before, and probably has a precompiled ngModuleDef with a type
// annotation that specifies the needed metadata.
data = this._readModuleDataFromCompiledClass(node, ngModuleImportedFrom, resolutionContext);
// Note that data here could still be null, if the class didn't have a precompiled
// ngModuleDef.
}
if (data === null) {
return null;
}
const context = node.getSourceFile().fileName;
return {
compilation: [
...data.declarations,
// Expand imports to the exported scope of those imports.
...flatten(data.imports.map(
ref =>
this.lookupScopesOrDie(ref.node as ts.Declaration, ref.ownedByModuleGuess, context)
.exported)),
// And include the compilation scope of exported modules.
...flatten(
data.exports
.map(
ref => this.lookupScopes(
ref.node as ts.Declaration, ref.ownedByModuleGuess, context))
.filter((scope: SelectorScopes | null): scope is SelectorScopes => scope !== null)
.map(scope => scope.exported))
],
exported: flatten(data.exports.map(ref => {
const scope =
this.lookupScopes(ref.node as ts.Declaration, ref.ownedByModuleGuess, context);
if (scope !== null) {
return scope.exported;
} else {
return [ref];
}
})),
};
}
/**
* Lookup the metadata of a component or directive class.
*
* Potentially this class is declared in a .d.ts file or otherwise has a manually created
* ngComponentDef/ngDirectiveDef. In this case, the type metadata of that definition is read
* to determine the metadata.
*/
private lookupDirectiveMetadata(ref: Reference<ts.Declaration>): ScopeDirective<Reference>|null {
const node = ts.getOriginalNode(ref.node) as ts.Declaration;
if (this._directiveToMetadata.has(node)) {
return this._directiveToMetadata.get(node) !;
} else {
return this._readMetadataFromCompiledClass(ref as Reference<ts.ClassDeclaration>);
}
}
private lookupPipeName(node: ts.Declaration): string|null {
if (this._pipeToName.has(node)) {
return this._pipeToName.get(node) !;
} else {
return this._readNameFromCompiledClass(node);
}
}
/**
* Read the metadata from a class that has already been compiled somehow (either it's in a .d.ts
* file, or in a .ts file with a handwritten definition).
*
* @param clazz the class of interest
* @param ngModuleImportedFrom module specifier of the import path to assume for all declarations
* stemming from this module.
*/
private _readModuleDataFromCompiledClass(
clazz: ts.Declaration, ngModuleImportedFrom: string|null,
resolutionContext: string): ModuleData|null {
// This operation is explicitly not memoized, as it depends on `ngModuleImportedFrom`.
// TODO(alxhub): investigate caching of .d.ts module metadata.
const ngModuleDef = this.reflector.getMembersOfClass(clazz).find(
member => member.name === 'ngModuleDef' && member.isStatic);
if (ngModuleDef === undefined) {
return null;
} else if (
// Validate that the shape of the ngModuleDef type is correct.
ngModuleDef.type === null || !ts.isTypeReferenceNode(ngModuleDef.type) ||
ngModuleDef.type.typeArguments === undefined ||
ngModuleDef.type.typeArguments.length !== 4) {
return null;
}
// Read the ModuleData out of the type arguments.
const [_, declarationMetadata, importMetadata, exportMetadata] = ngModuleDef.type.typeArguments;
return {
declarations: this._extractReferencesFromType(
declarationMetadata, ngModuleImportedFrom, resolutionContext),
exports:
this._extractReferencesFromType(exportMetadata, ngModuleImportedFrom, resolutionContext),
imports:
this._extractReferencesFromType(importMetadata, ngModuleImportedFrom, resolutionContext),
};
}
/**
* Get the selector from type metadata for a class with a precompiled ngComponentDef or
* ngDirectiveDef.
*/
private _readMetadataFromCompiledClass(ref: Reference<ts.ClassDeclaration>):
ScopeDirective<Reference>|null {
const clazz = ts.getOriginalNode(ref.node) as ts.ClassDeclaration;
const def = this.reflector.getMembersOfClass(clazz).find(
field =>
field.isStatic && (field.name === 'ngComponentDef' || field.name === 'ngDirectiveDef'));
if (def === undefined) {
// No definition could be found.
return null;
} else if (
def.type === null || !ts.isTypeReferenceNode(def.type) ||
def.type.typeArguments === undefined || def.type.typeArguments.length < 2) {
// The type metadata was the wrong shape.
return null;
}
const selector = readStringType(def.type.typeArguments[1]);
if (selector === null) {
return null;
}
return {
ref,
name: clazz.name !.text,
directive: ref,
isComponent: def.name === 'ngComponentDef', selector,
exportAs: readStringArrayType(def.type.typeArguments[2]),
inputs: readStringMapType(def.type.typeArguments[3]),
outputs: readStringMapType(def.type.typeArguments[4]),
queries: readStringArrayType(def.type.typeArguments[5]),
...extractDirectiveGuards(clazz, this.reflector),
};
}
/**
* Get the selector from type metadata for a class with a precompiled ngComponentDef or
* ngDirectiveDef.
*/
private _readNameFromCompiledClass(clazz: ts.Declaration): string|null {
const def = this.reflector.getMembersOfClass(clazz).find(
field => field.isStatic && field.name === 'ngPipeDef');
if (def === undefined) {
// No definition could be found.
return null;
} else if (
def.type === null || !ts.isTypeReferenceNode(def.type) ||
def.type.typeArguments === undefined || def.type.typeArguments.length < 2) {
// The type metadata was the wrong shape.
return null;
}
const type = def.type.typeArguments[1];
if (!ts.isLiteralTypeNode(type) || !ts.isStringLiteral(type.literal)) {
// The type metadata was the wrong type.
return null;
}
return type.literal.text;
}
/**
* Process a `TypeNode` which is a tuple of references to other types, and return `Reference`s to
* them.
*
* This operation assumes that these types should be imported from `ngModuleImportedFrom` unless
* they themselves were imported from another absolute path.
*/
private _extractReferencesFromType(
def: ts.TypeNode, ngModuleImportedFrom: string|null,
resolutionContext: string): Reference<ts.Declaration>[] {
if (!ts.isTupleTypeNode(def)) {
return [];
}
return def.elementTypes.map(element => {
if (!ts.isTypeQueryNode(element)) {
throw new Error(`Expected TypeQueryNode`);
}
const type = element.exprName;
if (ngModuleImportedFrom !== null) {
const {node, from} = reflectTypeEntityToDeclaration(type, this.checker);
const specifier = (from !== null && !from.startsWith('.') ? from : ngModuleImportedFrom);
return new Reference(node, {specifier, resolutionContext});
} else {
const {node} = reflectTypeEntityToDeclaration(type, this.checker);
return new Reference(node);
}
});
}
}
function flatten<T>(array: T[][]): T[] {
return array.reduce((accum, subArray) => {
accum.push(...subArray);
return accum;
}, [] as T[]);
}
function convertDirectiveReferenceList(
input: ScopeDirective<Reference>[], context: ts.SourceFile,
refEmitter: ReferenceEmitter): ScopeDirective<Expression>[] {
return input.map(meta => {
const directive = refEmitter.emit(meta.directive, context);
if (directive === null) {
throw new Error(`Could not write expression to reference ${meta.directive.node}`);
}
return {...meta, directive};
});
}
function convertPipeReferenceMap(
map: Map<string, Reference>, context: ts.SourceFile,
refEmitter: ReferenceEmitter): Map<string, Expression> {
const newMap = new Map<string, Expression>();
map.forEach((meta, selector) => {
const pipe = refEmitter.emit(meta, context);
if (pipe === null) {
throw new Error(`Could not write expression to reference ${meta.node}`);
}
newMap.set(selector, pipe);
});
return newMap;
}
function convertScopeToExpressions(
scope: CompilationScope<Reference>, context: ts.Declaration,
refEmitter: ReferenceEmitter): CompilationScope<Expression> {
const sourceContext = ts.getOriginalNode(context).getSourceFile();
const directives = convertDirectiveReferenceList(scope.directives, sourceContext, refEmitter);
const pipes = convertPipeReferenceMap(scope.pipes, sourceContext, refEmitter);
const declPointer = maybeUnwrapNameOfDeclaration(context);
let containsForwardDecls = false;
directives.forEach(meta => {
containsForwardDecls = containsForwardDecls ||
isExpressionForwardReference(meta.directive, declPointer, sourceContext);
});
!containsForwardDecls && pipes.forEach(expr => {
containsForwardDecls =
containsForwardDecls || isExpressionForwardReference(expr, declPointer, sourceContext);
});
return {directives, pipes, containsForwardDecls};
}
function isExpressionForwardReference(
expr: Expression, context: ts.Node, contextSource: ts.SourceFile): boolean {
if (isWrappedTsNodeExpr(expr)) {
const node = ts.getOriginalNode(expr.node);
return node.getSourceFile() === contextSource && context.pos < node.pos;
}
return false;
}
function isWrappedTsNodeExpr(expr: Expression): expr is WrappedNodeExpr<ts.Node> {
return expr instanceof WrappedNodeExpr;
}
function maybeUnwrapNameOfDeclaration(decl: ts.Declaration): ts.Declaration|ts.Identifier {
if ((ts.isClassDeclaration(decl) || ts.isVariableDeclaration(decl)) && decl.name !== undefined &&
ts.isIdentifier(decl.name)) {
return decl.name;
}
return decl;
}
function readStringType(type: ts.TypeNode): string|null {
if (!ts.isLiteralTypeNode(type) || !ts.isStringLiteral(type.literal)) {
return null;
}
return type.literal.text;
}
function readStringMapType(type: ts.TypeNode): {[key: string]: string} {
if (!ts.isTypeLiteralNode(type)) {
return {};
}
const obj: {[key: string]: string} = {};
type.members.forEach(member => {
if (!ts.isPropertySignature(member) || member.type === undefined || member.name === undefined ||
!ts.isStringLiteral(member.name)) {
return;
}
const value = readStringType(member.type);
if (value === null) {
return null;
}
obj[member.name.text] = value;
});
return obj;
}
function readStringArrayType(type: ts.TypeNode): string[] {
if (!ts.isTupleTypeNode(type)) {
return [];
}
const res: string[] = [];
type.elementTypes.forEach(el => {
if (!ts.isLiteralTypeNode(el) || !ts.isStringLiteral(el.literal)) {
return;
}
res.push(el.literal.text);
});
return res;
}

View File

@ -213,20 +213,3 @@ export function forwardRefResolver(
}
return expandForwardRef(args[0]);
}
export function extractDirectiveGuards(node: ts.Declaration, reflector: ReflectionHost): {
ngTemplateGuards: string[],
hasNgTemplateContextGuard: boolean,
} {
const methods = nodeStaticMethodNames(node, reflector);
const ngTemplateGuards = methods.filter(method => method.startsWith('ngTemplateGuard_'))
.map(method => method.split('_', 2)[1]);
const hasNgTemplateContextGuard = methods.some(name => name === 'ngTemplateContextGuard');
return {hasNgTemplateContextGuard, ngTemplateGuards};
}
function nodeStaticMethodNames(node: ts.Declaration, reflector: ReflectionHost): string[] {
return reflector.getMembersOfClass(node)
.filter(member => member.kind === ClassMemberKind.Method && member.isStatic)
.map(member => member.name);
}

View File

@ -18,6 +18,7 @@ ts_library(
"//packages/compiler-cli/src/ngtsc/partial_evaluator",
"//packages/compiler-cli/src/ngtsc/path",
"//packages/compiler-cli/src/ngtsc/reflection",
"//packages/compiler-cli/src/ngtsc/scope",
"//packages/compiler-cli/src/ngtsc/testing",
"//packages/compiler-cli/src/ngtsc/translator",
"//packages/compiler-cli/src/ngtsc/util",

View File

@ -13,10 +13,10 @@ import {ErrorCode, FatalDiagnosticError} from '../../diagnostics';
import {ModuleResolver, ReferenceEmitter} from '../../imports';
import {PartialEvaluator} from '../../partial_evaluator';
import {TypeScriptReflectionHost} from '../../reflection';
import {LocalModuleScopeRegistry, MetadataDtsModuleScopeResolver} from '../../scope';
import {getDeclaration, makeProgram} from '../../testing/in_memory_typescript';
import {ResourceLoader} from '../src/api';
import {ComponentDecoratorHandler} from '../src/component';
import {SelectorScopeRegistry} from '../src/selector_scope';
export class NoopResourceLoader implements ResourceLoader {
resolve(): string { throw new Error('Not implemented.'); }
@ -48,11 +48,13 @@ describe('ComponentDecoratorHandler', () => {
const moduleResolver = new ModuleResolver(program, options, host);
const importGraph = new ImportGraph(moduleResolver);
const cycleAnalyzer = new CycleAnalyzer(importGraph);
const scopeRegistry =
new LocalModuleScopeRegistry(new MetadataDtsModuleScopeResolver(checker, reflectionHost));
const refEmitter = new ReferenceEmitter([]);
const handler = new ComponentDecoratorHandler(
reflectionHost, evaluator,
new SelectorScopeRegistry(checker, reflectionHost, new ReferenceEmitter([])), false,
new NoopResourceLoader(), [''], false, true, moduleResolver, cycleAnalyzer);
reflectionHost, evaluator, scopeRegistry, false, new NoopResourceLoader(), [''], false,
true, moduleResolver, cycleAnalyzer, refEmitter);
const TestCmp = getDeclaration(program, 'entry.ts', 'TestCmp', ts.isClassDeclaration);
const detected = handler.detect(TestCmp, reflectionHost.getDecoratorsOfDeclaration(TestCmp));
if (detected === undefined) {

View File

@ -8,17 +8,16 @@
import * as ts from 'typescript';
import {ReferenceEmitter} from '../../imports';
import {PartialEvaluator} from '../../partial_evaluator';
import {TypeScriptReflectionHost} from '../../reflection';
import {LocalModuleScopeRegistry, MetadataDtsModuleScopeResolver} from '../../scope';
import {getDeclaration, makeProgram} from '../../testing/in_memory_typescript';
import {DirectiveDecoratorHandler} from '../src/directive';
import {SelectorScopeRegistry} from '../src/selector_scope';
describe('DirectiveDecoratorHandler', () => {
it('should use the `ReflectionHost` to detect class inheritance', () => {
const {program, options, host} = makeProgram([
const {program} = makeProgram([
{
name: 'node_modules/@angular/core/index.d.ts',
contents: 'export const Directive: any;',
@ -40,9 +39,9 @@ describe('DirectiveDecoratorHandler', () => {
const checker = program.getTypeChecker();
const reflectionHost = new TestReflectionHost(checker);
const evaluator = new PartialEvaluator(reflectionHost, checker);
const handler = new DirectiveDecoratorHandler(
reflectionHost, evaluator,
new SelectorScopeRegistry(checker, reflectionHost, new ReferenceEmitter([])), false);
const scopeRegistry =
new LocalModuleScopeRegistry(new MetadataDtsModuleScopeResolver(checker, reflectionHost));
const handler = new DirectiveDecoratorHandler(reflectionHost, evaluator, scopeRegistry, false);
const analyzeDirective = (dirName: string) => {
const DirNode = getDeclaration(program, 'entry.ts', dirName, ts.isClassDeclaration);

View File

@ -1,184 +0,0 @@
/**
* @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';
import {AbsoluteModuleStrategy, LocalIdentifierStrategy, LogicalProjectStrategy, Reference, ReferenceEmitter} from '../../imports';
import {LogicalFileSystem} from '../../path';
import {TypeScriptReflectionHost} from '../../reflection';
import {getDeclaration, makeProgram} from '../../testing/in_memory_typescript';
import {getRootDirs} from '../../util/src/typescript';
import {SelectorScopeRegistry} from '../src/selector_scope';
describe('SelectorScopeRegistry', () => {
it('absolute imports work', () => {
const {program, options, host} = makeProgram([
{
name: 'node_modules/@angular/core/index.d.ts',
contents: `
export interface NgComponentDefWithMeta<A, B, C, D, E, F> {}
export interface NgModuleDef<A, B, C, D> {}
`
},
{
name: 'node_modules/some_library/index.d.ts',
contents: `
import {NgModuleDef} from '@angular/core';
import * as i0 from './component';
export {SomeCmp} from './component';
export declare class SomeModule {
static ngModuleDef: NgModuleDef<SomeModule, [typeof i0.SomeCmp], never, [typeof i0.SomeCmp]>;
}
`
},
{
name: 'node_modules/some_library/component.d.ts',
contents: `
import {NgComponentDefWithMeta} from '@angular/core';
export declare class SomeCmp {
static ngComponentDef: NgComponentDefWithMeta<SomeCmp, 'some-cmp', never, {}, {}, never>;
}
`
},
{
name: 'entry.ts',
contents: `
export class ProgramCmp {}
export class ProgramModule {}
`
},
]);
const checker = program.getTypeChecker();
const reflectionHost = new TypeScriptReflectionHost(checker);
const ProgramModule =
getDeclaration(program, 'entry.ts', 'ProgramModule', ts.isClassDeclaration);
const ProgramCmp = getDeclaration(program, 'entry.ts', 'ProgramCmp', ts.isClassDeclaration);
const SomeModule = getDeclaration(
program, 'node_modules/some_library/index.d.ts', 'SomeModule', ts.isClassDeclaration);
expect(ProgramModule).toBeDefined();
expect(SomeModule).toBeDefined();
const ProgramCmpRef = new Reference(ProgramCmp);
const refEmitter = makeReferenceEmitter(program, checker, options, host);
const registry = new SelectorScopeRegistry(checker, reflectionHost, refEmitter);
registry.registerModule(ProgramModule, {
declarations: [new Reference(ProgramCmp)],
exports: [],
imports: [new Reference(
SomeModule,
{specifier: 'some_library', resolutionContext: '/node_modules/some_library/index.d.ts'})],
});
const ref = new Reference(ProgramCmp);
registry.registerDirective(ProgramCmp, {
name: 'ProgramCmp',
ref: ProgramCmpRef,
directive: ProgramCmpRef,
selector: 'program-cmp',
isComponent: true,
exportAs: null,
inputs: {},
outputs: {},
queries: [],
hasNgTemplateContextGuard: false,
ngTemplateGuards: [],
});
const scope = registry.lookupCompilationScope(ProgramCmp) !;
expect(scope).toBeDefined();
expect(scope.directives).toBeDefined();
expect(scope.directives.length).toBe(2);
});
it('exports of third-party libs work', () => {
const {program, options, host} = makeProgram([
{
name: 'node_modules/@angular/core/index.d.ts',
contents: `
export interface NgComponentDefWithMeta<A, B, C, D, E, F> {}
export interface NgModuleDef<A, B, C, D> {}
`
},
{
name: 'node_modules/some_library/index.d.ts',
contents: `
import {NgComponentDefWithMeta, NgModuleDef} from '@angular/core';
export declare class SomeModule {
static ngModuleDef: NgModuleDef<SomeModule, [typeof SomeCmp], never, [typeof SomeCmp]>;
}
export declare class SomeCmp {
static ngComponentDef: NgComponentDefWithMeta<SomeCmp, 'some-cmp', never, {}, {}, never>;
}
`
},
{
name: 'entry.ts',
contents: `
export class ProgramCmp {}
export class ProgramModule {}
`
},
]);
const checker = program.getTypeChecker();
const reflectionHost = new TypeScriptReflectionHost(checker);
const ProgramModule =
getDeclaration(program, 'entry.ts', 'ProgramModule', ts.isClassDeclaration);
const ProgramCmp = getDeclaration(program, 'entry.ts', 'ProgramCmp', ts.isClassDeclaration);
const SomeModule = getDeclaration(
program, 'node_modules/some_library/index.d.ts', 'SomeModule', ts.isClassDeclaration);
expect(ProgramModule).toBeDefined();
expect(SomeModule).toBeDefined();
const ProgramCmpRef = new Reference(ProgramCmp);
const refEmitter = makeReferenceEmitter(program, checker, options, host);
const registry = new SelectorScopeRegistry(checker, reflectionHost, refEmitter);
registry.registerModule(ProgramModule, {
declarations: [new Reference(ProgramCmp)],
exports: [new Reference(
SomeModule,
{specifier: 'some_library', resolutionContext: '/node_modules/some_library/index.d.ts'})],
imports: [],
});
registry.registerDirective(ProgramCmp, {
name: 'ProgramCmp',
ref: ProgramCmpRef,
directive: ProgramCmpRef,
selector: 'program-cmp',
isComponent: true,
exportAs: null,
inputs: {},
outputs: {},
queries: [],
hasNgTemplateContextGuard: false,
ngTemplateGuards: [],
});
const scope = registry.lookupCompilationScope(ProgramCmp) !;
expect(scope).toBeDefined();
expect(scope.directives).toBeDefined();
expect(scope.directives.length).toBe(2);
});
});
function makeReferenceEmitter(
program: ts.Program, checker: ts.TypeChecker, options: ts.CompilerOptions,
host: ts.CompilerHost): ReferenceEmitter {
const rootDirs = getRootDirs(host, options);
return new ReferenceEmitter([
new LocalIdentifierStrategy(),
new AbsoluteModuleStrategy(program, checker, options, host),
new LogicalProjectStrategy(checker, new LogicalFileSystem(rootDirs)),
]);
}