Skip to content

Commit 65dac11

Browse files
alan-agius4filipesilva
authored andcommitted
fix(@schematics/angular): correctly handle PropertyAssignments with StringLiteral keys
Closes #16009
1 parent 1df8a3d commit 65dac11

File tree

2 files changed

+24
-2
lines changed

2 files changed

+24
-2
lines changed

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -377,8 +377,7 @@ export function getMetadataField(
377377
// Filter out every fields that's not "metadataField". Also handles string literals
378378
// (but not expressions).
379379
.filter(({ name }) => {
380-
return (ts.isIdentifier(name) || ts.isStringLiteral(name))
381-
&& name.getText() === metadataField;
380+
return (ts.isIdentifier(name) || ts.isStringLiteral(name)) && name.text === metadataField;
382381
});
383382
}
384383

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

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,29 @@ describe('ast utils', () => {
9191
expect(output).toMatch(/declarations: \[\nAppComponent,\nFooComponent\n\]/);
9292
});
9393

94+
it('should add declarations to module when PropertyAssignment is StringLiteral', () => {
95+
moduleContent = tags.stripIndents`
96+
import { BrowserModule } from '@angular/platform-browser';
97+
import { NgModule } from '@angular/core';
98+
import { AppComponent } from './app.component';
99+
100+
@NgModule({
101+
"declarations": [
102+
AppComponent
103+
],
104+
"imports": [
105+
BrowserModule
106+
],
107+
"providers": [],
108+
"bootstrap": [AppComponent]
109+
})`;
110+
const source = getTsSource(modulePath, moduleContent);
111+
const changes = addDeclarationToModule(source, modulePath, 'FooComponent', './foo.component');
112+
const output = applyChanges(modulePath, moduleContent, changes);
113+
expect(output).toMatch(/import { FooComponent } from '.\/foo.component';/);
114+
expect(output).toMatch(/"declarations": \[\nAppComponent,\nFooComponent\n\]/);
115+
});
116+
94117
it('should add metadata', () => {
95118
const source = getTsSource(modulePath, moduleContent);
96119
const changes = addSymbolToNgModuleMetadata(source, modulePath, 'imports', 'HelloWorld');

0 commit comments

Comments
 (0)