Skip to content

Commit f2d4ebc

Browse files
fix(schematics): add comma before devtools for empty imports (#2542)
1 parent de02aa7 commit f2d4ebc

File tree

2 files changed

+41
-23
lines changed

2 files changed

+41
-23
lines changed

modules/schematics/src/store/index.spec.ts

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,7 @@ describe('Store Schematic', () => {
6060
expect(content).not.toMatch(
6161
/import { reducers, metaReducers } from '\.\/reducers';/
6262
);
63-
expect(content).toMatch(/StoreModule.forRoot\({}/);
64-
63+
expect(content).toMatch(/StoreModule.forRoot\({}\),/);
6564
expect(files.indexOf(`${projectPath}/src/app/reducers/index.ts`)).toBe(-1);
6665
});
6766

@@ -301,13 +300,32 @@ describe('Store Schematic', () => {
301300
expect(content).not.toMatch(/fooFeatureKey = 'foo'/);
302301
});
303302

304-
it('should add store runtime checks', () => {
305-
const options = { ...defaultOptions, module: 'app.module.ts' };
303+
it('should add the initial config correctly into an empty module', async () => {
304+
const options = {
305+
...defaultOptions,
306+
module: 'empty.module.ts',
307+
};
306308

307-
const tree = schematicRunner.runSchematic('store', options, appTree);
308-
const content = tree.readContent(`${projectPath}/src/app/app.module.ts`);
309-
expect(content).toMatch(/runtimeChecks: {/);
310-
expect(content).toMatch(/strictStateImmutability: true,/);
311-
expect(content).toMatch(/strictActionImmutability: true/);
309+
appTree.create(
310+
`${projectPath}/src/app/empty.module.ts`,
311+
`
312+
import { NgModule } from '@angular/core';
313+
314+
@NgModule({
315+
declarations: [],
316+
imports: [],
317+
})
318+
export class EmptyModule {}
319+
`
320+
);
321+
322+
const tree = await schematicRunner
323+
.runSchematicAsync('store', options, appTree)
324+
.toPromise();
325+
const content = tree.readContent(`${projectPath}/src/app/empty.module.ts`);
326+
327+
expect(content).toMatch(
328+
/imports: \[StoreModule.forRoot\(reducers, { metaReducers }\), !environment.production \? StoreDevtoolsModule.instrument\(\) : \[\]\]/
329+
);
312330
});
313331
});

modules/schematics/src/store/index.ts

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import {
2727
findModuleFromOptions,
2828
addImportToModule,
2929
parseName,
30+
visitNgModuleImports,
3031
} from '@ngrx/schematics/schematics-core';
3132
import { Schema as StoreOptions } from './schema';
3233

@@ -63,25 +64,14 @@ function addImportToNgModule(options: StoreOptions): Rule {
6364
`${options.path}/environments/environment`
6465
);
6566

66-
const runtimeChecks = `
67-
runtimeChecks: {
68-
strictStateImmutability: true,
69-
strictActionImmutability: true,
70-
}
71-
`;
72-
7367
const rootStoreReducers = options.minimal ? `{}` : `reducers`;
74-
75-
const rootStoreConfig = options.minimal
76-
? `{ ${runtimeChecks} }`
77-
: `{
78-
metaReducers, ${runtimeChecks} }`;
68+
const rootStoreConfig = options.minimal ? `` : `, { metaReducers }`;
7969

8070
const storeNgModuleImport = addImportToModule(
8171
source,
8272
modulePath,
8373
options.root
84-
? `StoreModule.forRoot(${rootStoreReducers}, ${rootStoreConfig})`
74+
? `StoreModule.forRoot(${rootStoreReducers}${rootStoreConfig})`
8575
: `StoreModule.forFeature(from${stringUtils.classify(
8676
options.name
8777
)}.${stringUtils.camelize(
@@ -123,10 +113,20 @@ function addImportToNgModule(options: StoreOptions): Rule {
123113
let rootImports: (Change | undefined)[] = [];
124114

125115
if (options.root) {
116+
let hasImports = false;
117+
visitNgModuleImports(source, (_, importNodes) => {
118+
hasImports = importNodes.length > 0;
119+
});
120+
121+
// `addImportToModule` adds a comma to imports when there are already imports present
122+
// because at this time the store import hasn't been committed yet, `addImportToModule` wont add a comma
123+
// so we have to add it here for empty import arrays
124+
let adjectiveComma = hasImports ? '' : ', ';
125+
126126
const storeDevtoolsNgModuleImport = addImportToModule(
127127
source,
128128
modulePath,
129-
`!environment.production ? StoreDevtoolsModule.instrument() : []`,
129+
`${adjectiveComma}!environment.production ? StoreDevtoolsModule.instrument() : []`,
130130
relativePath
131131
).shift();
132132

0 commit comments

Comments
 (0)