Skip to content

Commit

Permalink
feat(store): add createFeature migration (#3759)
Browse files Browse the repository at this point in the history
  • Loading branch information
timdeschryver authored Jan 26, 2023
1 parent dd76c63 commit b3c5931
Show file tree
Hide file tree
Showing 3 changed files with 133 additions and 0 deletions.
66 changes: 66 additions & 0 deletions modules/store/migrations/15_2_0/index.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import { Tree } from '@angular-devkit/schematics';
import {
SchematicTestRunner,
UnitTestTree,
} from '@angular-devkit/schematics/testing';
import * as path from 'path';

describe('Store Migration 15_2_0', () => {
const collectionPath = path.join(__dirname, '../migration.json');
const pkgName = 'store';

it(`should replace remove the State type argument`, async () => {
const input = `
import {createFeature} from '@ngrx/store';
interface AppState {
users: State;
}
export const usersFeature = createFeature<AppState>({
name: 'users',
reducer: createReducer(initialState, /* case reducers */),
});
`;

const expected = `
import {createFeature} from '@ngrx/store';
interface AppState {
users: State;
}
export const usersFeature = createFeature({
name: 'users',
reducer: createReducer(initialState, /* case reducers */),
});
`;
const appTree = new UnitTestTree(Tree.empty());
appTree.create('./fixture.ts', input);
const runner = new SchematicTestRunner('schematics', collectionPath);

const newTree = await runner
.runSchematicAsync(`ngrx-${pkgName}-migration-15-2-0`, {}, appTree)
.toPromise();
const file = newTree.readContent('fixture.ts');

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

it(`should not update createFeature when correctly used`, async () => {
const input = `
import {createFeature} from '@ngrx/store';
export const usersFeature = createFeature({
name: 'users',
reducer: createReducer(initialState, /* case reducers */),
});
`;

const appTree = new UnitTestTree(Tree.empty());
appTree.create('./fixture.ts', input);
const runner = new SchematicTestRunner('schematics', collectionPath);

const newTree = await runner
.runSchematicAsync(`ngrx-${pkgName}-migration-15-2-0`, {}, appTree)
.toPromise();
const file = newTree.readContent('fixture.ts');

expect(file).toBe(input);
});
});
62 changes: 62 additions & 0 deletions modules/store/migrations/15_2_0/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import * as ts from 'typescript';
import { Rule, chain, Tree } from '@angular-devkit/schematics';
import {
visitTSSourceFiles,
RemoveChange,
commitChanges,
InsertChange,
} from '../../schematics-core';

function updatecreateFeature(): Rule {
return (tree: Tree) => {
visitTSSourceFiles(tree, (sourceFile) => {
const runMigration = sourceFile.statements
.filter(ts.isImportDeclaration)
.filter(
(importDeclaration) =>
importDeclaration.moduleSpecifier.getText(sourceFile) ===
"'@ngrx/store'" ||
importDeclaration.moduleSpecifier.getText(sourceFile) ===
'"@ngrx/store"'
)
.some((importDeclaration) => {
return importDeclaration.importClause?.namedBindings
?.getText(sourceFile)
.includes('createFeature');
});

if (!runMigration) return;

const changes: (RemoveChange | InsertChange)[] = [];
ts.forEachChild(sourceFile, crawl);
return commitChanges(tree, sourceFile.fileName, changes);

function crawl(node: ts.Node) {
ts.forEachChild(node, crawl);

if (!ts.isCallExpression(node)) return;

const { typeArguments } = node;

if (!typeArguments?.length) return;

if (!ts.isIdentifier(node.expression)) return;
if (node.expression.text !== 'createFeature') return;

changes.push(
new RemoveChange(
sourceFile.fileName,
// to include <
typeArguments.pos - 1,
// to include >
typeArguments.end + 1
)
);
}
});
};
}

export default function (): Rule {
return chain([updatecreateFeature]);
}
5 changes: 5 additions & 0 deletions modules/store/migrations/migration.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@
"description": "The road to v13 RC",
"version": "13-rc.1",
"factory": "./13_0_0-rc/index"
},
"ngrx-store-migration-15-2-0": {
"description": "The road to v15.2",
"version": "15.2.0",
"factory": "./15_2_0/index"
}
}
}

0 comments on commit b3c5931

Please sign in to comment.