feat(ivy): create a ModuleResolver to map module paths to files (#27697)

PR Close #27697
This commit is contained in:
Alex Rickabaugh 2018-11-16 17:01:56 +01:00
parent 9e5016c845
commit 19a2b783cf
3 changed files with 26 additions and 2 deletions

View File

@ -8,4 +8,4 @@
export {ImportRewriter, NoopImportRewriter, R3SymbolsImportRewriter, validateAndRewriteCoreSymbol} from './src/core'; export {ImportRewriter, NoopImportRewriter, R3SymbolsImportRewriter, validateAndRewriteCoreSymbol} from './src/core';
export {AbsoluteReference, ImportMode, NodeReference, Reference, ResolvedReference} from './src/references'; export {AbsoluteReference, ImportMode, NodeReference, Reference, ResolvedReference} from './src/references';
export {ReferenceResolver, TsReferenceResolver} from './src/resolver'; export {ModuleResolver, ReferenceResolver, TsReferenceResolver} from './src/resolver';

View File

@ -17,6 +17,28 @@ export interface ReferenceResolver {
Reference<ts.Declaration>; Reference<ts.Declaration>;
} }
/**
* Used by `RouterEntryPointManager` and `NgModuleRouteAnalyzer` (which is in turn is used by
* `NgModuleDecoratorHandler`) for resolving the module source-files references in lazy-loaded
* routes (relative to the source-file containing the `NgModule` that provides the route
* definitions).
*/
export class ModuleResolver {
constructor(
private program: ts.Program, private compilerOptions: ts.CompilerOptions,
private host: ts.CompilerHost) {}
resolveModuleName(module: string, containingFile: ts.SourceFile): ts.SourceFile|null {
const resolved =
ts.resolveModuleName(module, containingFile.fileName, this.compilerOptions, this.host)
.resolvedModule;
if (resolved === undefined) {
return null;
}
return this.program.getSourceFile(resolved.resolvedFileName) || null;
}
}
export class TsReferenceResolver implements ReferenceResolver { export class TsReferenceResolver implements ReferenceResolver {
private moduleExportsCache = new Map<string, Map<ts.Declaration, string>|null>(); private moduleExportsCache = new Map<string, Map<ts.Declaration, string>|null>();

View File

@ -16,7 +16,7 @@ import {ComponentDecoratorHandler, DirectiveDecoratorHandler, InjectableDecorato
import {BaseDefDecoratorHandler} from './annotations/src/base_def'; import {BaseDefDecoratorHandler} from './annotations/src/base_def';
import {ErrorCode, ngErrorCode} from './diagnostics'; import {ErrorCode, ngErrorCode} from './diagnostics';
import {FlatIndexGenerator, ReferenceGraph, checkForPrivateExports, findFlatIndexEntryPoint} from './entry_point'; import {FlatIndexGenerator, ReferenceGraph, checkForPrivateExports, findFlatIndexEntryPoint} from './entry_point';
import {ImportRewriter, NoopImportRewriter, R3SymbolsImportRewriter, Reference, TsReferenceResolver} from './imports'; import {ImportRewriter, ModuleResolver, NoopImportRewriter, R3SymbolsImportRewriter, Reference, TsReferenceResolver} from './imports';
import {PartialEvaluator} from './partial_evaluator'; import {PartialEvaluator} from './partial_evaluator';
import {TypeScriptReflectionHost} from './reflection'; import {TypeScriptReflectionHost} from './reflection';
import {HostResourceLoader} from './resource_loader'; import {HostResourceLoader} from './resource_loader';
@ -45,6 +45,7 @@ export class NgtscProgram implements api.Program {
private flatIndexGenerator: FlatIndexGenerator|null = null; private flatIndexGenerator: FlatIndexGenerator|null = null;
private constructionDiagnostics: ts.Diagnostic[] = []; private constructionDiagnostics: ts.Diagnostic[] = [];
private moduleResolver: ModuleResolver;
constructor( constructor(
@ -124,6 +125,7 @@ export class NgtscProgram implements api.Program {
ts.createProgram(rootFiles, options, this.host, oldProgram && oldProgram.getTsProgram()); ts.createProgram(rootFiles, options, this.host, oldProgram && oldProgram.getTsProgram());
this.entryPoint = entryPoint !== null ? this.tsProgram.getSourceFile(entryPoint) || null : null; this.entryPoint = entryPoint !== null ? this.tsProgram.getSourceFile(entryPoint) || null : null;
this.moduleResolver = new ModuleResolver(this.tsProgram, options, this.host);
} }
getTsProgram(): ts.Program { return this.tsProgram; } getTsProgram(): ts.Program { return this.tsProgram; }