From d9261f245df6f7b75bde474e0308f42f9f2c39f6 Mon Sep 17 00:00:00 2001 From: Alan Date: Wed, 22 May 2019 14:56:56 +0200 Subject: [PATCH] fix(@schematics/angular): handle newline after `@` of a decorator Fixes #14490 --- packages/schematics/angular/utility/ast-utils.ts | 5 ++--- .../schematics/angular/utility/ast-utils_spec.ts | 16 ++++++++++++++++ 2 files changed, 18 insertions(+), 3 deletions(-) diff --git a/packages/schematics/angular/utility/ast-utils.ts b/packages/schematics/angular/utility/ast-utils.ts index dc659003f9dc..661409c15ad5 100644 --- a/packages/schematics/angular/utility/ast-utils.ts +++ b/packages/schematics/angular/utility/ast-utils.ts @@ -286,8 +286,7 @@ export function getDecoratorMetadata(source: ts.SourceFile, identifier: string, if (expr.expression.kind == ts.SyntaxKind.Identifier) { const id = expr.expression as ts.Identifier; - return id.getFullText(source) == identifier - && angularImports[id.getFullText(source)] === module; + return id.text == identifier && angularImports[id.text] === module; } else if (expr.expression.kind == ts.SyntaxKind.PropertyAccessExpression) { // This covers foo.NgModule when importing * as foo. const paExpr = expr.expression as ts.PropertyAccessExpression; @@ -297,7 +296,7 @@ export function getDecoratorMetadata(source: ts.SourceFile, identifier: string, } const id = paExpr.name.text; - const moduleId = (paExpr.expression as ts.Identifier).getText(source); + const moduleId = (paExpr.expression as ts.Identifier).text; return id === identifier && (angularImports[moduleId + '.'] === module); } diff --git a/packages/schematics/angular/utility/ast-utils_spec.ts b/packages/schematics/angular/utility/ast-utils_spec.ts index 6603d6c7fda5..54049048a169 100644 --- a/packages/schematics/angular/utility/ast-utils_spec.ts +++ b/packages/schematics/angular/utility/ast-utils_spec.ts @@ -169,6 +169,22 @@ describe('ast utils', () => { expect(output).toMatch(/imports: \[HelloWorld],\r?\n/m); }); + it(`should handle NgModule with newline after '@'`, () => { + const moduleContent = ` + import { BrowserModule } from '@angular/platform-browser'; + import { NgModule } from '@angular/core'; + + @ + NgModule({imports: [BrowserModule], declarations: []}) + export class AppModule { } + `; + const source = getTsSource(modulePath, moduleContent); + const changes = addExportToModule(source, modulePath, 'FooComponent', './foo.component'); + const output = applyChanges(modulePath, moduleContent, changes); + expect(output).toMatch(/import { FooComponent } from '.\/foo.component';/); + expect(output).toMatch(/exports: \[FooComponent\]/); + }); + it('should handle NgModule with no newlines', () => { const moduleContent = ` import { BrowserModule } from '@angular/platform-browser';