Skip to content

Commit 22d7728

Browse files
Alanmgechev
Alan
authored andcommitted
fix(@schematics/angular): handle newline after @ of a decorator
Fixes #14490
1 parent 2b777e4 commit 22d7728

File tree

2 files changed

+18
-3
lines changed

2 files changed

+18
-3
lines changed

packages/schematics/angular/utility/ast-utils.ts

+2-3
Original file line numberDiff line numberDiff line change
@@ -290,8 +290,7 @@ export function getDecoratorMetadata(source: ts.SourceFile, identifier: string,
290290
if (expr.expression.kind == ts.SyntaxKind.Identifier) {
291291
const id = expr.expression as ts.Identifier;
292292

293-
return id.getFullText(source) == identifier
294-
&& angularImports[id.getFullText(source)] === module;
293+
return id.text == identifier && angularImports[id.text] === module;
295294
} else if (expr.expression.kind == ts.SyntaxKind.PropertyAccessExpression) {
296295
// This covers foo.NgModule when importing * as foo.
297296
const paExpr = expr.expression as ts.PropertyAccessExpression;
@@ -301,7 +300,7 @@ export function getDecoratorMetadata(source: ts.SourceFile, identifier: string,
301300
}
302301

303302
const id = paExpr.name.text;
304-
const moduleId = (paExpr.expression as ts.Identifier).getText(source);
303+
const moduleId = (paExpr.expression as ts.Identifier).text;
305304

306305
return id === identifier && (angularImports[moduleId + '.'] === module);
307306
}

packages/schematics/angular/utility/ast-utils_spec.ts

+16
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,22 @@ describe('ast utils', () => {
169169
expect(output).toMatch(/imports: \[HelloWorld],\r?\n/m);
170170
});
171171

172+
it(`should handle NgModule with newline after '@'`, () => {
173+
const moduleContent = `
174+
import { BrowserModule } from '@angular/platform-browser';
175+
import { NgModule } from '@angular/core';
176+
177+
@
178+
NgModule({imports: [BrowserModule], declarations: []})
179+
export class AppModule { }
180+
`;
181+
const source = getTsSource(modulePath, moduleContent);
182+
const changes = addExportToModule(source, modulePath, 'FooComponent', './foo.component');
183+
const output = applyChanges(modulePath, moduleContent, changes);
184+
expect(output).toMatch(/import { FooComponent } from '.\/foo.component';/);
185+
expect(output).toMatch(/exports: \[FooComponent\]/);
186+
});
187+
172188
it('should handle NgModule with no newlines', () => {
173189
const moduleContent = `
174190
import { BrowserModule } from '@angular/platform-browser';

0 commit comments

Comments
 (0)