Skip to content

Commit 35ebf1e

Browse files
committed
fix(@schematics/angular): retain trailing comma when adding providers to app config
This fixes an issue which caused the new provider to be added in the position of the trailing comma. With this change the trailing comma is retained. Closes #26911 (cherry picked from commit 90363dd)
1 parent 45dea6f commit 35ebf1e

File tree

2 files changed

+32
-3
lines changed

2 files changed

+32
-3
lines changed

packages/schematics/angular/utility/standalone/rules.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -231,12 +231,10 @@ function addProvidersExpressionToAppConfig(
231231
// If there's a `providers` property, we can add the provider
232232
// to it, otherwise we need to declare it ourselves.
233233
if (providersLiteral) {
234-
const hasTrailingComma = providersLiteral.elements.hasTrailingComma;
235-
236234
applyChangesToFile(tree, filePath, [
237235
insertAfterLastOccurrence(
238236
providersLiteral.elements,
239-
(hasTrailingComma || providersLiteral.elements.length === 0 ? '' : ', ') + expression,
237+
(providersLiteral.elements.length === 0 ? '' : ', ') + expression,
240238
filePath,
241239
providersLiteral.getStart() + 1,
242240
),

packages/schematics/angular/utility/standalone/rules_spec.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -445,5 +445,36 @@ describe('standalone utilities', () => {
445445
assertContains(content, `import { provideModule } from '@my/module';`);
446446
assertContains(content, `providers: [provideModule([])]`);
447447
});
448+
449+
it('should add a root provider to a standalone app when providers contain a trailing comma', async () => {
450+
await setupProject(true);
451+
452+
const configPath = 'app/app.config.ts';
453+
host.overwrite(
454+
getPathWithinProject(configPath),
455+
`
456+
import { ApplicationConfig } from '@angular/core';
457+
import { provideRouter } from '@angular/router';
458+
459+
export const appConfig: ApplicationConfig = {
460+
providers: [
461+
provideRouter([]),
462+
]
463+
};
464+
`,
465+
);
466+
467+
await testRule(
468+
addRootProvider(
469+
projectName,
470+
({ code, external }) => code`${external('provideModule', '@my/module')}([])`,
471+
),
472+
host,
473+
);
474+
475+
const content = readFile('app/app.config.ts');
476+
assertContains(content, `import { provideModule } from '@my/module';`);
477+
assertContains(content, `providers: [provideRouter([]),provideModule([]),]`);
478+
});
448479
});
449480
});

0 commit comments

Comments
 (0)