Skip to content

fix(@schematics/angular): retain trailing comma when adding providers to app config #26929

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions packages/schematics/angular/utility/standalone/rules.ts
Original file line number Diff line number Diff line change
Expand Up @@ -231,12 +231,10 @@ function addProvidersExpressionToAppConfig(
// If there's a `providers` property, we can add the provider
// to it, otherwise we need to declare it ourselves.
if (providersLiteral) {
const hasTrailingComma = providersLiteral.elements.hasTrailingComma;

applyChangesToFile(tree, filePath, [
insertAfterLastOccurrence(
providersLiteral.elements,
(hasTrailingComma || providersLiteral.elements.length === 0 ? '' : ', ') + expression,
(providersLiteral.elements.length === 0 ? '' : ', ') + expression,
filePath,
providersLiteral.getStart() + 1,
),
Expand Down
31 changes: 31 additions & 0 deletions packages/schematics/angular/utility/standalone/rules_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -445,5 +445,36 @@ describe('standalone utilities', () => {
assertContains(content, `import { provideModule } from '@my/module';`);
assertContains(content, `providers: [provideModule([])]`);
});

it('should add a root provider to a standalone app when providers contain a trailing comma', async () => {
await setupProject(true);

const configPath = 'app/app.config.ts';
host.overwrite(
getPathWithinProject(configPath),
`
import { ApplicationConfig } from '@angular/core';
import { provideRouter } from '@angular/router';

export const appConfig: ApplicationConfig = {
providers: [
provideRouter([]),
]
};
`,
);

await testRule(
addRootProvider(
projectName,
({ code, external }) => code`${external('provideModule', '@my/module')}([])`,
),
host,
);

const content = readFile('app/app.config.ts');
assertContains(content, `import { provideModule } from '@my/module';`);
assertContains(content, `providers: [provideRouter([]),provideModule([]),]`);
});
});
});