Skip to content

Commit 7cadbc0

Browse files
timdeschryverbrandonroberts
authored andcommitted
fix(store): should not run schematics when not using named imports (#2095)
Closes #2093
1 parent 7445dd4 commit 7cadbc0

File tree

2 files changed

+78
-4
lines changed

2 files changed

+78
-4
lines changed

modules/store/migrations/8_0_0-beta/index.spec.ts

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,4 +110,48 @@ describe('Store Migration 8_0_0 beta', () => {
110110

111111
expect(file).toBe(expected);
112112
});
113+
114+
it(`should not run schematics when not using named imports`, () => {
115+
const contents = `
116+
import * as store from '@ngrx/store';
117+
118+
@NgModule({
119+
imports: [
120+
CommonModule,
121+
BrowserModule,
122+
BrowserAnimationsModule,
123+
HttpClientModule,
124+
AuthModule,
125+
AppRoutingModule,
126+
store.StoreModule.forRoot(reducers),
127+
],
128+
providers: [
129+
{
130+
provide: store.META_REDUCERS,
131+
useValue: [fooReducer, barReducer]
132+
}
133+
]
134+
bootstrap: [AppComponent],
135+
})
136+
export class AppModule {}
137+
`;
138+
139+
appTree.create('./app.module.ts', contents);
140+
const runner = new SchematicTestRunner('schematics', collectionPath);
141+
142+
let logs: string[] = [];
143+
runner.logger.subscribe(log => logs.push(log.message));
144+
145+
const newTree = runner.runSchematic(
146+
`ngrx-${pkgName}-migration-02`,
147+
{},
148+
appTree
149+
);
150+
const file = newTree.readContent('app.module.ts');
151+
152+
expect(file).toBe(contents);
153+
154+
expect(logs.length).toBe(1);
155+
expect(logs[0]).toMatch(/NgRx 8 Migration: Unable to run the schematics/);
156+
});
113157
});

modules/store/migrations/8_0_0-beta/index.ts

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
import * as ts from 'typescript';
2-
import { Rule, chain, Tree } from '@angular-devkit/schematics';
2+
import { tags, logging } from '@angular-devkit/core';
3+
import {
4+
Rule,
5+
chain,
6+
Tree,
7+
SchematicContext,
8+
} from '@angular-devkit/schematics';
39
import {
410
ReplaceChange,
511
createReplaceChange,
@@ -10,7 +16,7 @@ import {
1016
const META_REDUCERS = 'META_REDUCERS';
1117

1218
function updateMetaReducersToken(): Rule {
13-
return (tree: Tree) => {
19+
return (tree: Tree, context: SchematicContext) => {
1420
visitTSSourceFiles(tree, sourceFile => {
1521
const createChange = (node: ts.Node) =>
1622
createReplaceChange(
@@ -22,7 +28,11 @@ function updateMetaReducersToken(): Rule {
2228

2329
const changes: ReplaceChange[] = [];
2430
changes.push(
25-
...findMetaReducersImportStatements(sourceFile, createChange)
31+
...findMetaReducersImportStatements(
32+
sourceFile,
33+
createChange,
34+
context.logger
35+
)
2636
);
2737
changes.push(...findMetaReducersAssignment(sourceFile, createChange));
2838

@@ -37,11 +47,22 @@ export default function(): Rule {
3747

3848
function findMetaReducersImportStatements(
3949
sourceFile: ts.SourceFile,
40-
createChange: (node: ts.Node) => ReplaceChange
50+
createChange: (node: ts.Node) => ReplaceChange,
51+
logger: logging.LoggerApi
4152
) {
53+
let canRunSchematics = false;
54+
4255
const metaReducerImports = sourceFile.statements
4356
.filter(ts.isImportDeclaration)
4457
.filter(isNgRxStoreImport)
58+
.filter(p => {
59+
canRunSchematics = Boolean(
60+
p.importClause &&
61+
p.importClause.namedBindings &&
62+
(p.importClause!.namedBindings! as ts.NamedImports).elements
63+
);
64+
return canRunSchematics;
65+
})
4566
.map(p =>
4667
(p.importClause!.namedBindings! as ts.NamedImports).elements.filter(
4768
isMetaReducersImportSpecifier
@@ -50,6 +71,15 @@ function findMetaReducersImportStatements(
5071
.reduce((imports, curr) => imports.concat(curr), []);
5172

5273
const changes = metaReducerImports.map(createChange);
74+
if (!canRunSchematics && changes.length === 0) {
75+
logger.info(tags.stripIndent`
76+
NgRx 8 Migration: Unable to run the schematics to rename \`META_REDUCERS\` to \`USER_PROVIDED_META_REDUCERS\`
77+
in file '${sourceFile.fileName}'.
78+
79+
For more info see https://ngrx.io/guide/migration/v8#meta_reducers-token.
80+
`);
81+
}
82+
5383
return changes;
5484

5585
function isNgRxStoreImport(importDeclaration: ts.ImportDeclaration) {

0 commit comments

Comments
 (0)