@openrewrite/recipes-angular 1.24.0 → 1.26.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.
@@ -1,8 +1,20 @@
1
- import { ExecutionContext, Recipe, TreeVisitor } from "@openrewrite/rewrite";
2
- export declare class RemoveZoneJsPolyfill extends Recipe {
1
+ import { ExecutionContext, ScanningRecipe, TreeVisitor } from "@openrewrite/rewrite";
2
+ /**
3
+ * Removes `zone.js` from the `polyfills` of each `angular.json` project that has switched to zoneless
4
+ * change detection.
5
+ *
6
+ * A project counts as zoneless when a non-test source file under its `sourceRoot` (by default `<root>/src`)
7
+ * imports `provideZonelessChangeDetection` (or the experimental provider it replaced) from `@angular/core`.
8
+ * Every other project still runs change detection through Zone.js and keeps the polyfill.
9
+ *
10
+ * @see https://angular.dev/guide/zoneless#removing-zonejs
11
+ */
12
+ export declare class RemoveZoneJsPolyfill extends ScanningRecipe<Set<string>> {
3
13
  readonly name = "org.openrewrite.angular.migration.remove-zone-js-polyfill";
4
14
  readonly displayName: string;
5
15
  readonly description: string;
6
- editor(): Promise<TreeVisitor<any, ExecutionContext>>;
16
+ initialValue(_ctx: ExecutionContext): Set<string>;
17
+ scanner(zonelessFiles: Set<string>): Promise<TreeVisitor<any, ExecutionContext>>;
18
+ editorWithData(zonelessFiles: Set<string>): Promise<TreeVisitor<any, ExecutionContext>>;
7
19
  }
8
20
  //# sourceMappingURL=remove-zone-js-polyfill.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"remove-zone-js-polyfill.d.ts","sourceRoot":"","sources":["../../src/migration/remove-zone-js-polyfill.ts"],"names":[],"mappings":"AAKA,OAAO,EAAC,gBAAgB,EAAE,MAAM,EAAE,WAAW,EAAC,MAAM,sBAAsB,CAAC;AAG3E,qBAAa,oBAAqB,SAAQ,MAAM;IAC5C,QAAQ,CAAC,IAAI,+DAA+D;IAC5E,QAAQ,CAAC,WAAW,EAAE,MAAM,CAA+C;IAC3E,QAAQ,CAAC,WAAW,EAAE,MAAM,CAA0M;IAEhO,MAAM,IAAI,OAAO,CAAC,WAAW,CAAC,GAAG,EAAE,gBAAgB,CAAC,CAAC;CAsB9D"}
1
+ {"version":3,"file":"remove-zone-js-polyfill.d.ts","sourceRoot":"","sources":["../../src/migration/remove-zone-js-polyfill.ts"],"names":[],"mappings":"AAKA,OAAO,EAAS,gBAAgB,EAAE,cAAc,EAAE,WAAW,EAAC,MAAM,sBAAsB,CAAC;AAuC3F;;;;;;;;;GASG;AACH,qBAAa,oBAAqB,SAAQ,cAAc,CAAC,GAAG,CAAC,MAAM,CAAC,CAAC;IACjE,QAAQ,CAAC,IAAI,+DAA+D;IAC5E,QAAQ,CAAC,WAAW,EAAE,MAAM,CAA+C;IAC3E,QAAQ,CAAC,WAAW,EAAE,MAAM,CAE4B;IAExD,YAAY,CAAC,IAAI,EAAE,gBAAgB,GAAG,GAAG,CAAC,MAAM,CAAC;IAI3C,OAAO,CAAC,aAAa,EAAE,GAAG,CAAC,MAAM,CAAC,GAAG,OAAO,CAAC,WAAW,CAAC,GAAG,EAAE,gBAAgB,CAAC,CAAC;IAWhF,cAAc,CAAC,aAAa,EAAE,GAAG,CAAC,MAAM,CAAC,GAAG,OAAO,CAAC,WAAW,CAAC,GAAG,EAAE,gBAAgB,CAAC,CAAC;CA8ChG"}
@@ -16,15 +16,81 @@ exports.RemoveZoneJsPolyfill = void 0;
16
16
  * Moderne Proprietary. Only for use by Moderne customers under the terms of a commercial contract.
17
17
  */
18
18
  const rewrite_1 = require("@openrewrite/rewrite");
19
+ const javascript_1 = require("@openrewrite/rewrite/javascript");
19
20
  const json_1 = require("@openrewrite/rewrite/json");
20
- class RemoveZoneJsPolyfill extends rewrite_1.Recipe {
21
+ const java_1 = require("@openrewrite/rewrite/java");
22
+ const path_1 = require("path");
23
+ const ZONELESS_PROVIDERS = new Set(['provideZonelessChangeDetection', 'provideExperimentalZonelessChangeDetection']);
24
+ /** Test files configure their own `TestBed` providers, which says nothing about how the application bootstraps. */
25
+ const isTestFile = (path) => /\.(spec|test)\.[cm]?[jt]sx?$/.test(path);
26
+ const isBeneath = (path, dir) => !path_1.posix.relative(dir, path).startsWith('..');
27
+ /** The value of a string member of a JSON object, or undefined. */
28
+ function stringMember(obj, key) {
29
+ var _a;
30
+ const value = (_a = (0, json_1.findMember)(obj, key)) === null || _a === void 0 ? void 0 : _a.value;
31
+ return value && (0, json_1.isLiteral)(value) && typeof value.value === 'string' ? value.value : undefined;
32
+ }
33
+ /** Whether the file imports one of Angular's zoneless change detection providers from `@angular/core`. */
34
+ function importsZonelessProvider(cu) {
35
+ return cu.statements.some(({ element }) => {
36
+ var _a, _b;
37
+ if (element.kind !== javascript_1.JS.Kind.Import)
38
+ return false;
39
+ const imp = element;
40
+ const module = (_a = imp.moduleSpecifier) === null || _a === void 0 ? void 0 : _a.element;
41
+ if ((module === null || module === void 0 ? void 0 : module.kind) !== java_1.J.Kind.Literal || module.value !== '@angular/core')
42
+ return false;
43
+ const named = (_b = imp.importClause) === null || _b === void 0 ? void 0 : _b.namedBindings;
44
+ if ((named === null || named === void 0 ? void 0 : named.kind) !== javascript_1.JS.Kind.NamedImports)
45
+ return false;
46
+ return named.elements.elements.some(({ element: specifier }) => {
47
+ const imported = specifier.specifier;
48
+ // `{a as b}` keeps the imported name in the alias' property name, which newer parsers also
49
+ // give as a string literal for `{'a' as b}`.
50
+ const name = imported.kind === javascript_1.JS.Kind.Alias ? imported.propertyName.element : imported;
51
+ const text = name.kind === java_1.J.Kind.Identifier ? name.simpleName :
52
+ name.kind === java_1.J.Kind.Literal ? name.value : undefined;
53
+ return typeof text === 'string' && ZONELESS_PROVIDERS.has(text);
54
+ });
55
+ });
56
+ }
57
+ /**
58
+ * Removes `zone.js` from the `polyfills` of each `angular.json` project that has switched to zoneless
59
+ * change detection.
60
+ *
61
+ * A project counts as zoneless when a non-test source file under its `sourceRoot` (by default `<root>/src`)
62
+ * imports `provideZonelessChangeDetection` (or the experimental provider it replaced) from `@angular/core`.
63
+ * Every other project still runs change detection through Zone.js and keeps the polyfill.
64
+ *
65
+ * @see https://angular.dev/guide/zoneless#removing-zonejs
66
+ */
67
+ class RemoveZoneJsPolyfill extends rewrite_1.ScanningRecipe {
21
68
  constructor() {
22
69
  super(...arguments);
23
70
  this.name = "org.openrewrite.angular.migration.remove-zone-js-polyfill";
24
71
  this.displayName = "Remove zone.js polyfill from angular.json";
25
- this.description = "Removes zone.js entries from the `polyfills` array in `angular.json`. Angular 20 supports zoneless change detection via `provideZonelessChangeDetection()`, making the zone.js polyfill unnecessary.";
72
+ this.description = "Removes `zone.js` entries from the `polyfills` arrays in `angular.json` for projects " +
73
+ "that provide zoneless change detection with `provideZonelessChangeDetection()`. Projects that still use " +
74
+ "Zone.js-based change detection keep the polyfill.";
75
+ }
76
+ initialValue(_ctx) {
77
+ return new Set();
26
78
  }
27
- editor() {
79
+ scanner(zonelessFiles) {
80
+ return __awaiter(this, void 0, void 0, function* () {
81
+ return new class extends javascript_1.JavaScriptVisitor {
82
+ visitJsCompilationUnit(cu, _ctx) {
83
+ return __awaiter(this, void 0, void 0, function* () {
84
+ if (!isTestFile(cu.sourcePath) && importsZonelessProvider(cu)) {
85
+ zonelessFiles.add(path_1.posix.normalize(cu.sourcePath));
86
+ }
87
+ return cu;
88
+ });
89
+ }
90
+ };
91
+ });
92
+ }
93
+ editorWithData(zonelessFiles) {
28
94
  return __awaiter(this, void 0, void 0, function* () {
29
95
  return new class extends json_1.JsonVisitor {
30
96
  visitDocument(doc, p) {
@@ -32,11 +98,37 @@ class RemoveZoneJsPolyfill extends rewrite_1.Recipe {
32
98
  visitDocument: { get: () => super.visitDocument }
33
99
  });
34
100
  return __awaiter(this, void 0, void 0, function* () {
35
- if (!doc.sourcePath.endsWith('angular.json'))
101
+ if (!doc.sourcePath.endsWith('angular.json') || zonelessFiles.size === 0)
36
102
  return doc;
37
103
  return _super.visitDocument.call(this, doc, p);
38
104
  });
39
105
  }
106
+ /** Whether the `angular.json` project enclosing the cursor has a zoneless source file. */
107
+ inZonelessProject(cursor) {
108
+ var _a, _b;
109
+ const doc = cursor.firstEnclosing((n) => (n === null || n === void 0 ? void 0 : n.kind) === json_1.Json.Kind.Document);
110
+ // The project is the object directly inside the `projects` object.
111
+ let inner;
112
+ let outer;
113
+ for (let c = cursor; c; c = c.parent) {
114
+ const member = c.value;
115
+ if ((member === null || member === void 0 ? void 0 : member.kind) === json_1.Json.Kind.Member && (0, json_1.getMemberKeyName)(member) === 'projects')
116
+ break;
117
+ if ((0, json_1.isObject)(c.value))
118
+ [inner, outer] = [outer, c.value];
119
+ }
120
+ const project = inner;
121
+ if (!doc || !project)
122
+ return false;
123
+ // `sourceRoot` is relative to the workspace and defaults to the project's `root` plus `src`, as Angular's builders do.
124
+ const sourceRoot = (_a = stringMember(project, 'sourceRoot')) !== null && _a !== void 0 ? _a : path_1.posix.join((_b = stringMember(project, 'root')) !== null && _b !== void 0 ? _b : '', 'src');
125
+ const root = path_1.posix.join(path_1.posix.dirname(doc.sourcePath), sourceRoot);
126
+ for (const file of zonelessFiles) {
127
+ if (isBeneath(file, root))
128
+ return true;
129
+ }
130
+ return false;
131
+ }
40
132
  visitMember(member, p) {
41
133
  const _super = Object.create(null, {
42
134
  visitMember: { get: () => super.visitMember }
@@ -53,7 +145,9 @@ class RemoveZoneJsPolyfill extends rewrite_1.Recipe {
53
145
  const entry = (0, json_1.toPlain)(element);
54
146
  return typeof entry === 'string' && (entry === 'zone.js' || entry.startsWith('zone.js/'));
55
147
  });
56
- return value === m.value ? m : Object.assign(Object.assign({}, m), { value });
148
+ if (value === m.value || !this.inZonelessProject(this.cursor))
149
+ return m;
150
+ return Object.assign(Object.assign({}, m), { value });
57
151
  });
58
152
  }
59
153
  }();
@@ -1 +1 @@
1
- {"version":3,"file":"remove-zone-js-polyfill.js","sourceRoot":"","sources":["../../src/migration/remove-zone-js-polyfill.ts"],"names":[],"mappings":";;;;;;;;;;;;AAAA;;;;GAIG;AACH,kDAA2E;AAC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IAAA,uBAAgB,EAAC,CAAC,CAAC,KAAK,WAAW;4BAAE,OAAO,CAAC,CAAC;wBAClD,IAAI,CAAC,IAAA,cAAO,EAAC,CAAC,CAAC,KAAK,CAAC;4BAAE,OAAO,CAAC,CAAC;wBAEhC,MAAM,KAAK,GAAG,IAAA,qBAAc,EAAC,CAAC,CAAC,KAAK,EAAE,OAAO,CAAC,EAAE;4BAC5C,MAAM,KAAK,GAAG,IAAA,cAAO,EAAC,OAAO,CAAC,CAAC;4BAC/B,OAAO,OAAO,KAAK,KAAK,QAAQ,IAAI,CAAC,KAAK,KAAK,SAAS,IAAI,KAAK,CAAC,UAAU,CAAC,UAAU,CAAC,CAAC,CAAC;wBAC9F,CAAC,CAAC,CAAC;wBAEH,IAAI,KAAK,KAAK,CAAC,CAAC,KAAK,IAAI,CAAC,IAAI,CAAC,iBAAiB,CAAC,IAAI,CAAC,MAAM,CAAC;4BAAE,OAAO,CAAC,CAAC;wBACxE,OAAO,gCAAI,CAAC,KAAE,KAAK,GAAgB,CAAC;oBACxC,CAAC;iBAAA;aACJ,EAAE,CAAC;QACR,CAAC;KAAA;CACJ;AApED,oDAoEC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@openrewrite/recipes-angular",
3
- "version": "1.24.0",
3
+ "version": "1.26.0",
4
4
  "license": "Moderne Proprietary",
5
5
  "description": "OpenRewrite recipes for Angular.",
6
6
  "homepage": "https://github.com/openrewrite/rewrite",
@@ -3,21 +3,106 @@
3
3
  *
4
4
  * Moderne Proprietary. Only for use by Moderne customers under the terms of a commercial contract.
5
5
  */
6
- import {ExecutionContext, Recipe, TreeVisitor} from "@openrewrite/rewrite";
7
- import {getMemberKeyName, isArray, Json, JsonVisitor, removeElements, toPlain} from "@openrewrite/rewrite/json";
6
+ import {Cursor, ExecutionContext, ScanningRecipe, TreeVisitor} from "@openrewrite/rewrite";
7
+ import {JavaScriptVisitor, JS} from "@openrewrite/rewrite/javascript";
8
+ import {findMember, getMemberKeyName, isArray, isLiteral, isObject, Json, JsonVisitor, removeElements, toPlain} from "@openrewrite/rewrite/json";
9
+ import {J} from "@openrewrite/rewrite/java";
10
+ import {posix} from "path";
8
11
 
9
- export class RemoveZoneJsPolyfill extends Recipe {
12
+ const ZONELESS_PROVIDERS = new Set(['provideZonelessChangeDetection', 'provideExperimentalZonelessChangeDetection']);
13
+
14
+ /** Test files configure their own `TestBed` providers, which says nothing about how the application bootstraps. */
15
+ const isTestFile = (path: string) => /\.(spec|test)\.[cm]?[jt]sx?$/.test(path);
16
+ const isBeneath = (path: string, dir: string) => !posix.relative(dir, path).startsWith('..');
17
+
18
+ /** The value of a string member of a JSON object, or undefined. */
19
+ function stringMember(obj: Json.Object, key: string): string | undefined {
20
+ const value = findMember(obj, key)?.value;
21
+ return value && isLiteral(value) && typeof value.value === 'string' ? value.value : undefined;
22
+ }
23
+
24
+ /** Whether the file imports one of Angular's zoneless change detection providers from `@angular/core`. */
25
+ function importsZonelessProvider(cu: JS.CompilationUnit): boolean {
26
+ return cu.statements.some(({element}) => {
27
+ if (element.kind !== JS.Kind.Import) return false;
28
+ const imp = element as JS.Import;
29
+ const module = imp.moduleSpecifier?.element;
30
+ if (module?.kind !== J.Kind.Literal || (module as J.Literal).value !== '@angular/core') return false;
31
+ const named = imp.importClause?.namedBindings;
32
+ if (named?.kind !== JS.Kind.NamedImports) return false;
33
+ return (named as JS.NamedImports).elements.elements.some(({element: specifier}) => {
34
+ const imported = (specifier as JS.ImportSpecifier).specifier;
35
+ // `{a as b}` keeps the imported name in the alias' property name, which newer parsers also
36
+ // give as a string literal for `{'a' as b}`.
37
+ const name: J = imported.kind === JS.Kind.Alias ? (imported as JS.Alias).propertyName.element : imported;
38
+ const text = name.kind === J.Kind.Identifier ? (name as J.Identifier).simpleName :
39
+ name.kind === J.Kind.Literal ? (name as J.Literal).value : undefined;
40
+ return typeof text === 'string' && ZONELESS_PROVIDERS.has(text);
41
+ });
42
+ });
43
+ }
44
+
45
+ /**
46
+ * Removes `zone.js` from the `polyfills` of each `angular.json` project that has switched to zoneless
47
+ * change detection.
48
+ *
49
+ * A project counts as zoneless when a non-test source file under its `sourceRoot` (by default `<root>/src`)
50
+ * imports `provideZonelessChangeDetection` (or the experimental provider it replaced) from `@angular/core`.
51
+ * Every other project still runs change detection through Zone.js and keeps the polyfill.
52
+ *
53
+ * @see https://angular.dev/guide/zoneless#removing-zonejs
54
+ */
55
+ export class RemoveZoneJsPolyfill extends ScanningRecipe<Set<string>> {
10
56
  readonly name = "org.openrewrite.angular.migration.remove-zone-js-polyfill";
11
57
  readonly displayName: string = "Remove zone.js polyfill from angular.json";
12
- readonly description: string = "Removes zone.js entries from the `polyfills` array in `angular.json`. Angular 20 supports zoneless change detection via `provideZonelessChangeDetection()`, making the zone.js polyfill unnecessary.";
58
+ readonly description: string = "Removes `zone.js` entries from the `polyfills` arrays in `angular.json` for projects " +
59
+ "that provide zoneless change detection with `provideZonelessChangeDetection()`. Projects that still use " +
60
+ "Zone.js-based change detection keep the polyfill.";
13
61
 
14
- async editor(): Promise<TreeVisitor<any, ExecutionContext>> {
62
+ initialValue(_ctx: ExecutionContext): Set<string> {
63
+ return new Set();
64
+ }
65
+
66
+ async scanner(zonelessFiles: Set<string>): Promise<TreeVisitor<any, ExecutionContext>> {
67
+ return new class extends JavaScriptVisitor<ExecutionContext> {
68
+ protected async visitJsCompilationUnit(cu: JS.CompilationUnit, _ctx: ExecutionContext): Promise<J | undefined> {
69
+ if (!isTestFile(cu.sourcePath) && importsZonelessProvider(cu)) {
70
+ zonelessFiles.add(posix.normalize(cu.sourcePath));
71
+ }
72
+ return cu;
73
+ }
74
+ };
75
+ }
76
+
77
+ async editorWithData(zonelessFiles: Set<string>): Promise<TreeVisitor<any, ExecutionContext>> {
15
78
  return new class extends JsonVisitor<ExecutionContext> {
16
79
  protected async visitDocument(doc: Json.Document, p: ExecutionContext): Promise<Json | undefined> {
17
- if (!doc.sourcePath.endsWith('angular.json')) return doc;
80
+ if (!doc.sourcePath.endsWith('angular.json') || zonelessFiles.size === 0) return doc;
18
81
  return super.visitDocument(doc, p);
19
82
  }
20
83
 
84
+ /** Whether the `angular.json` project enclosing the cursor has a zoneless source file. */
85
+ private inZonelessProject(cursor: Cursor): boolean {
86
+ const doc = cursor.firstEnclosing((n): n is Json.Document => n?.kind === Json.Kind.Document);
87
+ // The project is the object directly inside the `projects` object.
88
+ let inner: Json.Object | undefined;
89
+ let outer: Json.Object | undefined;
90
+ for (let c: Cursor | undefined = cursor; c; c = c.parent) {
91
+ const member = c.value as Json.Member;
92
+ if (member?.kind === Json.Kind.Member && getMemberKeyName(member) === 'projects') break;
93
+ if (isObject(c.value)) [inner, outer] = [outer, c.value];
94
+ }
95
+ const project = inner;
96
+ if (!doc || !project) return false;
97
+ // `sourceRoot` is relative to the workspace and defaults to the project's `root` plus `src`, as Angular's builders do.
98
+ const sourceRoot = stringMember(project, 'sourceRoot') ?? posix.join(stringMember(project, 'root') ?? '', 'src');
99
+ const root = posix.join(posix.dirname(doc.sourcePath), sourceRoot);
100
+ for (const file of zonelessFiles) {
101
+ if (isBeneath(file, root)) return true;
102
+ }
103
+ return false;
104
+ }
105
+
21
106
  protected async visitMember(member: Json.Member, p: ExecutionContext): Promise<Json | undefined> {
22
107
  const m = await super.visitMember(member, p) as Json.Member;
23
108
  if (!m) return m;
@@ -29,7 +114,9 @@ export class RemoveZoneJsPolyfill extends Recipe {
29
114
  const entry = toPlain(element);
30
115
  return typeof entry === 'string' && (entry === 'zone.js' || entry.startsWith('zone.js/'));
31
116
  });
32
- return value === m.value ? m : {...m, value} as Json.Member;
117
+
118
+ if (value === m.value || !this.inZonelessProject(this.cursor)) return m;
119
+ return {...m, value} as Json.Member;
33
120
  }
34
121
  }();
35
122
  }