Skip to content
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

fix(store): should not run schematics when not using named imports #2095

Merged
merged 1 commit into from
Aug 29, 2019
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
44 changes: 44 additions & 0 deletions modules/store/migrations/8_0_0-beta/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,4 +110,48 @@ describe('Store Migration 8_0_0 beta', () => {

expect(file).toBe(expected);
});

it(`should not run schematics when not using named imports`, () => {
const contents = `
import * as store from '@ngrx/store';

@NgModule({
imports: [
CommonModule,
BrowserModule,
BrowserAnimationsModule,
HttpClientModule,
AuthModule,
AppRoutingModule,
store.StoreModule.forRoot(reducers),
],
providers: [
{
provide: store.META_REDUCERS,
useValue: [fooReducer, barReducer]
}
]
bootstrap: [AppComponent],
})
export class AppModule {}
`;

appTree.create('./app.module.ts', contents);
const runner = new SchematicTestRunner('schematics', collectionPath);

let logs: string[] = [];
runner.logger.subscribe(log => logs.push(log.message));

const newTree = runner.runSchematic(
`ngrx-${pkgName}-migration-02`,
{},
appTree
);
const file = newTree.readContent('app.module.ts');

expect(file).toBe(contents);

expect(logs.length).toBe(1);
expect(logs[0]).toMatch(/NgRx 8 Migration: Unable to run the schematics/);
});
});
38 changes: 34 additions & 4 deletions modules/store/migrations/8_0_0-beta/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
import * as ts from 'typescript';
import { Rule, chain, Tree } from '@angular-devkit/schematics';
import { tags, logging } from '@angular-devkit/core';
import {
Rule,
chain,
Tree,
SchematicContext,
} from '@angular-devkit/schematics';
import {
ReplaceChange,
createReplaceChange,
Expand All @@ -10,7 +16,7 @@ import {
const META_REDUCERS = 'META_REDUCERS';

function updateMetaReducersToken(): Rule {
return (tree: Tree) => {
return (tree: Tree, context: SchematicContext) => {
visitTSSourceFiles(tree, sourceFile => {
const createChange = (node: ts.Node) =>
createReplaceChange(
Expand All @@ -22,7 +28,11 @@ function updateMetaReducersToken(): Rule {

const changes: ReplaceChange[] = [];
changes.push(
...findMetaReducersImportStatements(sourceFile, createChange)
...findMetaReducersImportStatements(
sourceFile,
createChange,
context.logger
)
);
changes.push(...findMetaReducersAssignment(sourceFile, createChange));

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

function findMetaReducersImportStatements(
sourceFile: ts.SourceFile,
createChange: (node: ts.Node) => ReplaceChange
createChange: (node: ts.Node) => ReplaceChange,
logger: logging.LoggerApi
) {
let canRunSchematics = false;

const metaReducerImports = sourceFile.statements
.filter(ts.isImportDeclaration)
.filter(isNgRxStoreImport)
.filter(p => {
canRunSchematics = Boolean(
p.importClause &&
p.importClause.namedBindings &&
(p.importClause!.namedBindings! as ts.NamedImports).elements
);
return canRunSchematics;
})
.map(p =>
(p.importClause!.namedBindings! as ts.NamedImports).elements.filter(
isMetaReducersImportSpecifier
Expand All @@ -50,6 +71,15 @@ function findMetaReducersImportStatements(
.reduce((imports, curr) => imports.concat(curr), []);

const changes = metaReducerImports.map(createChange);
if (!canRunSchematics && changes.length === 0) {
logger.info(tags.stripIndent`
NgRx 8 Migration: Unable to run the schematics to rename \`META_REDUCERS\` to \`USER_PROVIDED_META_REDUCERS\`
in file '${sourceFile.fileName}'.

For more info see https://ngrx.io/guide/migration/v8#meta_reducers-token.
`);
}

return changes;

function isNgRxStoreImport(importDeclaration: ts.ImportDeclaration) {
Expand Down