-
-
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.
refactor(router-store): change name for full router state serializer (#…
…3430) Closes #3416 BREAKING CHANGES: The full router state serializer has been renamed. BEFORE: The full router state serializer is named `DefaultRouterStateSerializer` AFTER: The full router state serializer is named `FullRouterStateSerializer`. A migration is provided to rename the export in affected projects.
- Loading branch information
1 parent
5abf828
commit d443f50
Showing
12 changed files
with
267 additions
and
51 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,77 @@ | ||
import { Tree } from '@angular-devkit/schematics'; | ||
import { | ||
SchematicTestRunner, | ||
UnitTestTree, | ||
} from '@angular-devkit/schematics/testing'; | ||
import * as path from 'path'; | ||
import { createPackageJson } from '@ngrx/schematics-core/testing/create-package'; | ||
import { waitForAsync } from '@angular/core/testing'; | ||
|
||
describe('Router Store Migration 14_0_0', () => { | ||
let appTree: UnitTestTree; | ||
const collectionPath = path.join(__dirname, '../migration.json'); | ||
const pkgName = 'router-store'; | ||
|
||
beforeEach(() => { | ||
appTree = new UnitTestTree(Tree.empty()); | ||
appTree.create( | ||
'/tsconfig.json', | ||
` | ||
{ | ||
"include": [**./*.ts"] | ||
} | ||
` | ||
); | ||
createPackageJson('', pkgName, appTree); | ||
}); | ||
|
||
describe('Rename serializers', () => { | ||
it( | ||
`should rename the DefaultRouterStateSerializer to FullRouterStateSerializer`, | ||
waitForAsync(async () => { | ||
const input = ` | ||
import { DefaultRouterStateSerializer } from '@ngrx/router-store'; | ||
const fullSerializer: DefaultRouterStateSerializer; | ||
@NgModule({ | ||
imports: [ | ||
AuthModule, | ||
AppRoutingModule, | ||
StoreRouterConnectingModule.forRoot({ serializer: DefaultRouterStateSerializer, key: 'router' }), | ||
CoreModule, | ||
], | ||
bootstrap: [AppComponent], | ||
}) | ||
export class AppModule {} | ||
`; | ||
const expected = ` | ||
import { FullRouterStateSerializer } from '@ngrx/router-store'; | ||
const fullSerializer: FullRouterStateSerializer; | ||
@NgModule({ | ||
imports: [ | ||
AuthModule, | ||
AppRoutingModule, | ||
StoreRouterConnectingModule.forRoot({ serializer: FullRouterStateSerializer, key: 'router' }), | ||
CoreModule, | ||
], | ||
bootstrap: [AppComponent], | ||
}) | ||
export class AppModule {} | ||
`; | ||
|
||
appTree.create('./app.module.ts', input); | ||
const runner = new SchematicTestRunner('schematics', collectionPath); | ||
|
||
const newTree = await runner | ||
.runSchematicAsync(`ngrx-${pkgName}-migration-04`, {}, appTree) | ||
.toPromise(); | ||
const file = newTree.readContent('app.module.ts'); | ||
|
||
expect(file).toBe(expected); | ||
}) | ||
); | ||
}); | ||
}); |
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,140 @@ | ||
import * as ts from 'typescript'; | ||
import { Rule, chain, Tree } from '@angular-devkit/schematics'; | ||
import { | ||
visitTSSourceFiles, | ||
commitChanges, | ||
createReplaceChange, | ||
ReplaceChange, | ||
} from '../../schematics-core'; | ||
|
||
const renames = { | ||
DefaultRouterStateSerializer: 'FullRouterStateSerializer', | ||
}; | ||
|
||
function renameSerializers() { | ||
return (tree: Tree) => { | ||
visitTSSourceFiles(tree, (sourceFile) => { | ||
const routerStoreImports = sourceFile.statements | ||
.filter(ts.isImportDeclaration) | ||
.filter(({ moduleSpecifier }) => | ||
moduleSpecifier.getText(sourceFile).includes('@ngrx/router-store') | ||
); | ||
|
||
if (routerStoreImports.length === 0) { | ||
return; | ||
} | ||
|
||
const changes = [ | ||
...findSerializerImportDeclarations(sourceFile, routerStoreImports), | ||
...findSerializerReplacements(sourceFile), | ||
]; | ||
|
||
commitChanges(tree, sourceFile.fileName, changes); | ||
}); | ||
}; | ||
} | ||
|
||
function findSerializerImportDeclarations( | ||
sourceFile: ts.SourceFile, | ||
imports: ts.ImportDeclaration[] | ||
) { | ||
const changes = imports | ||
.map((p) => (p?.importClause?.namedBindings as ts.NamedImports)?.elements) | ||
.reduce( | ||
(imports, curr) => imports.concat(curr ?? []), | ||
[] as ts.ImportSpecifier[] | ||
) | ||
.map((specifier) => { | ||
if (!ts.isImportSpecifier(specifier)) { | ||
return { hit: false }; | ||
} | ||
|
||
const serializerImports = Object.keys(renames); | ||
if (serializerImports.includes(specifier.name.text)) { | ||
return { hit: true, specifier, text: specifier.name.text }; | ||
} | ||
|
||
// if import is renamed | ||
if ( | ||
specifier.propertyName && | ||
serializerImports.includes(specifier.propertyName.text) | ||
) { | ||
return { hit: true, specifier, text: specifier.propertyName.text }; | ||
} | ||
|
||
return { hit: false }; | ||
}) | ||
.filter(({ hit }) => hit) | ||
.map(({ specifier, text }) => | ||
!!specifier && !!text | ||
? createReplaceChange( | ||
sourceFile, | ||
specifier, | ||
text, | ||
(renames as any)[text] | ||
) | ||
: undefined | ||
) | ||
.filter((change) => !!change) as Array<ReplaceChange>; | ||
|
||
return changes; | ||
} | ||
|
||
function findSerializerReplacements(sourceFile: ts.SourceFile) { | ||
const renameKeys = Object.keys(renames); | ||
const changes: ReplaceChange[] = []; | ||
ts.forEachChild(sourceFile, (node) => find(node, changes)); | ||
return changes; | ||
|
||
function find(node: ts.Node, changes: ReplaceChange[]) { | ||
let change = undefined; | ||
|
||
if ( | ||
ts.isPropertyAssignment(node) && | ||
renameKeys.includes(node.initializer.getText(sourceFile)) | ||
) { | ||
change = { | ||
node: node.initializer, | ||
text: node.initializer.getText(sourceFile), | ||
}; | ||
} | ||
|
||
if ( | ||
ts.isPropertyAccessExpression(node) && | ||
renameKeys.includes(node.expression.getText(sourceFile)) | ||
) { | ||
change = { | ||
node: node.expression, | ||
text: node.expression.getText(sourceFile), | ||
}; | ||
} | ||
|
||
if ( | ||
ts.isVariableDeclaration(node) && | ||
node.type && | ||
renameKeys.includes(node.type.getText(sourceFile)) | ||
) { | ||
change = { | ||
node: node.type, | ||
text: node.type.getText(sourceFile), | ||
}; | ||
} | ||
|
||
if (change) { | ||
changes.push( | ||
createReplaceChange( | ||
sourceFile, | ||
change.node, | ||
change.text, | ||
(renames as any)[change.text] | ||
) | ||
); | ||
} | ||
|
||
ts.forEachChild(node, (childNode) => find(childNode, changes)); | ||
} | ||
} | ||
|
||
export default function (): Rule { | ||
return chain([renameSerializers()]); | ||
} |
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
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
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
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
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
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
Oops, something went wrong.