-
-
Notifications
You must be signed in to change notification settings - Fork 2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(store): add createFeature migration (#3759)
- Loading branch information
1 parent
dd76c63
commit b3c5931
Showing
3 changed files
with
133 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters