Skip to content

Commit d9261f2

Browse files
Alanmgechev
authored andcommitted
fix(@schematics/angular): handle newline after @ of a decorator
Fixes #14490
1 parent 11631b5 commit d9261f2

File tree

2 files changed

+18
-3
lines changed

2 files changed

+18
-3
lines changed

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -286,8 +286,7 @@ export function getDecoratorMetadata(source: ts.SourceFile, identifier: string,
286286
if (expr.expression.kind == ts.SyntaxKind.Identifier) {
287287
const id = expr.expression as ts.Identifier;
288288

289-
return id.getFullText(source) == identifier
290-
&& angularImports[id.getFullText(source)] === module;
289+
return id.text == identifier && angularImports[id.text] === module;
291290
} else if (expr.expression.kind == ts.SyntaxKind.PropertyAccessExpression) {
292291
// This covers foo.NgModule when importing * as foo.
293292
const paExpr = expr.expression as ts.PropertyAccessExpression;
@@ -297,7 +296,7 @@ export function getDecoratorMetadata(source: ts.SourceFile, identifier: string,
297296
}
298297

299298
const id = paExpr.name.text;
300-
const moduleId = (paExpr.expression as ts.Identifier).getText(source);
299+
const moduleId = (paExpr.expression as ts.Identifier).text;
301300

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

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

Lines changed: 16 additions & 0 deletions
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)