forked from angular/angular
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(core): migrate ExperimentalPendingTasks to PendingTasks
This commit promotes the `ExperimentalPendingTasks` service from experimental to developer preview and includes a migration schematic for the rename. BREAKING CHANGE: `ExperimentalPendingTasks` has been renamed to `PendingTasks`.
- Loading branch information
Showing
38 changed files
with
325 additions
and
77 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
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
21 changes: 21 additions & 0 deletions
21
packages/core/schematics/migrations/pending-tasks/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,21 @@ | ||
load("//tools:defaults.bzl", "ts_library") | ||
|
||
package( | ||
default_visibility = [ | ||
"//packages/core/schematics:__pkg__", | ||
"//packages/core/schematics/migrations/google3:__pkg__", | ||
"//packages/core/schematics/test:__pkg__", | ||
], | ||
) | ||
|
||
ts_library( | ||
name = "pending-tasks", | ||
srcs = glob(["**/*.ts"]), | ||
tsconfig = "//packages/core/schematics:tsconfig.json", | ||
deps = [ | ||
"//packages/core/schematics/utils", | ||
"@npm//@angular-devkit/schematics", | ||
"@npm//@types/node", | ||
"@npm//typescript", | ||
], | ||
) |
58 changes: 58 additions & 0 deletions
58
packages/core/schematics/migrations/pending-tasks/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,58 @@ | ||
/** | ||
* @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 './migration'; | ||
|
||
export function migrate(): 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 afterRender phase 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, program.getTypeChecker(), rewriter); | ||
|
||
if (update !== null) { | ||
tree.commitUpdate(update); | ||
} | ||
} | ||
} |
67 changes: 67 additions & 0 deletions
67
packages/core/schematics/migrations/pending-tasks/migration.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,67 @@ | ||
/** | ||
* @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 { | ||
getImportOfIdentifier, | ||
getImportSpecifier, | ||
getNamedImports, | ||
} from '../../utils/typescript/imports'; | ||
|
||
const CORE = '@angular/core'; | ||
const EXPERIMENTAL_PENDING_TASKS = 'ExperimentalPendingTasks'; | ||
|
||
type RewriteFn = (startPos: number, width: number, text: string) => void; | ||
|
||
export function migrateFile( | ||
sourceFile: ts.SourceFile, | ||
typeChecker: ts.TypeChecker, | ||
rewriteFn: RewriteFn, | ||
) { | ||
const changeTracker = new ChangeTracker(ts.createPrinter()); | ||
// Check if there are any imports of the `AfterRenderPhase` enum. | ||
const coreImports = getNamedImports(sourceFile, CORE); | ||
if (!coreImports) { | ||
return; | ||
} | ||
const importSpecifier = getImportSpecifier(sourceFile, CORE, EXPERIMENTAL_PENDING_TASKS); | ||
if (!importSpecifier) { | ||
return; | ||
} | ||
const nodeToReplace = importSpecifier.propertyName ?? importSpecifier.name; | ||
if (!ts.isIdentifier(nodeToReplace)) { | ||
return; | ||
} | ||
|
||
changeTracker.replaceNode(nodeToReplace, ts.factory.createIdentifier('PendingTasks')); | ||
|
||
ts.forEachChild(sourceFile, function visit(node: ts.Node) { | ||
// import handled above | ||
if (ts.isImportDeclaration(node)) { | ||
return; | ||
} | ||
|
||
if ( | ||
ts.isIdentifier(node) && | ||
node.text === EXPERIMENTAL_PENDING_TASKS && | ||
getImportOfIdentifier(typeChecker, node)?.name === EXPERIMENTAL_PENDING_TASKS | ||
) { | ||
changeTracker.replaceNode(node, ts.factory.createIdentifier('PendingTasks')); | ||
} | ||
|
||
ts.forEachChild(node, visit); | ||
}); | ||
|
||
// Write the 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
/** | ||
* @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('experimental pending tasks migration', () => { | ||
let runner: SchematicTestRunner; | ||
let host: TempScopedNodeJsSyncHost; | ||
let tree: UnitTestTree; | ||
let tmpDirPath: string; | ||
|
||
function writeFile(filePath: string, contents: string) { | ||
host.sync.write(normalize(filePath), virtualFs.stringToFileBuffer(contents)); | ||
} | ||
|
||
function runMigration() { | ||
return runner.runSchematic('pending-tasks', {}, 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'}}}}}, | ||
}), | ||
); | ||
|
||
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); | ||
}); | ||
|
||
it('should update ExperimentalPendingTasks', async () => { | ||
writeFile( | ||
'/index.ts', | ||
` | ||
import {ExperimentalPendingTasks, Directive} from '@angular/core'; | ||
@Directive({ | ||
selector: '[someDirective]' | ||
}) | ||
export class SomeDirective { | ||
x = inject(ExperimentalPendingTasks); | ||
}`, | ||
); | ||
|
||
await runMigration(); | ||
|
||
const content = tree.readContent('/index.ts').replace(/\s+/g, ' '); | ||
expect(content).toContain("import {PendingTasks, Directive} from '@angular/core';"); | ||
expect(content).toContain('x = inject(PendingTasks);'); | ||
}); | ||
|
||
it('should update import alias', async () => { | ||
writeFile( | ||
'/index.ts', | ||
` | ||
import {ExperimentalPendingTasks as Y, Directive} from '@angular/core'; | ||
@Directive({ | ||
selector: '[someDirective]' | ||
}) | ||
export class SomeDirective { | ||
x = inject(Y); | ||
}`, | ||
); | ||
|
||
await runMigration(); | ||
|
||
const content = tree.readContent('/index.ts').replace(/\s+/g, ' '); | ||
expect(content).toContain("import {PendingTasks as Y, Directive} from '@angular/core';"); | ||
expect(content).toContain('x = inject(Y);'); | ||
}); | ||
}); |
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.