From a385a7862dcbf309000b9ef925a804153c40ed95 Mon Sep 17 00:00:00 2001 From: timdeschryver <28659384+timdeschryver@users.noreply.github.com> Date: Thu, 28 May 2020 19:02:43 +0200 Subject: [PATCH] fix(schematics): add comma before devtools for empty imports --- modules/schematics/src/store/index.spec.ts | 36 ++++++++++++++++------ modules/schematics/src/store/index.ts | 28 ++++++++--------- 2 files changed, 41 insertions(+), 23 deletions(-) diff --git a/modules/schematics/src/store/index.spec.ts b/modules/schematics/src/store/index.spec.ts index bd8cd07ede..040664d747 100644 --- a/modules/schematics/src/store/index.spec.ts +++ b/modules/schematics/src/store/index.spec.ts @@ -60,8 +60,7 @@ describe('Store Schematic', () => { expect(content).not.toMatch( /import { reducers, metaReducers } from '\.\/reducers';/ ); - expect(content).toMatch(/StoreModule.forRoot\({}/); - + expect(content).toMatch(/StoreModule.forRoot\({}\),/); expect(files.indexOf(`${projectPath}/src/app/reducers/index.ts`)).toBe(-1); }); @@ -301,13 +300,32 @@ describe('Store Schematic', () => { expect(content).not.toMatch(/fooFeatureKey = 'foo'/); }); - it('should add store runtime checks', () => { - const options = { ...defaultOptions, module: 'app.module.ts' }; + it('should add the initial config correctly into an empty module', async () => { + const options = { + ...defaultOptions, + module: 'empty.module.ts', + }; - const tree = schematicRunner.runSchematic('store', options, appTree); - const content = tree.readContent(`${projectPath}/src/app/app.module.ts`); - expect(content).toMatch(/runtimeChecks: {/); - expect(content).toMatch(/strictStateImmutability: true,/); - expect(content).toMatch(/strictActionImmutability: true/); + appTree.create( + `${projectPath}/src/app/empty.module.ts`, + ` + import { NgModule } from '@angular/core'; + + @NgModule({ + declarations: [], + imports: [], + }) + export class EmptyModule {} + ` + ); + + const tree = await schematicRunner + .runSchematicAsync('store', options, appTree) + .toPromise(); + const content = tree.readContent(`${projectPath}/src/app/empty.module.ts`); + + expect(content).toMatch( + /imports: \[StoreModule.forRoot\(reducers, { metaReducers }\), !environment.production \? StoreDevtoolsModule.instrument\(\) : \[\]\]/ + ); }); }); diff --git a/modules/schematics/src/store/index.ts b/modules/schematics/src/store/index.ts index b704720d53..70d910bb28 100644 --- a/modules/schematics/src/store/index.ts +++ b/modules/schematics/src/store/index.ts @@ -27,6 +27,7 @@ import { findModuleFromOptions, addImportToModule, parseName, + visitNgModuleImports, } from '@ngrx/schematics/schematics-core'; import { Schema as StoreOptions } from './schema'; @@ -63,25 +64,14 @@ function addImportToNgModule(options: StoreOptions): Rule { `${options.path}/environments/environment` ); - const runtimeChecks = ` - runtimeChecks: { - strictStateImmutability: true, - strictActionImmutability: true, - } - `; - const rootStoreReducers = options.minimal ? `{}` : `reducers`; - - const rootStoreConfig = options.minimal - ? `{ ${runtimeChecks} }` - : `{ - metaReducers, ${runtimeChecks} }`; + const rootStoreConfig = options.minimal ? `` : `, { metaReducers }`; const storeNgModuleImport = addImportToModule( source, modulePath, options.root - ? `StoreModule.forRoot(${rootStoreReducers}, ${rootStoreConfig})` + ? `StoreModule.forRoot(${rootStoreReducers}${rootStoreConfig})` : `StoreModule.forFeature(from${stringUtils.classify( options.name )}.${stringUtils.camelize( @@ -123,10 +113,20 @@ function addImportToNgModule(options: StoreOptions): Rule { let rootImports: (Change | undefined)[] = []; if (options.root) { + let hasImports = false; + visitNgModuleImports(source, (_, importNodes) => { + hasImports = importNodes.length > 0; + }); + + // `addImportToModule` adds a comma to imports when there are already imports present + // because at this time the store import hasn't been committed yet, `addImportToModule` wont add a comma + // so we have to add it here for empty import arrays + let adjectiveComma = hasImports ? '' : ', '; + const storeDevtoolsNgModuleImport = addImportToModule( source, modulePath, - `!environment.production ? StoreDevtoolsModule.instrument() : []`, + `${adjectiveComma}!environment.production ? StoreDevtoolsModule.instrument() : []`, relativePath ).shift();