@openrewrite/recipes-angular 1.35.0 → 1.37.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.d.ts +8 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +38 -6
- package/dist/index.js.map +1 -1
- package/dist/libraries/components/package-version.d.ts +1 -1
- package/dist/libraries/components/package-version.d.ts.map +1 -1
- package/dist/libraries/components/package-version.js +1 -1
- package/dist/libraries/components/package-version.js.map +1 -1
- package/dist/migration/add-static-false-to-view-queries.d.ts.map +1 -1
- package/dist/migration/add-static-false-to-view-queries.js +16 -13
- package/dist/migration/add-static-false-to-view-queries.js.map +1 -1
- package/dist/migration/angular-version.d.ts +22 -2
- package/dist/migration/angular-version.d.ts.map +1 -1
- package/dist/migration/angular-version.js +65 -18
- package/dist/migration/angular-version.js.map +1 -1
- package/dist/migration/jasmine-to-vitest/add-vitest-imports.d.ts +11 -0
- package/dist/migration/jasmine-to-vitest/add-vitest-imports.d.ts.map +1 -0
- package/dist/migration/jasmine-to-vitest/add-vitest-imports.js +76 -0
- package/dist/migration/jasmine-to-vitest/add-vitest-imports.js.map +1 -0
- package/dist/migration/jasmine-to-vitest/migrate-fake-async.d.ts +14 -0
- package/dist/migration/jasmine-to-vitest/migrate-fake-async.d.ts.map +1 -0
- package/dist/migration/jasmine-to-vitest/migrate-fake-async.js +225 -0
- package/dist/migration/jasmine-to-vitest/migrate-fake-async.js.map +1 -0
- package/dist/migration/jasmine-to-vitest/migrate-jasmine-lifecycle.d.ts +11 -0
- package/dist/migration/jasmine-to-vitest/migrate-jasmine-lifecycle.d.ts.map +1 -0
- package/dist/migration/jasmine-to-vitest/migrate-jasmine-lifecycle.js +379 -0
- package/dist/migration/jasmine-to-vitest/migrate-jasmine-lifecycle.js.map +1 -0
- package/dist/migration/jasmine-to-vitest/migrate-jasmine-matchers.d.ts +18 -0
- package/dist/migration/jasmine-to-vitest/migrate-jasmine-matchers.d.ts.map +1 -0
- package/dist/migration/jasmine-to-vitest/migrate-jasmine-matchers.js +446 -0
- package/dist/migration/jasmine-to-vitest/migrate-jasmine-matchers.js.map +1 -0
- package/dist/migration/jasmine-to-vitest/migrate-jasmine-misc.d.ts +11 -0
- package/dist/migration/jasmine-to-vitest/migrate-jasmine-misc.d.ts.map +1 -0
- package/dist/migration/jasmine-to-vitest/migrate-jasmine-misc.js +209 -0
- package/dist/migration/jasmine-to-vitest/migrate-jasmine-misc.js.map +1 -0
- package/dist/migration/jasmine-to-vitest/migrate-jasmine-spies.d.ts +11 -0
- package/dist/migration/jasmine-to-vitest/migrate-jasmine-spies.d.ts.map +1 -0
- package/dist/migration/jasmine-to-vitest/migrate-jasmine-spies.js +567 -0
- package/dist/migration/jasmine-to-vitest/migrate-jasmine-spies.js.map +1 -0
- package/dist/migration/jasmine-to-vitest/migrate-jasmine-to-vitest.d.ts +16 -0
- package/dist/migration/jasmine-to-vitest/migrate-jasmine-to-vitest.d.ts.map +1 -0
- package/dist/migration/jasmine-to-vitest/migrate-jasmine-to-vitest.js +90 -0
- package/dist/migration/jasmine-to-vitest/migrate-jasmine-to-vitest.js.map +1 -0
- package/dist/migration/jasmine-to-vitest/migrate-jasmine-types.d.ts +11 -0
- package/dist/migration/jasmine-to-vitest/migrate-jasmine-types.d.ts.map +1 -0
- package/dist/migration/jasmine-to-vitest/migrate-jasmine-types.js +142 -0
- package/dist/migration/jasmine-to-vitest/migrate-jasmine-types.js.map +1 -0
- package/dist/migration/jasmine-to-vitest/support.d.ts +88 -0
- package/dist/migration/jasmine-to-vitest/support.d.ts.map +1 -0
- package/dist/migration/jasmine-to-vitest/support.js +561 -0
- package/dist/migration/jasmine-to-vitest/support.js.map +1 -0
- package/dist/migration/upgrade-to-angular-10.d.ts.map +1 -1
- package/dist/migration/upgrade-to-angular-10.js +3 -2
- package/dist/migration/upgrade-to-angular-10.js.map +1 -1
- package/dist/migration/upgrade-to-angular-11.d.ts.map +1 -1
- package/dist/migration/upgrade-to-angular-11.js +3 -2
- package/dist/migration/upgrade-to-angular-11.js.map +1 -1
- package/dist/migration/upgrade-to-angular-12.d.ts.map +1 -1
- package/dist/migration/upgrade-to-angular-12.js +3 -2
- package/dist/migration/upgrade-to-angular-12.js.map +1 -1
- package/dist/migration/upgrade-to-angular-13.d.ts.map +1 -1
- package/dist/migration/upgrade-to-angular-13.js +3 -2
- package/dist/migration/upgrade-to-angular-13.js.map +1 -1
- package/dist/migration/upgrade-to-angular-14.d.ts.map +1 -1
- package/dist/migration/upgrade-to-angular-14.js +3 -2
- package/dist/migration/upgrade-to-angular-14.js.map +1 -1
- package/dist/migration/upgrade-to-angular-15.d.ts.map +1 -1
- package/dist/migration/upgrade-to-angular-15.js +3 -2
- package/dist/migration/upgrade-to-angular-15.js.map +1 -1
- package/dist/migration/upgrade-to-angular-16.d.ts.map +1 -1
- package/dist/migration/upgrade-to-angular-16.js +3 -2
- package/dist/migration/upgrade-to-angular-16.js.map +1 -1
- package/dist/migration/upgrade-to-angular-17.d.ts.map +1 -1
- package/dist/migration/upgrade-to-angular-17.js +3 -2
- package/dist/migration/upgrade-to-angular-17.js.map +1 -1
- package/dist/migration/upgrade-to-angular-18.d.ts.map +1 -1
- package/dist/migration/upgrade-to-angular-18.js +3 -2
- package/dist/migration/upgrade-to-angular-18.js.map +1 -1
- package/dist/migration/upgrade-to-angular-19.d.ts.map +1 -1
- package/dist/migration/upgrade-to-angular-19.js +3 -2
- package/dist/migration/upgrade-to-angular-19.js.map +1 -1
- package/dist/migration/upgrade-to-angular-20.d.ts.map +1 -1
- package/dist/migration/upgrade-to-angular-20.js +3 -2
- package/dist/migration/upgrade-to-angular-20.js.map +1 -1
- package/dist/migration/upgrade-to-angular-21.d.ts.map +1 -1
- package/dist/migration/upgrade-to-angular-21.js +3 -2
- package/dist/migration/upgrade-to-angular-21.js.map +1 -1
- package/dist/migration/upgrade-to-angular-22.d.ts.map +1 -1
- package/dist/migration/upgrade-to-angular-22.js +3 -2
- package/dist/migration/upgrade-to-angular-22.js.map +1 -1
- package/dist/migration/upgrade-to-angular-8.d.ts.map +1 -1
- package/dist/migration/upgrade-to-angular-8.js +3 -2
- package/dist/migration/upgrade-to-angular-8.js.map +1 -1
- package/dist/migration/upgrade-to-angular-9.d.ts.map +1 -1
- package/dist/migration/upgrade-to-angular-9.js +3 -2
- package/dist/migration/upgrade-to-angular-9.js.map +1 -1
- package/package.json +1 -1
- package/src/index.ts +24 -0
- package/src/libraries/components/package-version.ts +2 -2
- package/src/migration/add-static-false-to-view-queries.ts +16 -12
- package/src/migration/angular-version.ts +65 -16
- package/src/migration/jasmine-to-vitest/add-vitest-imports.ts +51 -0
- package/src/migration/jasmine-to-vitest/migrate-fake-async.ts +211 -0
- package/src/migration/jasmine-to-vitest/migrate-jasmine-lifecycle.ts +346 -0
- package/src/migration/jasmine-to-vitest/migrate-jasmine-matchers.ts +410 -0
- package/src/migration/jasmine-to-vitest/migrate-jasmine-misc.ts +190 -0
- package/src/migration/jasmine-to-vitest/migrate-jasmine-spies.ts +533 -0
- package/src/migration/jasmine-to-vitest/migrate-jasmine-to-vitest.ts +72 -0
- package/src/migration/jasmine-to-vitest/migrate-jasmine-types.ts +97 -0
- package/src/migration/jasmine-to-vitest/support.ts +516 -0
- package/src/migration/upgrade-to-angular-10.ts +4 -3
- package/src/migration/upgrade-to-angular-11.ts +4 -3
- package/src/migration/upgrade-to-angular-12.ts +4 -3
- package/src/migration/upgrade-to-angular-13.ts +4 -3
- package/src/migration/upgrade-to-angular-14.ts +4 -3
- package/src/migration/upgrade-to-angular-15.ts +4 -3
- package/src/migration/upgrade-to-angular-16.ts +4 -3
- package/src/migration/upgrade-to-angular-17.ts +4 -3
- package/src/migration/upgrade-to-angular-18.ts +4 -3
- package/src/migration/upgrade-to-angular-19.ts +4 -3
- package/src/migration/upgrade-to-angular-20.ts +4 -3
- package/src/migration/upgrade-to-angular-21.ts +4 -3
- package/src/migration/upgrade-to-angular-22.ts +4 -3
- package/src/migration/upgrade-to-angular-8.ts +4 -3
- package/src/migration/upgrade-to-angular-9.ts +4 -3
|
@@ -47,12 +47,12 @@ export function majorFor<T = number>(majors: Map<string, T>, sourcePath: string)
|
|
|
47
47
|
}
|
|
48
48
|
|
|
49
49
|
/** Records the major of a package in every `package.json` that declares one, keyed by its directory. */
|
|
50
|
-
export function packageMajorScanner(packageName: string, record: (directory: string, major: number) => void): TreeVisitor<any, ExecutionContext> {
|
|
50
|
+
export function packageMajorScanner(packageName: string, record: (directory: string, major: number, packageJson: Json.Document) => void): TreeVisitor<any, ExecutionContext> {
|
|
51
51
|
return new class extends JsonVisitor<ExecutionContext> {
|
|
52
52
|
protected async visitDocument(document: Json.Document, p: ExecutionContext): Promise<Json | undefined> {
|
|
53
53
|
const path = document.sourcePath;
|
|
54
54
|
const major = path === 'package.json' || path.endsWith('/package.json') ? packageMajor(document, packageName) : undefined;
|
|
55
|
-
if (major !== undefined) record(directoryOf(path), major);
|
|
55
|
+
if (major !== undefined) record(directoryOf(path), major, document);
|
|
56
56
|
return document;
|
|
57
57
|
}
|
|
58
58
|
}();
|
|
@@ -43,6 +43,9 @@ const UNDECIDED = "Added `static: false`, but can't tell whether this query is r
|
|
|
43
43
|
"Use `static: true` if the class reads it in `ngOnInit` or earlier (Angular 7 resolved it then " +
|
|
44
44
|
"unless it was inside `*ngIf` or `*ngFor`). See " + GUIDE;
|
|
45
45
|
|
|
46
|
+
const NOT_ADDED = "Could not add the `static` flag because the query options are not an object literal the recipe can read. " +
|
|
47
|
+
"Add `static: true` or `static: false` by hand, as Angular 8 requires. See " + GUIDE;
|
|
48
|
+
|
|
46
49
|
/**
|
|
47
50
|
* @see https://v8.angular.io/guide/static-query-migration
|
|
48
51
|
*/
|
|
@@ -73,18 +76,14 @@ export class AddStaticFalseToViewQueries extends Recipe {
|
|
|
73
76
|
let optionsObj: J.NewClass | undefined;
|
|
74
77
|
if (a.arguments.elements.length >= 2) {
|
|
75
78
|
const optionsArg = a.arguments.elements[1].element;
|
|
76
|
-
if (!isObjectLiteral(optionsArg)) return a;
|
|
79
|
+
if (!isObjectLiteral(optionsArg) || !optionsArg.body) return foundSearchResult(a, NOT_ADDED);
|
|
77
80
|
optionsObj = optionsArg;
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
propertyName(stmt.element as JS.PropertyAssignment) === 'static') {
|
|
85
|
-
return a;
|
|
86
|
-
}
|
|
87
|
-
}
|
|
81
|
+
|
|
82
|
+
const names = optionsObj.body!.statements.map(stmt => stmt.element.kind === JS.Kind.PropertyAssignment ?
|
|
83
|
+
propertyName(stmt.element as JS.PropertyAssignment) : undefined);
|
|
84
|
+
if (names.includes('static')) return a;
|
|
85
|
+
// A spread or a key that can't be read may carry `static`, which a flag added after it would override.
|
|
86
|
+
if (names.includes(undefined)) return foundSearchResult(a, NOT_ADDED);
|
|
88
87
|
}
|
|
89
88
|
|
|
90
89
|
const timing = await queryTiming(this.cursor, earlyHooks, this.classes);
|
|
@@ -258,9 +257,14 @@ function mayInheritOrBeInherited(clazz: J.ClassDeclaration, cursor: Cursor): boo
|
|
|
258
257
|
return extended;
|
|
259
258
|
}
|
|
260
259
|
|
|
261
|
-
/** The name of a property in an object literal, written `name` or `'name'
|
|
260
|
+
/** The name of a property in an object literal, written `name`, `'name'` or `['name']`; `undefined` when it can't be read. */
|
|
262
261
|
function propertyName(prop: JS.PropertyAssignment): string | undefined {
|
|
263
262
|
const name = prop.name.element;
|
|
263
|
+
if (name.kind === JS.Kind.ComputedPropertyName) {
|
|
264
|
+
// `[key]` names whatever the variable holds, so only a literal is readable.
|
|
265
|
+
const key = (name as JS.ComputedPropertyName).expression.element;
|
|
266
|
+
return isLiteral(key) && typeof key.value === 'string' ? key.value : undefined;
|
|
267
|
+
}
|
|
264
268
|
if (isIdentifier(name)) return name.simpleName;
|
|
265
269
|
return isLiteral(name) && typeof name.value === 'string' ? name.value : undefined;
|
|
266
270
|
}
|
|
@@ -25,13 +25,26 @@ interface AngularProjects {
|
|
|
25
25
|
majors?: Map<string, number>;
|
|
26
26
|
}
|
|
27
27
|
|
|
28
|
+
/**
|
|
29
|
+
* The `@angular/core` major each `package.json` declared when the run first scanned it, keyed by its tree id, which
|
|
30
|
+
* the dependency upgrade of an earlier cycle keeps.
|
|
31
|
+
*/
|
|
32
|
+
const startingMajors = new Map<string, number>();
|
|
33
|
+
|
|
34
|
+
function startingMajor(packageJson: SourceFile, major: number): number {
|
|
35
|
+
const starting = startingMajors.get(packageJson.id);
|
|
36
|
+
if (starting !== undefined) return starting;
|
|
37
|
+
startingMajors.set(packageJson.id, major);
|
|
38
|
+
return major;
|
|
39
|
+
}
|
|
40
|
+
|
|
28
41
|
export class FindAngularProjectVersions extends ScanningRecipe<Map<string, number>> {
|
|
29
42
|
readonly name = "org.openrewrite.angular.migration.find-angular-project-versions";
|
|
30
43
|
readonly displayName = "Find the Angular version of each project";
|
|
31
44
|
readonly description = "Records the `@angular/core` major that each `package.json` declares, so that the " +
|
|
32
45
|
"migrations of an Angular major leave the projects that are already on it alone.";
|
|
33
46
|
|
|
34
|
-
constructor(private readonly projects: AngularProjects = {}) {
|
|
47
|
+
constructor(private readonly projects: AngularProjects = {}, private readonly atStart: boolean = true) {
|
|
35
48
|
super();
|
|
36
49
|
}
|
|
37
50
|
|
|
@@ -43,25 +56,28 @@ export class FindAngularProjectVersions extends ScanningRecipe<Map<string, numbe
|
|
|
43
56
|
|
|
44
57
|
async scanner(majors: Map<string, number>): Promise<TreeVisitor<any, ExecutionContext>> {
|
|
45
58
|
return check(hasSourcePath('**/package.json'),
|
|
46
|
-
packageMajorScanner(ANGULAR_CORE, (directory, major) =>
|
|
59
|
+
packageMajorScanner(ANGULAR_CORE, (directory, major, packageJson) =>
|
|
60
|
+
majors.set(directory, this.atStart ? startingMajor(packageJson, major) : major)));
|
|
47
61
|
}
|
|
48
62
|
}
|
|
49
63
|
|
|
50
|
-
|
|
51
|
-
|
|
64
|
+
type MajorTest = (major: number) => boolean;
|
|
65
|
+
|
|
66
|
+
/** Whether a source file belongs to a project whose `package.json` declares an `@angular/core` major that passes the test. */
|
|
67
|
+
function inProject(projects: AngularProjects, sourcePath: string, test: MajorTest): boolean {
|
|
52
68
|
const major = projects.majors && majorFor(projects.majors, sourcePath);
|
|
53
|
-
return major !== undefined && major
|
|
69
|
+
return major !== undefined && test(major);
|
|
54
70
|
}
|
|
55
71
|
|
|
56
|
-
/** Matches the source files of the projects
|
|
57
|
-
class
|
|
58
|
-
constructor(private readonly projects: AngularProjects, private readonly
|
|
72
|
+
/** Matches the source files of the projects whose `@angular/core` major passes the test. */
|
|
73
|
+
class InAngularProject extends TreeVisitor<any, ExecutionContext> {
|
|
74
|
+
constructor(private readonly projects: AngularProjects, private readonly test: MajorTest) {
|
|
59
75
|
super();
|
|
60
76
|
}
|
|
61
77
|
|
|
62
78
|
protected async preVisit(tree: any, _ctx: ExecutionContext): Promise<any> {
|
|
63
79
|
this.stopAfterPreVisit();
|
|
64
|
-
return isSourceFile(tree) &&
|
|
80
|
+
return isSourceFile(tree) && inProject(this.projects, tree.sourcePath, this.test) ? foundSearchResult(tree) : tree;
|
|
65
81
|
}
|
|
66
82
|
}
|
|
67
83
|
|
|
@@ -70,14 +86,14 @@ function implementedInJava(recipe: Recipe): boolean {
|
|
|
70
86
|
return recipe instanceof RpcRecipe || typeof (recipe as any).javaRecipeName === 'string';
|
|
71
87
|
}
|
|
72
88
|
|
|
73
|
-
/** A copy of a recipe and its sub-recipes, with the same class, name and options, that edits only projects
|
|
74
|
-
function gated(recipe: Recipe, projects: AngularProjects,
|
|
89
|
+
/** A copy of a recipe and its sub-recipes, with the same class, name and options, that edits only the projects whose major passes the test. */
|
|
90
|
+
function gated(recipe: Recipe, projects: AngularProjects, test: MajorTest): Recipe {
|
|
75
91
|
if (implementedInJava(recipe)) return recipe;
|
|
76
|
-
const gate = () => new
|
|
92
|
+
const gate = () => new InAngularProject(projects, test);
|
|
77
93
|
const recipeList = recipe.recipeList;
|
|
78
94
|
const overrides: Record<string, unknown> = {
|
|
79
95
|
async recipeList(this: Recipe): Promise<Recipe[]> {
|
|
80
|
-
return (await recipeList.call(this)).map(child => gated(child, projects,
|
|
96
|
+
return (await recipeList.call(this)).map(child => gated(child, projects, test));
|
|
81
97
|
}
|
|
82
98
|
};
|
|
83
99
|
if (recipe instanceof ScanningRecipe) {
|
|
@@ -87,7 +103,7 @@ function gated(recipe: Recipe, projects: AngularProjects, version: number): Reci
|
|
|
87
103
|
return check(gate(), editorWithData.call(this, acc));
|
|
88
104
|
};
|
|
89
105
|
overrides.generate = async function (this: ScanningRecipe<any>, acc: any, ctx: ExecutionContext): Promise<SourceFile[]> {
|
|
90
|
-
return (await generate.call(this, acc, ctx)).filter(file =>
|
|
106
|
+
return (await generate.call(this, acc, ctx)).filter(file => inProject(projects, file.sourcePath, test));
|
|
91
107
|
};
|
|
92
108
|
} else {
|
|
93
109
|
const editor = recipe.editor;
|
|
@@ -98,8 +114,41 @@ function gated(recipe: Recipe, projects: AngularProjects, version: number): Reci
|
|
|
98
114
|
return Object.assign(Object.create(Object.getPrototypeOf(recipe)), recipe, overrides);
|
|
99
115
|
}
|
|
100
116
|
|
|
101
|
-
/**
|
|
117
|
+
/**
|
|
118
|
+
* The migrations of an Angular major gated to the projects that were on an earlier one when the run started, except
|
|
119
|
+
* dependency changes implemented in Java.
|
|
120
|
+
*/
|
|
102
121
|
export function beforeAngular(version: number, recipes: Recipe[]): Recipe[] {
|
|
103
122
|
const projects: AngularProjects = {};
|
|
104
|
-
return [new FindAngularProjectVersions(projects), ...recipes.map(recipe => gated(recipe, projects, version))];
|
|
123
|
+
return [new FindAngularProjectVersions(projects), ...recipes.map(recipe => gated(recipe, projects, major => major < version))];
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
/** Recipes gated to the projects whose `package.json` now declares the given `@angular/core` major or a later one. */
|
|
127
|
+
export function onAngular(version: number, recipes: Recipe[]): Recipe[] {
|
|
128
|
+
const projects: AngularProjects = {};
|
|
129
|
+
return [new FindAngularProjectVersions(projects, false), ...recipes.map(recipe => gated(recipe, projects, major => major >= version))];
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
export class UpgradeAngularDependencyVersions extends Recipe {
|
|
133
|
+
readonly name = "org.openrewrite.angular.migration.UpgradeAngularDependencyVersions";
|
|
134
|
+
readonly displayName = "Upgrade the Angular dependencies";
|
|
135
|
+
readonly description = "Upgrades the dependencies of an Angular major, and runs another cycle when it changes " +
|
|
136
|
+
"them, so that the code changes of the major apply to the projects that the upgrade moved to it.";
|
|
137
|
+
readonly causesAnotherCycle = true;
|
|
138
|
+
|
|
139
|
+
constructor(private readonly upgrades: Recipe[]) {
|
|
140
|
+
super();
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
async recipeList(): Promise<Recipe[]> {
|
|
144
|
+
return this.upgrades;
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
/**
|
|
149
|
+
* The migration to an Angular major: the dependency upgrade, and the code changes for the projects that it moved to
|
|
150
|
+
* the major, in the following cycle. A project whose dependency upgrade the package manager rejects keeps its code.
|
|
151
|
+
*/
|
|
152
|
+
export function upgradeToAngular(version: number, codeChanges: Recipe[], dependencyUpgrades: Recipe[]): Recipe[] {
|
|
153
|
+
return [...onAngular(version, codeChanges), new UpgradeAngularDependencyVersions(dependencyUpgrades)];
|
|
105
154
|
}
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright 2026 the original author or authors.
|
|
3
|
+
*
|
|
4
|
+
* Moderne Proprietary. Only for use by Moderne customers under the terms of a commercial contract.
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
import {ExecutionContext, Recipe, TreeVisitor} from "@openrewrite/rewrite";
|
|
8
|
+
import {J, isIdentifier, isMethodInvocation} from "@openrewrite/rewrite/java";
|
|
9
|
+
import {JS, maybeBind, namesDeclaredIn} from "@openrewrite/rewrite/javascript";
|
|
10
|
+
import {JasmineToVitestVisitor, walk} from "./support";
|
|
11
|
+
|
|
12
|
+
/** The Vitest globals a migrated test calls, and that `vitest/globals` would otherwise declare. */
|
|
13
|
+
const VITEST_GLOBALS = ['afterAll', 'afterEach', 'beforeAll', 'beforeEach', 'describe', 'expect', 'it', 'vi'];
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* @see https://github.com/angular/angular-cli/blob/main/packages/schematics/angular/refactor/jasmine-vitest/test-file-transformer.ts
|
|
17
|
+
*/
|
|
18
|
+
export class AddVitestImports extends Recipe {
|
|
19
|
+
readonly name = "org.openrewrite.angular.migration.add-vitest-imports";
|
|
20
|
+
readonly displayName = "Import the Vitest globals a test uses";
|
|
21
|
+
readonly description = "Adds `import { describe, expect, it, vi, ... } from 'vitest'` for the Vitest globals a test " +
|
|
22
|
+
"file calls and does not declare itself. Angular's `unit-test` builder runs Vitest with `globals: true`, " +
|
|
23
|
+
"so this is only needed where a project turned the globals off.";
|
|
24
|
+
|
|
25
|
+
async editor(): Promise<TreeVisitor<any, ExecutionContext>> {
|
|
26
|
+
return new class extends JasmineToVitestVisitor {
|
|
27
|
+
override async visitJsCompilationUnit(cu: JS.CompilationUnit, p: ExecutionContext): Promise<J | undefined> {
|
|
28
|
+
const visited = await super.visitJsCompilationUnit(cu, p);
|
|
29
|
+
if (visited !== cu) return visited;
|
|
30
|
+
const declared = namesDeclaredIn(cu);
|
|
31
|
+
const used = new Set<string>();
|
|
32
|
+
walk(cu, n => {
|
|
33
|
+
if (n.kind === JS.Kind.Import) return false;
|
|
34
|
+
if (isMethodInvocation(n)) {
|
|
35
|
+
const callee = n.select ? n.select.element : n.name;
|
|
36
|
+
if (isIdentifier(callee) && VITEST_GLOBALS.includes(callee.simpleName)) used.add(callee.simpleName);
|
|
37
|
+
} else if (n.kind === J.Kind.FieldAccess) {
|
|
38
|
+
const target = (n as J.FieldAccess).target;
|
|
39
|
+
if (isIdentifier(target) && VITEST_GLOBALS.includes(target.simpleName)) used.add(target.simpleName);
|
|
40
|
+
}
|
|
41
|
+
});
|
|
42
|
+
for (const name of VITEST_GLOBALS) {
|
|
43
|
+
if (used.has(name) && !declared.has(name)) {
|
|
44
|
+
maybeBind(this, {module: 'vitest', member: name, onlyIfReferenced: false});
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
return cu;
|
|
48
|
+
}
|
|
49
|
+
};
|
|
50
|
+
}
|
|
51
|
+
}
|
|
@@ -0,0 +1,211 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright 2026 the original author or authors.
|
|
3
|
+
*
|
|
4
|
+
* Moderne Proprietary. Only for use by Moderne customers under the terms of a commercial contract.
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
import {Cursor, ExecutionContext, Recipe, TreeVisitor} from "@openrewrite/rewrite";
|
|
8
|
+
import {J, isIdentifier, isLiteral, isMethodInvocation} from "@openrewrite/rewrite/java";
|
|
9
|
+
import {JS, MethodMatcher, maybeUnbind} from "@openrewrite/rewrite/javascript";
|
|
10
|
+
import {create} from "mutative";
|
|
11
|
+
import {
|
|
12
|
+
ANGULAR_CORE_TESTING,
|
|
13
|
+
JasmineToVitestVisitor,
|
|
14
|
+
args,
|
|
15
|
+
functionBody,
|
|
16
|
+
functionExpression,
|
|
17
|
+
isFunctionLike,
|
|
18
|
+
isNamed,
|
|
19
|
+
printTrimmed,
|
|
20
|
+
replaceWith,
|
|
21
|
+
select
|
|
22
|
+
} from "./support";
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* @see https://github.com/angular/angular-cli/blob/main/packages/schematics/angular/refactor/jasmine-vitest/transformers/fake-async-test.ts
|
|
26
|
+
* @see https://github.com/angular/angular-cli/blob/main/packages/schematics/angular/refactor/jasmine-vitest/transformers/fake-async-tick.ts
|
|
27
|
+
* @see https://github.com/angular/angular-cli/blob/main/packages/schematics/angular/refactor/jasmine-vitest/transformers/fake-async-flush.ts
|
|
28
|
+
* @see https://github.com/angular/angular-cli/blob/main/packages/schematics/angular/refactor/jasmine-vitest/transformers/fake-async-flush-microtasks.ts
|
|
29
|
+
*/
|
|
30
|
+
export class MigrateFakeAsyncToVitestTimers extends Recipe {
|
|
31
|
+
readonly name = "org.openrewrite.angular.migration.migrate-fake-async-to-vitest-timers";
|
|
32
|
+
readonly displayName = "Migrate Angular's `fakeAsync` tests to Vitest fake timers";
|
|
33
|
+
readonly description = "Rewrites tests using `fakeAsync` from `@angular/core/testing` to Vitest's fake timers: each " +
|
|
34
|
+
"`fakeAsync(fn)` becomes an `async` function, the outermost `describe` holding one gets `vi.useFakeTimers()` " +
|
|
35
|
+
"and `vi.useRealTimers()` hooks, `tick(ms)` becomes `await vi.advanceTimersByTimeAsync(ms)`, `flush()` becomes " +
|
|
36
|
+
"`await vi.runAllTimersAsync()` and `flushMicrotasks()` becomes `await vi.advanceTimersByTimeAsync(0)`. " +
|
|
37
|
+
"The tests then no longer need zone.js's `fakeAsync` patch. What `flush` returned, or its `maxTurns`, is left " +
|
|
38
|
+
"with a `TODO: vitest-migration` comment, as the Angular CLI's `refactor-jasmine-vitest` schematic does.";
|
|
39
|
+
|
|
40
|
+
async editor(): Promise<TreeVisitor<any, ExecutionContext>> {
|
|
41
|
+
return new FakeAsyncVisitor();
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
const FAKE_ASYNC = new MethodMatcher(`${ANGULAR_CORE_TESTING} fakeAsync(..)`);
|
|
46
|
+
const TICK = new MethodMatcher(`${ANGULAR_CORE_TESTING} tick(..)`);
|
|
47
|
+
const FLUSH = new MethodMatcher(`${ANGULAR_CORE_TESTING} flush(..)`);
|
|
48
|
+
const FLUSH_MICROTASKS = new MethodMatcher(`${ANGULAR_CORE_TESTING} flushMicrotasks(..)`);
|
|
49
|
+
|
|
50
|
+
function isDescribe(m: J): m is J.MethodInvocation {
|
|
51
|
+
if (!isMethodInvocation(m)) return false;
|
|
52
|
+
if (!m.select) return ['describe', 'fdescribe', 'xdescribe'].includes(m.name.simpleName);
|
|
53
|
+
return isNamed(select(m), 'describe');
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
class FakeAsyncVisitor extends JasmineToVitestVisitor {
|
|
57
|
+
private fakeAsyncCount = 0;
|
|
58
|
+
|
|
59
|
+
protected override async visitMethodInvocation(method: J.MethodInvocation, p: ExecutionContext): Promise<J | undefined> {
|
|
60
|
+
const outermostDescribe = isDescribe(method)
|
|
61
|
+
&& !this.cursor.parent?.firstEnclosing((t: any): t is J.MethodInvocation => isDescribe(t));
|
|
62
|
+
const before = this.fakeAsyncCount;
|
|
63
|
+
const cursor = this.cursor;
|
|
64
|
+
const visited = await super.visitMethodInvocation(method, p);
|
|
65
|
+
if (!visited || !isMethodInvocation(visited)) return visited;
|
|
66
|
+
const m = visited;
|
|
67
|
+
|
|
68
|
+
if (outermostDescribe && this.fakeAsyncCount > before) {
|
|
69
|
+
return this.addFakeTimerHooks(m);
|
|
70
|
+
}
|
|
71
|
+
if (FAKE_ASYNC.matches(m.methodType)) {
|
|
72
|
+
// Inside a `describe`, as the hooks it needs go there.
|
|
73
|
+
if (isFunctionLike(args(m)[0])
|
|
74
|
+
&& this.cursor.parent?.firstEnclosing((t: any): t is J.MethodInvocation => isDescribe(t))) {
|
|
75
|
+
this.fakeAsyncCount++;
|
|
76
|
+
return this.fakeAsync(m, cursor);
|
|
77
|
+
}
|
|
78
|
+
return m;
|
|
79
|
+
}
|
|
80
|
+
if (TICK.matches(m.methodType)) {
|
|
81
|
+
maybeUnbind(this, {module: ANGULAR_CORE_TESTING, member: 'tick'});
|
|
82
|
+
return replaceWith(m, this.cursor, 'await vi.advanceTimersByTimeAsync(', ...(args(m).length ? [args(m)[0]] : ['0']), ')');
|
|
83
|
+
}
|
|
84
|
+
if (FLUSH_MICROTASKS.matches(m.methodType)) {
|
|
85
|
+
maybeUnbind(this, {module: ANGULAR_CORE_TESTING, member: 'flushMicrotasks'});
|
|
86
|
+
return replaceWith(m, this.cursor, 'await vi.advanceTimersByTimeAsync(0)');
|
|
87
|
+
}
|
|
88
|
+
if (FLUSH.matches(m.methodType)) {
|
|
89
|
+
maybeUnbind(this, {module: ANGULAR_CORE_TESTING, member: 'flush'});
|
|
90
|
+
const statement = isStatement(cursor);
|
|
91
|
+
const result = await replaceWith(m, this.cursor, statement
|
|
92
|
+
? 'await vi.runAllTimersAsync()' : 'await vi.runAllTimersAsync() ?? 0');
|
|
93
|
+
if (args(m).length > 0) {
|
|
94
|
+
this.todo(result, 'flush(maxTurns) was called but maxTurns parameter is not migrated. Please migrate manually.');
|
|
95
|
+
}
|
|
96
|
+
if (!statement) {
|
|
97
|
+
this.todo(result, 'flush() return value is not migrated. Please migrate manually.');
|
|
98
|
+
}
|
|
99
|
+
return result;
|
|
100
|
+
}
|
|
101
|
+
return m;
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
/** `fakeAsync((args) => { ... })` to `async (args) => { ... }`. */
|
|
105
|
+
private async fakeAsync(m: J.MethodInvocation, cursor: Cursor): Promise<J> {
|
|
106
|
+
const callback = args(m)[0];
|
|
107
|
+
const options = args(m)[1];
|
|
108
|
+
const params = await parametersText(callback, this.cursor);
|
|
109
|
+
const body = functionBody(callback)!;
|
|
110
|
+
const inBeforeEach = isHookArgument(cursor, 'beforeEach') && !flushDisabled(options);
|
|
111
|
+
const shell = await replaceWith(m, this.cursor, inBeforeEach
|
|
112
|
+
? `async (${params}) => {\nawait vi.runOnlyPendingTimersAsync();\n}`
|
|
113
|
+
: `async (${params}) => {}`);
|
|
114
|
+
const shellBody = functionBody(shell) as J.Block;
|
|
115
|
+
let block: J.Block;
|
|
116
|
+
if (body.kind === J.Kind.Block) {
|
|
117
|
+
block = body as J.Block;
|
|
118
|
+
} else {
|
|
119
|
+
// An expression body becomes the block's one statement.
|
|
120
|
+
const wrapped = await replaceWith(m, this.cursor, `async (${params}) => {\n`, body, ';\n}');
|
|
121
|
+
block = functionBody(wrapped) as J.Block;
|
|
122
|
+
}
|
|
123
|
+
if (inBeforeEach) {
|
|
124
|
+
const flushStatement = shellBody.statements[0];
|
|
125
|
+
const last = block.statements[block.statements.length - 1];
|
|
126
|
+
const prefix = last ? last.element.prefix : flushStatement.element.prefix;
|
|
127
|
+
block = create(block, d => {
|
|
128
|
+
d.statements.push({...flushStatement, element: {...flushStatement.element, prefix}} as any);
|
|
129
|
+
});
|
|
130
|
+
}
|
|
131
|
+
return create(shell as JS.ArrowFunction, d => {
|
|
132
|
+
d.lambda.body = block as any;
|
|
133
|
+
});
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
/** Puts the fake-timer hooks first in the outermost `describe` that holds a `fakeAsync`. */
|
|
137
|
+
private async addFakeTimerHooks(m: J.MethodInvocation): Promise<J> {
|
|
138
|
+
maybeUnbind(this, {module: ANGULAR_CORE_TESTING, member: 'fakeAsync'});
|
|
139
|
+
const callback = args(m)[1];
|
|
140
|
+
const body = callback && isFunctionLike(callback) ? functionBody(callback) : undefined;
|
|
141
|
+
if (!body || body.kind !== J.Kind.Block) return m;
|
|
142
|
+
const hooks = await replaceWith(m, this.cursor, 'describe(x, () => {\n' +
|
|
143
|
+
'beforeEach(() => {\nvi.useFakeTimers({ advanceTimeDelta: 1, shouldAdvanceTime: true });\n});\n' +
|
|
144
|
+
'afterEach(() => {\nvi.useRealTimers();\n});\n})');
|
|
145
|
+
const hookStatements = (functionBody(args(hooks as J.MethodInvocation)[1]) as J.Block).statements;
|
|
146
|
+
const block = body as J.Block;
|
|
147
|
+
const first = block.statements[0];
|
|
148
|
+
const indentPrefix = first ? {...first.element.prefix, comments: []} : hookStatements[0].element.prefix;
|
|
149
|
+
const firstIndent = '\n' + indentPrefix.whitespace.substring(indentPrefix.whitespace.lastIndexOf('\n') + 1);
|
|
150
|
+
const newBlock = create(block, d => {
|
|
151
|
+
const hooksPadded = hookStatements.map((s, i) => ({
|
|
152
|
+
...s,
|
|
153
|
+
element: {...s.element, prefix: {kind: J.Kind.Space, comments: [], whitespace: i === 0 ? firstIndent : firstIndent}}
|
|
154
|
+
}));
|
|
155
|
+
d.statements.unshift(...hooksPadded as any);
|
|
156
|
+
});
|
|
157
|
+
return create(m, d => {
|
|
158
|
+
for (const e of d.arguments.elements) {
|
|
159
|
+
if (e.element.id === callback.id) {
|
|
160
|
+
e.element = withBlock(callback, newBlock) as any;
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
});
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
function withBlock(fn: J, block: J.Block): J {
|
|
168
|
+
if (fn.kind === JS.Kind.ArrowFunction) {
|
|
169
|
+
return create(fn as JS.ArrowFunction, d => {
|
|
170
|
+
d.lambda.body = block as any;
|
|
171
|
+
});
|
|
172
|
+
}
|
|
173
|
+
return create(fn as JS.StatementExpression, d => {
|
|
174
|
+
(d.statement as any).body = block;
|
|
175
|
+
});
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
async function parametersText(fn: J, cursor: Cursor): Promise<string> {
|
|
179
|
+
if (fn.kind === JS.Kind.ArrowFunction) {
|
|
180
|
+
const params = (fn as JS.ArrowFunction).lambda.parameters.parameters
|
|
181
|
+
.map(p => p.element).filter(p => p.kind !== J.Kind.Empty);
|
|
182
|
+
return (await Promise.all(params.map(p => printTrimmed(p, cursor)))).join(', ');
|
|
183
|
+
}
|
|
184
|
+
const params = functionExpression(fn)!.parameters.elements.map(p => p.element).filter(p => p.kind !== J.Kind.Empty);
|
|
185
|
+
return (await Promise.all(params.map(p => printTrimmed(p, cursor)))).join(', ');
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
/** Whether the call at the cursor is an argument of `hook(...)`. */
|
|
189
|
+
function isHookArgument(cursor: Cursor, hook: string): boolean {
|
|
190
|
+
const parent = cursor.parentTree()?.value;
|
|
191
|
+
return !!parent && isMethodInvocation(parent) && !parent.select && parent.name.simpleName === hook;
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
/** `fakeAsync(fn, { flush: false })`. */
|
|
195
|
+
function flushDisabled(options: J | undefined): boolean {
|
|
196
|
+
if (!options || options.kind !== J.Kind.NewClass || !(options as J.NewClass).body) return false;
|
|
197
|
+
return (options as J.NewClass).body!.statements.some(s => {
|
|
198
|
+
const prop = s.element as JS.PropertyAssignment;
|
|
199
|
+
if (prop.kind !== JS.Kind.PropertyAssignment) return false;
|
|
200
|
+
const name = prop.name.element;
|
|
201
|
+
return isIdentifier(name) && name.simpleName === 'flush' && !!prop.initializer
|
|
202
|
+
&& isLiteral(prop.initializer) && prop.initializer.value === false;
|
|
203
|
+
});
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
/** Whether the call at the cursor is a statement of its own, so nothing reads what it returns. */
|
|
207
|
+
function isStatement(cursor: Cursor): boolean {
|
|
208
|
+
const parent = cursor.parentTree()?.value;
|
|
209
|
+
return parent?.kind === J.Kind.Block || parent?.kind === JS.Kind.CompilationUnit
|
|
210
|
+
|| parent?.kind === JS.Kind.ExpressionStatement;
|
|
211
|
+
}
|