Skip to content

Commit

Permalink
fix(@schematics/angular): handle newline after @ of a decorator
Browse files Browse the repository at this point in the history
Fixes #14490
  • Loading branch information
Alan authored and mgechev committed May 23, 2019
1 parent 11631b5 commit d9261f2
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 3 deletions.
5 changes: 2 additions & 3 deletions packages/schematics/angular/utility/ast-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
}
Expand Down
16 changes: 16 additions & 0 deletions packages/schematics/angular/utility/ast-utils_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down

0 comments on commit d9261f2

Please sign in to comment.