From b6c4af649505a7d158c7d4c0df4d43270bd29fd7 Mon Sep 17 00:00:00 2001 From: Chuck Jazdzewski Date: Thu, 13 Jul 2017 14:25:17 -0700 Subject: [PATCH] feat(compiler-cli): automatically lower lambda expressions in metadata --- packages/compiler-cli/src/compiler_host.ts | 10 +- .../src/path_mapped_compiler_host.ts | 2 +- packages/compiler-cli/src/transformers/api.ts | 4 + .../src/transformers/entry_points.ts | 8 + .../src/transformers/lower_expressions.ts | 177 ++++++++++++++++++ .../transformers/module_filename_resolver.ts | 8 + .../compiler-cli/src/transformers/program.ts | 42 +++-- packages/compiler-cli/test/ngc_spec.ts | 118 +++++++++++- .../transformers/lower_expressions_spec.ts | 107 +++++++++++ 9 files changed, 456 insertions(+), 20 deletions(-) create mode 100644 packages/compiler-cli/src/transformers/lower_expressions.ts create mode 100644 packages/compiler-cli/test/transformers/lower_expressions_spec.ts diff --git a/packages/compiler-cli/src/compiler_host.ts b/packages/compiler-cli/src/compiler_host.ts index 04d5083770..e7c5c03954 100644 --- a/packages/compiler-cli/src/compiler_host.ts +++ b/packages/compiler-cli/src/compiler_host.ts @@ -25,8 +25,9 @@ export interface CompilerHostContext extends ts.ModuleResolutionHost { assumeFileExists(fileName: string): void; } +export interface MetadataProvider { getMetadata(source: ts.SourceFile): ModuleMetadata|undefined; } + export class CompilerHost implements AotCompilerHost { - protected metadataCollector = new MetadataCollector(); private isGenDirChildOfRootDir: boolean; protected basePath: string; private genDir: string; @@ -39,7 +40,8 @@ export class CompilerHost implements AotCompilerHost { constructor( protected program: ts.Program, protected options: AngularCompilerOptions, - protected context: CompilerHostContext, collectorOptions?: CollectorOptions) { + protected context: CompilerHostContext, collectorOptions?: CollectorOptions, + protected metadataProvider: MetadataProvider = new MetadataCollector()) { // normalize the path so that it never ends with '/'. this.basePath = path.normalize(path.join(this.options.basePath !, '.')).replace(/\\/g, '/'); this.genDir = path.normalize(path.join(this.options.genDir !, '.')).replace(/\\/g, '/'); @@ -206,7 +208,7 @@ export class CompilerHost implements AotCompilerHost { } const sf = this.getSourceFile(filePath); - const metadata = this.metadataCollector.getMetadata(sf); + const metadata = this.metadataProvider.getMetadata(sf); return metadata ? [metadata] : []; } @@ -245,7 +247,7 @@ export class CompilerHost implements AotCompilerHost { v3Metadata.metadata[prop] = v1Metadata.metadata[prop]; } - const exports = this.metadataCollector.getMetadata(this.getSourceFile(dtsFilePath)); + const exports = this.metadataProvider.getMetadata(this.getSourceFile(dtsFilePath)); if (exports) { for (let prop in exports.metadata) { if (!v3Metadata.metadata[prop]) { diff --git a/packages/compiler-cli/src/path_mapped_compiler_host.ts b/packages/compiler-cli/src/path_mapped_compiler_host.ts index 2463b5b4ee..f17b0a5800 100644 --- a/packages/compiler-cli/src/path_mapped_compiler_host.ts +++ b/packages/compiler-cli/src/path_mapped_compiler_host.ts @@ -132,7 +132,7 @@ export class PathMappedCompilerHost extends CompilerHost { } else { const sf = this.getSourceFile(rootedPath); sf.fileName = sf.fileName; - const metadata = this.metadataCollector.getMetadata(sf); + const metadata = this.metadataProvider.getMetadata(sf); return metadata ? [metadata] : []; } } diff --git a/packages/compiler-cli/src/transformers/api.ts b/packages/compiler-cli/src/transformers/api.ts index ea0919c3b5..de1ccc3b9d 100644 --- a/packages/compiler-cli/src/transformers/api.ts +++ b/packages/compiler-cli/src/transformers/api.ts @@ -91,6 +91,10 @@ export interface CompilerOptions extends ts.CompilerOptions { // Whether to enable support for