Skip to content

Commit b3c5931

Browse files
feat(store): add createFeature migration (#3759)
1 parent dd76c63 commit b3c5931

File tree

3 files changed

+133
-0
lines changed

3 files changed

+133
-0
lines changed
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
import { Tree } from '@angular-devkit/schematics';
2+
import {
3+
SchematicTestRunner,
4+
UnitTestTree,
5+
} from '@angular-devkit/schematics/testing';
6+
import * as path from 'path';
7+
8+
describe('Store Migration 15_2_0', () => {
9+
const collectionPath = path.join(__dirname, '../migration.json');
10+
const pkgName = 'store';
11+
12+
it(`should replace remove the State type argument`, async () => {
13+
const input = `
14+
import {createFeature} from '@ngrx/store';
15+
interface AppState {
16+
users: State;
17+
}
18+
export const usersFeature = createFeature<AppState>({
19+
name: 'users',
20+
reducer: createReducer(initialState, /* case reducers */),
21+
});
22+
`;
23+
24+
const expected = `
25+
import {createFeature} from '@ngrx/store';
26+
interface AppState {
27+
users: State;
28+
}
29+
export const usersFeature = createFeature({
30+
name: 'users',
31+
reducer: createReducer(initialState, /* case reducers */),
32+
});
33+
`;
34+
const appTree = new UnitTestTree(Tree.empty());
35+
appTree.create('./fixture.ts', input);
36+
const runner = new SchematicTestRunner('schematics', collectionPath);
37+
38+
const newTree = await runner
39+
.runSchematicAsync(`ngrx-${pkgName}-migration-15-2-0`, {}, appTree)
40+
.toPromise();
41+
const file = newTree.readContent('fixture.ts');
42+
43+
expect(file).toBe(expected);
44+
});
45+
46+
it(`should not update createFeature when correctly used`, async () => {
47+
const input = `
48+
import {createFeature} from '@ngrx/store';
49+
export const usersFeature = createFeature({
50+
name: 'users',
51+
reducer: createReducer(initialState, /* case reducers */),
52+
});
53+
`;
54+
55+
const appTree = new UnitTestTree(Tree.empty());
56+
appTree.create('./fixture.ts', input);
57+
const runner = new SchematicTestRunner('schematics', collectionPath);
58+
59+
const newTree = await runner
60+
.runSchematicAsync(`ngrx-${pkgName}-migration-15-2-0`, {}, appTree)
61+
.toPromise();
62+
const file = newTree.readContent('fixture.ts');
63+
64+
expect(file).toBe(input);
65+
});
66+
});
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
import * as ts from 'typescript';
2+
import { Rule, chain, Tree } from '@angular-devkit/schematics';
3+
import {
4+
visitTSSourceFiles,
5+
RemoveChange,
6+
commitChanges,
7+
InsertChange,
8+
} from '../../schematics-core';
9+
10+
function updatecreateFeature(): Rule {
11+
return (tree: Tree) => {
12+
visitTSSourceFiles(tree, (sourceFile) => {
13+
const runMigration = sourceFile.statements
14+
.filter(ts.isImportDeclaration)
15+
.filter(
16+
(importDeclaration) =>
17+
importDeclaration.moduleSpecifier.getText(sourceFile) ===
18+
"'@ngrx/store'" ||
19+
importDeclaration.moduleSpecifier.getText(sourceFile) ===
20+
'"@ngrx/store"'
21+
)
22+
.some((importDeclaration) => {
23+
return importDeclaration.importClause?.namedBindings
24+
?.getText(sourceFile)
25+
.includes('createFeature');
26+
});
27+
28+
if (!runMigration) return;
29+
30+
const changes: (RemoveChange | InsertChange)[] = [];
31+
ts.forEachChild(sourceFile, crawl);
32+
return commitChanges(tree, sourceFile.fileName, changes);
33+
34+
function crawl(node: ts.Node) {
35+
ts.forEachChild(node, crawl);
36+
37+
if (!ts.isCallExpression(node)) return;
38+
39+
const { typeArguments } = node;
40+
41+
if (!typeArguments?.length) return;
42+
43+
if (!ts.isIdentifier(node.expression)) return;
44+
if (node.expression.text !== 'createFeature') return;
45+
46+
changes.push(
47+
new RemoveChange(
48+
sourceFile.fileName,
49+
// to include <
50+
typeArguments.pos - 1,
51+
// to include >
52+
typeArguments.end + 1
53+
)
54+
);
55+
}
56+
});
57+
};
58+
}
59+
60+
export default function (): Rule {
61+
return chain([updatecreateFeature]);
62+
}

modules/store/migrations/migration.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,11 @@
2525
"description": "The road to v13 RC",
2626
"version": "13-rc.1",
2727
"factory": "./13_0_0-rc/index"
28+
},
29+
"ngrx-store-migration-15-2-0": {
30+
"description": "The road to v15.2",
31+
"version": "15.2.0",
32+
"factory": "./15_2_0/index"
2833
}
2934
}
3035
}

0 commit comments

Comments
 (0)