Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(migrations): Schematics for
TransferState
, StateKey
and `mak…
…eStateKey` migration. (#49594) These 3 classes have been moved from platform-browser to core by #49563 PR Close #49594
- Loading branch information
Showing
10 changed files
with
332 additions
and
8 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
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
33 changes: 33 additions & 0 deletions
33
packages/core/schematics/migrations/transfer-state/BUILD.bazel
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,33 @@ | ||
load("//tools:defaults.bzl", "esbuild", "ts_library") | ||
|
||
package( | ||
default_visibility = [ | ||
"//packages/core/schematics:__pkg__", | ||
"//packages/core/schematics/migrations/google3:__pkg__", | ||
"//packages/core/schematics/test:__pkg__", | ||
], | ||
) | ||
|
||
ts_library( | ||
name = "transfer-state", | ||
srcs = glob(["**/*.ts"]), | ||
tsconfig = "//packages/core/schematics:tsconfig.json", | ||
deps = [ | ||
"//packages/core/schematics/utils", | ||
"@npm//@angular-devkit/schematics", | ||
"@npm//@types/node", | ||
"@npm//typescript", | ||
], | ||
) | ||
|
||
esbuild( | ||
name = "bundle", | ||
entry_point = ":index.ts", | ||
external = [ | ||
"@angular-devkit/*", | ||
"typescript", | ||
], | ||
format = "cjs", | ||
platform = "node", | ||
deps = [":transfer-state"], | ||
) |
19 changes: 19 additions & 0 deletions
19
packages/core/schematics/migrations/transfer-state/README.md
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,19 @@ | ||
## Import TransferState-related symbols from `@angular/core` | ||
|
||
The following symbols were moved from `@angular/platform-browser` to `@angular/core`: | ||
|
||
* `TransferState` | ||
* `makeStateKey` | ||
* `StateKey` | ||
|
||
This migration updates symbol imports to use `@angular/core`. | ||
|
||
#### Before | ||
```ts | ||
import { TransferState, makeStateKey, StateKey } from '@angular/platform-browser'; | ||
``` | ||
|
||
#### After | ||
```ts | ||
import { TransferState, makeStateKey, StateKey } from '@angular/core'; | ||
``` |
59 changes: 59 additions & 0 deletions
59
packages/core/schematics/migrations/transfer-state/index.ts
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,59 @@ | ||
/** | ||
* @license | ||
* Copyright Google LLC All Rights Reserved. | ||
* | ||
* Use of this source code is governed by an MIT-style license that can be | ||
* found in the LICENSE file at https://angular.io/license | ||
*/ | ||
import {Rule, SchematicsException, Tree, UpdateRecorder} from '@angular-devkit/schematics'; | ||
import {relative} from 'path'; | ||
|
||
import {getProjectTsConfigPaths} from '../../utils/project_tsconfig_paths'; | ||
import {canMigrateFile, createMigrationProgram} from '../../utils/typescript/compiler_host'; | ||
|
||
import {migrateFile} from './utils'; | ||
|
||
|
||
export default function(): Rule { | ||
return async (tree: Tree) => { | ||
const {buildPaths, testPaths} = await getProjectTsConfigPaths(tree); | ||
const basePath = process.cwd(); | ||
const allPaths = [...buildPaths, ...testPaths]; | ||
|
||
if (!allPaths.length) { | ||
throw new SchematicsException( | ||
'Could not find any tsconfig file. Cannot run the transfer state migration.'); | ||
} | ||
|
||
for (const tsconfigPath of allPaths) { | ||
runMigration(tree, tsconfigPath, basePath); | ||
} | ||
}; | ||
} | ||
|
||
|
||
function runMigration(tree: Tree, tsconfigPath: string, basePath: string) { | ||
const program = createMigrationProgram(tree, tsconfigPath, basePath); | ||
const sourceFiles = | ||
program.getSourceFiles().filter(sourceFile => canMigrateFile(basePath, sourceFile, program)); | ||
|
||
for (const sourceFile of sourceFiles) { | ||
let update: UpdateRecorder|null = null; | ||
|
||
const rewriter = (startPos: number, width: number, text: string|null) => { | ||
if (update === null) { | ||
// Lazily initialize update, because most files will not require migration. | ||
update = tree.beginUpdate(relative(basePath, sourceFile.fileName)); | ||
} | ||
update.remove(startPos, width); | ||
if (text !== null) { | ||
update.insertLeft(startPos, text); | ||
} | ||
}; | ||
migrateFile(sourceFile, rewriter); | ||
|
||
if (update !== null) { | ||
tree.commitUpdate(update); | ||
} | ||
} | ||
} |
75 changes: 75 additions & 0 deletions
75
packages/core/schematics/migrations/transfer-state/utils.ts
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,75 @@ | ||
/** | ||
* @license | ||
* Copyright Google LLC All Rights Reserved. | ||
* | ||
* Use of this source code is governed by an MIT-style license that can be | ||
* found in the LICENSE file at https://angular.io/license | ||
*/ | ||
|
||
import ts from 'typescript'; | ||
|
||
import {ChangeTracker} from '../../utils/change_tracker'; | ||
import {getImportSpecifiers, removeSymbolFromNamedImports} from '../../utils/typescript/imports'; | ||
import {closestNode} from '../../utils/typescript/nodes'; | ||
|
||
export const symbolsToUpdate = new Set(['makeStateKey', 'StateKey', 'TransferState']); | ||
export const platformBrowserModule = '@angular/platform-browser'; | ||
export const coreModule = '@angular/core'; | ||
|
||
export type RewriteFn = (startPos: number, width: number, text: string) => void; | ||
|
||
export function migrateFile(sourceFile: ts.SourceFile, rewriteFn: RewriteFn) { | ||
const exposedImports = | ||
getImportSpecifiers(sourceFile, platformBrowserModule, [...symbolsToUpdate]); | ||
if (exposedImports.length === 0) { | ||
return; | ||
} | ||
|
||
migrateImports(sourceFile, rewriteFn); | ||
} | ||
|
||
function migrateImports(sourceFile: ts.SourceFile, rewriteFn: RewriteFn) { | ||
let changeTracker = new ChangeTracker(ts.createPrinter()); | ||
const updatedImports = new Map<ts.NamedImports, ts.NamedImports>(); | ||
const addedImports = new Array(); | ||
const importSpecifiers = | ||
getImportSpecifiers(sourceFile, platformBrowserModule, [...symbolsToUpdate]); | ||
for (const importSpecifier of importSpecifiers) { | ||
const namedImports = closestNode(importSpecifier, ts.isNamedImports)!; | ||
const importToUpdate = updatedImports.get(namedImports) ?? namedImports; | ||
const rewrittenNamedImports = removeSymbolFromNamedImports(importToUpdate, importSpecifier); | ||
updatedImports.set(namedImports, rewrittenNamedImports); | ||
addedImports.push(importSpecifier.name.getText()); | ||
} | ||
|
||
// Remove the existing imports | ||
for (const [originalNode, rewrittenNode] of updatedImports.entries()) { | ||
if (rewrittenNode.elements.length > 0) { | ||
changeTracker.replaceNode(originalNode, rewrittenNode); | ||
} else { | ||
const importDeclaration = originalNode.parent.parent; | ||
changeTracker.removeNode(importDeclaration); | ||
} | ||
} | ||
|
||
// Apply the removal changes | ||
for (const changesInFile of changeTracker.recordChanges().values()) { | ||
for (const change of changesInFile) { | ||
rewriteFn(change.start, change.removeLength ?? 0, change.text); | ||
} | ||
} | ||
|
||
changeTracker.clearChanges(); | ||
|
||
// Add the new imports | ||
for (const i of addedImports) { | ||
changeTracker.addImport(sourceFile, i, coreModule, null, true); | ||
} | ||
|
||
// Apply the adding changes | ||
for (const changesInFile of changeTracker.recordChanges().values()) { | ||
for (const change of changesInFile) { | ||
rewriteFn(change.start, change.removeLength ?? 0, change.text); | ||
} | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
/** | ||
* @license | ||
* Copyright Google LLC All Rights Reserved. | ||
* | ||
* Use of this source code is governed by an MIT-style license that can be | ||
* found in the LICENSE file at https://angular.io/license | ||
*/ | ||
|
||
import {getSystemPath, normalize, virtualFs} from '@angular-devkit/core'; | ||
import {TempScopedNodeJsSyncHost} from '@angular-devkit/core/node/testing'; | ||
import {HostTree} from '@angular-devkit/schematics'; | ||
import {SchematicTestRunner, UnitTestTree} from '@angular-devkit/schematics/testing'; | ||
import {runfiles} from '@bazel/runfiles'; | ||
import shx from 'shelljs'; | ||
|
||
describe('TransferState migration', () => { | ||
let runner: SchematicTestRunner; | ||
let host: TempScopedNodeJsSyncHost; | ||
let tree: UnitTestTree; | ||
let tmpDirPath: string; | ||
let previousWorkingDir: string; | ||
|
||
function writeFile(filePath: string, contents: string) { | ||
host.sync.write(normalize(filePath), virtualFs.stringToFileBuffer(contents)); | ||
} | ||
|
||
function runMigration() { | ||
return runner.runSchematic('migration-transfer-state', {}, tree); | ||
} | ||
|
||
beforeEach(() => { | ||
runner = new SchematicTestRunner('test', runfiles.resolvePackageRelative('../migrations.json')); | ||
host = new TempScopedNodeJsSyncHost(); | ||
tree = new UnitTestTree(new HostTree(host)); | ||
|
||
writeFile('/tsconfig.json', JSON.stringify({ | ||
compilerOptions: { | ||
lib: ['es2015'], | ||
strictNullChecks: true, | ||
}, | ||
})); | ||
|
||
writeFile('/angular.json', JSON.stringify({ | ||
version: 1, | ||
projects: {t: {root: '', architect: {build: {options: {tsConfig: './tsconfig.json'}}}}} | ||
})); | ||
|
||
previousWorkingDir = shx.pwd(); | ||
tmpDirPath = getSystemPath(host.root); | ||
|
||
// Switch into the temporary directory path. This allows us to run | ||
// the schematic against our custom unit test tree. | ||
shx.cd(tmpDirPath); | ||
}); | ||
|
||
afterEach(() => { | ||
shx.cd(previousWorkingDir); | ||
shx.rm('-r', tmpDirPath); | ||
}); | ||
|
||
it('should change imports', async () => { | ||
writeFile('/index.ts', `import { TransferState } from '@angular/platform-browser';`); | ||
|
||
await runMigration(); | ||
|
||
const content = tree.readContent('/index.ts'); | ||
expect(content).toContain(`import { TransferState } from '@angular/core'`); | ||
}); | ||
|
||
it('should change imports', async () => { | ||
writeFile( | ||
'/index.ts', | ||
`import { TransferState, StateKey, makeStateKey } from '@angular/platform-browser';`); | ||
|
||
await runMigration(); | ||
|
||
const content = tree.readContent('/index.ts'); | ||
expect(content).not.toContain(`@angular/platform-browser`); | ||
expect(content).toContain( | ||
`import { makeStateKey, StateKey, TransferState } from '@angular/core'`); | ||
}); | ||
|
||
it('should change imports with existing core import', async () => { | ||
writeFile('/index.ts', ` | ||
import { TransferState, StateKey, makeStateKey } from '@angular/platform-browser'; | ||
import { NgOnInit } from '@angular/core'; | ||
`); | ||
|
||
await runMigration(); | ||
|
||
const content = tree.readContent('/index.ts'); | ||
expect(content).toContain( | ||
`import { NgOnInit, makeStateKey, StateKey, TransferState } from '@angular/core'`); | ||
}); | ||
|
||
it('should change imports but keep others ', async () => { | ||
writeFile( | ||
'/index.ts', | ||
`import { TransferState, StateKey, makeStateKey, bootstrapApplication } from '@angular/platform-browser';`); | ||
|
||
await runMigration(); | ||
|
||
const content = tree.readContent('/index.ts'); | ||
expect(content).toContain( | ||
`import { makeStateKey, StateKey, TransferState } from '@angular/core'`); | ||
expect(content).toContain(`import { bootstrapApplication } from '@angular/platform-browser'`); | ||
}); | ||
|
||
it('should not change imports', async () => { | ||
writeFile('/index.ts', ` | ||
import { TransferState } from '@not-angular/platform-browser' | ||
`); | ||
|
||
await runMigration(); | ||
|
||
const content = tree.readContent('/index.ts'); | ||
expect(content).toContain(`import { TransferState } from '@not-angular/platform-browser'`); | ||
}); | ||
}); |
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.