From aaf8145c480682238ea4f720c4509763173898ec Mon Sep 17 00:00:00 2001 From: Andrew Kushnir Date: Mon, 22 Apr 2019 18:04:53 -0700 Subject: [PATCH] fix(ivy): support module.id as @NgModule's "id" field value (#30040) Prior to this commit, the check that verifies correct "id" field type was too strict and didn't allow `module.id` as @NgModule's "id" field value. This change adds a special handling for `module.id` and uses it as id of @NgModule if specified. PR Close #30040 --- .../src/ngtsc/annotations/src/ng_module.ts | 19 +++++------------ .../compiler-cli/test/ngtsc/ngtsc_spec.ts | 21 ++++++++++++++++++- 2 files changed, 25 insertions(+), 15 deletions(-) diff --git a/packages/compiler-cli/src/ngtsc/annotations/src/ng_module.ts b/packages/compiler-cli/src/ngtsc/annotations/src/ng_module.ts index 97691411bc..f65b93db86 100644 --- a/packages/compiler-cli/src/ngtsc/annotations/src/ng_module.ts +++ b/packages/compiler-cli/src/ngtsc/annotations/src/ng_module.ts @@ -6,7 +6,7 @@ * found in the LICENSE file at https://angular.io/license */ -import {Expression, ExternalExpr, InvokeFunctionExpr, LiteralArrayExpr, LiteralExpr, R3Identifiers, R3InjectorMetadata, R3NgModuleMetadata, R3Reference, Statement, WrappedNodeExpr, compileInjector, compileNgModule} from '@angular/compiler'; +import {Expression, ExternalExpr, InvokeFunctionExpr, LiteralArrayExpr, R3Identifiers, R3InjectorMetadata, R3NgModuleMetadata, R3Reference, Statement, WrappedNodeExpr, compileInjector, compileNgModule} from '@angular/compiler'; import * as ts from 'typescript'; import {ErrorCode, FatalDiagnosticError} from '../../diagnostics'; @@ -29,7 +29,7 @@ export interface NgModuleAnalysis { metadataStmt: Statement|null; declarations: Reference[]; exports: Reference[]; - id: string|null; + id: Expression|null; } /** @@ -121,16 +121,8 @@ export class NgModuleDecoratorHandler implements DecoratorHandler { env.driveMain(); const jsContents = env.getContents('test.js'); - expect(jsContents).toContain('i0.\u0275registerNgModuleType("test", TestModule);'); + expect(jsContents).toContain('i0.\u0275registerNgModuleType(\'test\', TestModule);'); }); + it('should emit a \u0275registerNgModuleType call when the module id is defined as `module.id`', + () => { + env.tsconfig(); + env.write('index.d.ts', ` + declare const module = {id: string}; + `); + env.write('test.ts', ` + import {NgModule} from '@angular/core'; + + @NgModule({id: module.id}) + export class TestModule {} + `); + + env.driveMain(); + + const jsContents = env.getContents('test.js'); + expect(jsContents).toContain('i0.\u0275registerNgModuleType(module.id, TestModule);'); + }); + it('should filter out directives and pipes from module exports in the injector def', () => { env.tsconfig(); env.write('test.ts', `