@openrewrite/recipes-angular 1.33.0 → 1.35.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.
Files changed (53) hide show
  1. package/dist/index.d.ts +4 -0
  2. package/dist/index.d.ts.map +1 -1
  3. package/dist/index.js +35 -5
  4. package/dist/index.js.map +1 -1
  5. package/dist/libraries/components/lst.d.ts +7 -0
  6. package/dist/libraries/components/lst.d.ts.map +1 -1
  7. package/dist/libraries/components/lst.js +11 -0
  8. package/dist/libraries/components/lst.js.map +1 -1
  9. package/dist/libraries/components/package-version.d.ts +7 -2
  10. package/dist/libraries/components/package-version.d.ts.map +1 -1
  11. package/dist/libraries/components/package-version.js +5 -3
  12. package/dist/libraries/components/package-version.js.map +1 -1
  13. package/dist/libraries/components/symbols.d.ts +3 -0
  14. package/dist/libraries/components/symbols.d.ts.map +1 -1
  15. package/dist/libraries/components/symbols.js +8 -0
  16. package/dist/libraries/components/symbols.js.map +1 -1
  17. package/dist/libraries/ng-inline-svg/migrate-ng-inline-svg.d.ts +127 -0
  18. package/dist/libraries/ng-inline-svg/migrate-ng-inline-svg.d.ts.map +1 -0
  19. package/dist/libraries/ng-inline-svg/migrate-ng-inline-svg.js +708 -0
  20. package/dist/libraries/ng-inline-svg/migrate-ng-inline-svg.js.map +1 -0
  21. package/dist/libraries/ng2-cookies/migrate-ng2-cookies.d.ts +60 -0
  22. package/dist/libraries/ng2-cookies/migrate-ng2-cookies.d.ts.map +1 -0
  23. package/dist/libraries/ng2-cookies/migrate-ng2-cookies.js +472 -0
  24. package/dist/libraries/ng2-cookies/migrate-ng2-cookies.js.map +1 -0
  25. package/dist/migration/package-major.d.ts +43 -0
  26. package/dist/migration/package-major.d.ts.map +1 -0
  27. package/dist/migration/package-major.js +100 -0
  28. package/dist/migration/package-major.js.map +1 -0
  29. package/dist/migration/replace-inject-flags.js +2 -2
  30. package/dist/migration/replace-inject-flags.js.map +1 -1
  31. package/dist/migration/replace-platform-browser-dynamic.d.ts +32 -0
  32. package/dist/migration/replace-platform-browser-dynamic.d.ts.map +1 -0
  33. package/dist/migration/replace-platform-browser-dynamic.js +212 -0
  34. package/dist/migration/replace-platform-browser-dynamic.js.map +1 -0
  35. package/dist/migration/upgrade-to-angular-20.d.ts.map +1 -1
  36. package/dist/migration/upgrade-to-angular-20.js +4 -1
  37. package/dist/migration/upgrade-to-angular-20.js.map +1 -1
  38. package/dist/migration/use-angular-build-builders.d.ts +51 -0
  39. package/dist/migration/use-angular-build-builders.d.ts.map +1 -0
  40. package/dist/migration/use-angular-build-builders.js +280 -0
  41. package/dist/migration/use-angular-build-builders.js.map +1 -0
  42. package/package.json +2 -2
  43. package/src/index.ts +19 -0
  44. package/src/libraries/components/lst.ts +11 -0
  45. package/src/libraries/components/package-version.ts +7 -7
  46. package/src/libraries/components/symbols.ts +7 -0
  47. package/src/libraries/ng-inline-svg/migrate-ng-inline-svg.ts +613 -0
  48. package/src/libraries/ng2-cookies/migrate-ng2-cookies.ts +425 -0
  49. package/src/migration/package-major.ts +93 -0
  50. package/src/migration/replace-inject-flags.ts +2 -2
  51. package/src/migration/replace-platform-browser-dynamic.ts +197 -0
  52. package/src/migration/upgrade-to-angular-20.ts +4 -1
  53. package/src/migration/use-angular-build-builders.ts +285 -0
@@ -0,0 +1,708 @@
1
+ "use strict";
2
+ /*
3
+ * Copyright 2026 the original author or authors.
4
+ *
5
+ * Moderne Proprietary. Only for use by Moderne customers under the terms of a commercial contract.
6
+ */
7
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
8
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
9
+ return new (P || (P = Promise))(function (resolve, reject) {
10
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
11
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
12
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
13
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
14
+ });
15
+ };
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ exports.MigrateNgInlineSvgToAngularSvgIcon = exports.MigrateNgInlineSvgCodeChanges = exports.FindNgInlineSvgUsage = exports.FindInlineSvgForRootConfig = exports.MoveInlineSvgImports = exports.ProvideSvgIconRegistry = exports.MigrateInlineSvgTemplates = void 0;
18
+ exports.migrateTemplate = migrateTemplate;
19
+ const rewrite_1 = require("@openrewrite/rewrite");
20
+ const text_1 = require("@openrewrite/rewrite/text");
21
+ const javascript_1 = require("@openrewrite/rewrite/javascript");
22
+ const json_1 = require("@openrewrite/rewrite/json");
23
+ const java_1 = require("@openrewrite/rewrite/java");
24
+ const mutative_1 = require("mutative");
25
+ const lst_1 = require("../components/lst");
26
+ const package_major_1 = require("../../migration/package-major");
27
+ const package_version_1 = require("../components/package-version");
28
+ const symbols_1 = require("../components/symbols");
29
+ const template_elements_1 = require("../components/template-elements");
30
+ const path_1 = require("path");
31
+ /** `ng-inline-svg` and `ng-inline-svg-2`, the fork that kept its API for later Angular majors. */
32
+ const MODULES = ['ng-inline-svg', 'ng-inline-svg-2'];
33
+ const ANGULAR_SVG_ICON = 'angular-svg-icon';
34
+ /** The first Angular major that `angular-svg-icon` has a release for. Projects before it, or of no known major, are left. */
35
+ const FIRST_MAJOR = 7;
36
+ /** The inputs of `InlineSVGDirective` that `<svg-icon>` takes under the same name, besides `inlineSVG` itself, by the first major that has it. */
37
+ const CARRIED = new Map([['onSVGLoaded', 18]]);
38
+ /** `SvgIconComponent` is standalone from `angular-svg-icon` 16; before, only `AngularSvgIconModule` can be imported. */
39
+ const FIRST_STANDALONE_MAJOR = 16;
40
+ const supported = (major) => major !== undefined && major >= FIRST_MAJOR;
41
+ /**
42
+ * Whether an attribute only restates a default of `InlineSVGDirective` that `<svg-icon>` shares, so it can be dropped:
43
+ * `cacheSVG` is `true` by default, and `angular-svg-icon` always caches an SVG by its URL.
44
+ */
45
+ const isRedundant = (a) => { var _a; return (0, template_elements_1.bareName)(a) === 'cacheSVG' && ((_a = a.value) === null || _a === void 0 ? void 0 : _a.trim()) === 'true'; };
46
+ /** The inputs and outputs of `InlineSVGDirective` that `<svg-icon>` has no counterpart for. */
47
+ const UNMAPPED = new Set(['resolveSVGUrl', 'replaceContents', 'prepend', 'injectComponent', 'cacheSVG',
48
+ 'setSVGAttributes', 'removeSVGAttributes', 'forceEvalStyles', 'evalScripts', 'fallbackImgUrl', 'fallbackSVG',
49
+ 'onSVGInserted', 'onSVGFailed']);
50
+ const UNMAPPED_TEMPLATE = "`ng-inline-svg` is unmaintained. An `[inlineSVG]` here uses an option that `<svg-icon>` " +
51
+ "of `angular-svg-icon` has no counterpart for, or replaces content of its element, so it was left for a hand migration.";
52
+ const FOR_ROOT = "`angular-svg-icon` has no `baseUrl` or `clientOnly` option: prefix the `src` of each `<svg-icon>` with " +
53
+ "the base URL instead, and pass only an `AngularSvgIconConfig` to `forRoot`.";
54
+ const LEFTOVER = "`ng-inline-svg` is unmaintained. `angular-svg-icon` has no counterpart for this import, a template " +
55
+ "of this project still uses `[inlineSVG]`, which needs it, or the project's Angular major is before " + FIRST_MAJOR +
56
+ " or not known from its `package.json`, or `angular-svg-icon` could not be added to it, so there is no " +
57
+ "`angular-svg-icon` to move it to.";
58
+ const NO_REGISTRY = "`<svg-icon>` needs the `SvgIconRegistryService` that `AngularSvgIconModule.forRoot()` provides, which " +
59
+ "`ng-inline-svg` did not need, and nothing in this project provides it. Add `importProvidersFrom(AngularSvgIconModule.forRoot())`, " +
60
+ "or `provideAngularSvgIcon()` from `angular-svg-icon` 18 on, to the providers of the application.";
61
+ const isNgInlineSvg = (module) => !!module && MODULES.includes(module);
62
+ /**
63
+ * Moves each `[inlineSVG]` of a template into an `<svg-icon>` child of the element it was on: the element keeps its
64
+ * other attributes and structural directives, and the SVG is inserted below it as before, one element deeper.
65
+ */
66
+ function migrateTemplate(template, major) {
67
+ if (!template.includes('inlineSVG'))
68
+ return { text: template, flagged: false };
69
+ const carries = (a) => { var _a; return ((_a = CARRIED.get((0, template_elements_1.bareName)(a))) !== null && _a !== void 0 ? _a : Infinity) <= major; };
70
+ const unmapped = (a) => (UNMAPPED.has((0, template_elements_1.bareName)(a)) || CARRIED.has((0, template_elements_1.bareName)(a)) && !carries(a)) && !isRedundant(a);
71
+ const edits = [];
72
+ let flagged = false;
73
+ for (const element of (0, template_elements_1.parseElements)(template)) {
74
+ const source = element.attributes.find(a => (0, template_elements_1.bareName)(a) === 'inlineSVG');
75
+ if (!source)
76
+ continue;
77
+ const carried = element.attributes.filter(carries);
78
+ const redundant = element.attributes.filter(isRedundant);
79
+ const content = element.closeStart === undefined ? undefined : template.substring(element.openEnd, element.closeStart);
80
+ if (element.attributes.some(unmapped) || content === undefined || content.trim() !== '') {
81
+ flagged = true;
82
+ continue;
83
+ }
84
+ const attribute = (a) => template.substring(a.start, a.end);
85
+ const src = attribute(source).replace('inlineSVG', 'src');
86
+ const icon = `<svg-icon ${[src, ...carried.map(attribute)].join(' ')}></svg-icon>`;
87
+ edits.push(...[source, ...carried, ...redundant].map(a => (0, template_elements_1.removeAttribute)(template, a)));
88
+ edits.push({ start: element.openEnd, end: element.closeStart, text: icon });
89
+ }
90
+ return { text: (0, template_elements_1.applyEdits)(template, edits), flagged };
91
+ }
92
+ /** Moves `[inlineSVG]` to `<svg-icon>` in `.html` templates and in the inline `template` of components. */
93
+ class MigrateInlineSvgTemplates extends rewrite_1.ScanningRecipe {
94
+ /** @param requires a package that the `package.json` of a project must declare for its code to move. */
95
+ constructor(requires) {
96
+ super();
97
+ this.requires = requires;
98
+ this.name = "org.openrewrite.angular.nginlinesvg.MigrateInlineSvgTemplates";
99
+ this.displayName = "Move `[inlineSVG]` to `<svg-icon>` in templates";
100
+ this.description = "Replaces the `[inlineSVG]` directive of `ng-inline-svg` with an `<svg-icon [src]>` of " +
101
+ "`angular-svg-icon` inside the element it was on, carrying `onSVGLoaded` over from Angular 18 on and dropping a `cacheSVG` of `true`, " +
102
+ "since `angular-svg-icon` always caches. The SVG ends up one element " +
103
+ "deeper, inside `<svg-icon>`, so a style that selects it as a direct child needs adjusting. An element that " +
104
+ "uses another option of `ng-inline-svg`, or whose content the directive would replace, is flagged instead. " +
105
+ "Only projects on Angular 7 or later, as their `package.json` tells within one major, are migrated.";
106
+ }
107
+ initialValue(_ctx) {
108
+ return new Map();
109
+ }
110
+ scanner(majors) {
111
+ return __awaiter(this, void 0, void 0, function* () {
112
+ return (0, package_major_1.singleMajorScanner)('@angular/core', majors, this.requires);
113
+ });
114
+ }
115
+ editorWithData(majors) {
116
+ return __awaiter(this, void 0, void 0, function* () {
117
+ const html = new class extends text_1.PlainTextVisitor {
118
+ visitText(text, p) {
119
+ const _super = Object.create(null, {
120
+ visitText: { get: () => super.visitText }
121
+ });
122
+ return __awaiter(this, void 0, void 0, function* () {
123
+ const t = yield _super.visitText.call(this, text, p);
124
+ const major = t && (0, package_version_1.majorFor)(majors, t.sourcePath);
125
+ if (!t || !t.sourcePath.endsWith('.html') || !supported(major))
126
+ return t;
127
+ const { text: updated, flagged } = migrateTemplate(t.text, major);
128
+ const migrated = updated === t.text ? t : Object.assign(Object.assign({}, t), { text: updated, snippets: [] });
129
+ return flagged && !(0, lst_1.hasSearchResult)(migrated, 'ng-inline-svg') ? (0, rewrite_1.foundSearchResult)(migrated, UNMAPPED_TEMPLATE) : migrated;
130
+ });
131
+ }
132
+ }();
133
+ const inline = new class extends javascript_1.JavaScriptVisitor {
134
+ constructor() {
135
+ super(...arguments);
136
+ this.major = 0;
137
+ }
138
+ visitJsCompilationUnit(cu, p) {
139
+ const _super = Object.create(null, {
140
+ visitJsCompilationUnit: { get: () => super.visitJsCompilationUnit }
141
+ });
142
+ return __awaiter(this, void 0, void 0, function* () {
143
+ const major = (0, package_version_1.majorFor)(majors, cu.sourcePath);
144
+ if (!supported(major))
145
+ return cu;
146
+ this.major = major;
147
+ return _super.visitJsCompilationUnit.call(this, cu, p);
148
+ });
149
+ }
150
+ visitPropertyAssignment(property, p) {
151
+ const _super = Object.create(null, {
152
+ visitPropertyAssignment: { get: () => super.visitPropertyAssignment }
153
+ });
154
+ return __awaiter(this, void 0, void 0, function* () {
155
+ var _a, _b;
156
+ const prop = yield _super.visitPropertyAssignment.call(this, property, p);
157
+ if ((0, lst_1.propertyName)((_a = prop === null || prop === void 0 ? void 0 : prop.name) === null || _a === void 0 ? void 0 : _a.element) !== 'template')
158
+ return prop;
159
+ const initializer = prop.initializer;
160
+ if (!initializer || !(0, java_1.isLiteral)(initializer) || typeof initializer.value !== 'string')
161
+ return prop;
162
+ const value = migrateTemplate(initializer.value, this.major);
163
+ const valueSource = migrateTemplate((_b = initializer.valueSource) !== null && _b !== void 0 ? _b : '', this.major).text;
164
+ let literal = valueSource === initializer.valueSource ? initializer :
165
+ (0, mutative_1.create)(initializer, draft => {
166
+ draft.valueSource = valueSource;
167
+ draft.value = value.text;
168
+ });
169
+ if (value.flagged && !(0, lst_1.hasSearchResult)(literal, 'ng-inline-svg'))
170
+ literal = (0, rewrite_1.foundSearchResult)(literal, UNMAPPED_TEMPLATE);
171
+ return literal === initializer ? prop : Object.assign(Object.assign({}, prop), { initializer: literal });
172
+ });
173
+ }
174
+ }();
175
+ return (0, lst_1.inSequence)(html, inline);
176
+ });
177
+ }
178
+ }
179
+ exports.MigrateInlineSvgTemplates = MigrateInlineSvgTemplates;
180
+ function packagesScanner(packages, requires) {
181
+ return new class extends json_1.JsonVisitor {
182
+ visitDocument(doc, _p) {
183
+ return __awaiter(this, void 0, void 0, function* () {
184
+ const path = doc.sourcePath;
185
+ if (path === 'package.json' || path.endsWith('/package.json')) {
186
+ const dir = (0, package_version_1.directoryOf)(path);
187
+ packages.dirs.set(dir, dir);
188
+ const major = (0, package_major_1.declares)(doc, requires) ? (0, package_major_1.singleMajor)(doc, '@angular/core') : undefined;
189
+ if (major !== undefined)
190
+ packages.majors.set(dir, major);
191
+ }
192
+ return doc;
193
+ });
194
+ }
195
+ }();
196
+ }
197
+ const projectOf = (packages, sourcePath) => { var _a; return (_a = (0, package_version_1.majorFor)(packages.dirs, sourcePath)) !== null && _a !== void 0 ? _a : ''; };
198
+ /** The name of a decorator, such as `NgModule`, and the object literal it is given, if it is given one. */
199
+ function decoratorMetadata(a) {
200
+ var _a, _b, _c;
201
+ const first = (_c = (_b = (_a = a.arguments) === null || _a === void 0 ? void 0 : _a.elements) === null || _b === void 0 ? void 0 : _b[0]) === null || _c === void 0 ? void 0 : _c.element;
202
+ return (0, java_1.isIdentifier)(a.annotationType) && first && (0, javascript_1.isObjectLiteral)(first) ?
203
+ { name: a.annotationType.simpleName, metadata: first } : undefined;
204
+ }
205
+ /** The array of a property of a decorator's metadata, such as its `imports`. */
206
+ function arrayProperty(metadata, name) {
207
+ var _a, _b, _c, _d;
208
+ const statements = (_b = (_a = metadata.body) === null || _a === void 0 ? void 0 : _a.statements) !== null && _b !== void 0 ? _b : [];
209
+ for (let index = 0; index < statements.length; index++) {
210
+ const prop = statements[index].element;
211
+ if (prop.kind === javascript_1.JS.Kind.PropertyAssignment && (0, lst_1.propertyName)((_c = prop.name) === null || _c === void 0 ? void 0 : _c.element) === name &&
212
+ ((_d = prop.initializer) === null || _d === void 0 ? void 0 : _d.kind) === java_1.J.Kind.NewArray) {
213
+ return { index, array: prop.initializer };
214
+ }
215
+ }
216
+ return undefined;
217
+ }
218
+ const isNamed = (e, names) => !!e && (0, java_1.isIdentifier)(e) && names.includes(e.simpleName);
219
+ /**
220
+ * Calls `InlineSVGModule.forRoot()` in the `imports` of each `@NgModule` of a project that never calls it, since
221
+ * `ng-inline-svg` provides its cache from the root injector by itself, while `angular-svg-icon` provides its registry only
222
+ * through `forRoot()`. Its registry reuses one that a parent injector has, so a lazy module that calls `forRoot()` too
223
+ * shares the root one. A standalone component of a project with no `@NgModule` to provide it is flagged.
224
+ */
225
+ class ProvideSvgIconRegistry extends rewrite_1.ScanningRecipe {
226
+ /** @param requires a package that the `package.json` of a project must declare for its code to move. */
227
+ constructor(requires) {
228
+ super();
229
+ this.requires = requires;
230
+ this.name = "org.openrewrite.angular.nginlinesvg.ProvideSvgIconRegistry";
231
+ this.displayName = "Provide the `angular-svg-icon` registry where `ng-inline-svg` needed none";
232
+ this.description = "Imports `InlineSVGModule.forRoot()` instead of `InlineSVGModule` in the `@NgModule`s of a " +
233
+ "project that never calls `forRoot()`, so that the `AngularSvgIconModule` it becomes provides the registry that " +
234
+ "`<svg-icon>` needs. A standalone component that imports `ng-inline-svg` in a project with no such `@NgModule` is flagged.";
235
+ }
236
+ initialValue(_ctx) {
237
+ return { dirs: new Map(), majors: new Map(), forRoot: new Set(), ngModuleImports: new Set() };
238
+ }
239
+ scanner(projects) {
240
+ return __awaiter(this, void 0, void 0, function* () {
241
+ const packages = packagesScanner(projects, this.requires);
242
+ const code = new class extends javascript_1.JavaScriptVisitor {
243
+ constructor() {
244
+ super(...arguments);
245
+ this.sourcePath = '';
246
+ }
247
+ visitJsCompilationUnit(cu, p) {
248
+ const _super = Object.create(null, {
249
+ visitJsCompilationUnit: { get: () => super.visitJsCompilationUnit }
250
+ });
251
+ return __awaiter(this, void 0, void 0, function* () {
252
+ this.sourcePath = cu.sourcePath;
253
+ return _super.visitJsCompilationUnit.call(this, cu, p);
254
+ });
255
+ }
256
+ visitMethodInvocation(method, p) {
257
+ const _super = Object.create(null, {
258
+ visitMethodInvocation: { get: () => super.visitMethodInvocation }
259
+ });
260
+ return __awaiter(this, void 0, void 0, function* () {
261
+ var _a;
262
+ if (method.name.simpleName === 'forRoot' && isNamed((_a = method.select) === null || _a === void 0 ? void 0 : _a.element, ['InlineSVGModule'])) {
263
+ projects.forRoot.add(this.sourcePath);
264
+ }
265
+ return _super.visitMethodInvocation.call(this, method, p);
266
+ });
267
+ }
268
+ visitAnnotation(annotation, p) {
269
+ const _super = Object.create(null, {
270
+ visitAnnotation: { get: () => super.visitAnnotation }
271
+ });
272
+ return __awaiter(this, void 0, void 0, function* () {
273
+ var _a;
274
+ const decorator = decoratorMetadata(annotation);
275
+ const imports = (decorator === null || decorator === void 0 ? void 0 : decorator.name) === 'NgModule' ? arrayProperty(decorator.metadata, 'imports') : undefined;
276
+ if ((_a = imports === null || imports === void 0 ? void 0 : imports.array.initializer) === null || _a === void 0 ? void 0 : _a.elements.some(e => isNamed(e.element, ['InlineSVGModule']))) {
277
+ projects.ngModuleImports.add(this.sourcePath);
278
+ }
279
+ return _super.visitAnnotation.call(this, annotation, p);
280
+ });
281
+ }
282
+ }();
283
+ return (0, lst_1.inSequence)(packages, code);
284
+ });
285
+ }
286
+ editorWithData(projects) {
287
+ return __awaiter(this, void 0, void 0, function* () {
288
+ const projectsOf = (files) => new Set([...files].map(f => projectOf(projects, f)));
289
+ const providing = projectsOf(projects.forRoot);
290
+ const withNgModule = projectsOf(projects.ngModuleImports);
291
+ const forRoot = yield forRootCall();
292
+ return new class extends javascript_1.JavaScriptVisitor {
293
+ constructor() {
294
+ super(...arguments);
295
+ this.project = '';
296
+ }
297
+ visitJsCompilationUnit(cu, p) {
298
+ const _super = Object.create(null, {
299
+ visitJsCompilationUnit: { get: () => super.visitJsCompilationUnit }
300
+ });
301
+ return __awaiter(this, void 0, void 0, function* () {
302
+ this.project = projectOf(projects, cu.sourcePath);
303
+ const left = providing.has(this.project) || !supported((0, package_version_1.majorFor)(projects.majors, cu.sourcePath));
304
+ return left ? cu : _super.visitJsCompilationUnit.call(this, cu, p);
305
+ });
306
+ }
307
+ visitAnnotation(annotation, p) {
308
+ const _super = Object.create(null, {
309
+ visitAnnotation: { get: () => super.visitAnnotation }
310
+ });
311
+ return __awaiter(this, void 0, void 0, function* () {
312
+ var _a;
313
+ const a = yield _super.visitAnnotation.call(this, annotation, p);
314
+ const decorator = a && decoratorMetadata(a);
315
+ const imports = decorator && arrayProperty(decorator.metadata, 'imports');
316
+ const elements = (_a = imports === null || imports === void 0 ? void 0 : imports.array.initializer) === null || _a === void 0 ? void 0 : _a.elements;
317
+ if (!decorator || !imports || !elements)
318
+ return a;
319
+ let changed;
320
+ if (decorator.name === 'NgModule') {
321
+ changed = elements.map(e => isNamed(e.element, ['InlineSVGModule']) ?
322
+ Object.assign(Object.assign({}, e), { element: Object.assign(Object.assign({}, forRoot), { prefix: e.element.prefix }) }) : e);
323
+ }
324
+ else if (!withNgModule.has(this.project)) {
325
+ changed = elements.map(e => isNamed(e.element, ['InlineSVGModule', 'InlineSVGDirective']) &&
326
+ !(0, lst_1.hasSearchResult)(e.element, 'SvgIconRegistryService') ? Object.assign(Object.assign({}, e), { element: (0, rewrite_1.foundSearchResult)(e.element, NO_REGISTRY) }) : e);
327
+ }
328
+ else {
329
+ return a;
330
+ }
331
+ if (changed.every((e, i) => e === elements[i]))
332
+ return a;
333
+ const array = Object.assign(Object.assign({}, imports.array), { initializer: Object.assign(Object.assign({}, imports.array.initializer), { elements: changed }) });
334
+ const statements = [...decorator.metadata.body.statements];
335
+ const prop = statements[imports.index].element;
336
+ statements[imports.index] = Object.assign(Object.assign({}, statements[imports.index]), { element: Object.assign(Object.assign({}, prop), { initializer: array }) });
337
+ const metadata = Object.assign(Object.assign({}, decorator.metadata), { body: Object.assign(Object.assign({}, decorator.metadata.body), { statements }) });
338
+ const args = [...a.arguments.elements];
339
+ args[0] = Object.assign(Object.assign({}, args[0]), { element: metadata });
340
+ return Object.assign(Object.assign({}, a), { arguments: Object.assign(Object.assign({}, a.arguments), { elements: args }) });
341
+ });
342
+ }
343
+ }();
344
+ });
345
+ }
346
+ }
347
+ exports.ProvideSvgIconRegistry = ProvideSvgIconRegistry;
348
+ const LAST_CARRIED_MAJOR = Math.max(...CARRIED.values());
349
+ /**
350
+ * Moves the imports of `InlineSVGModule` and `InlineSVGDirective` to `AngularSvgIconModule` and `SvgIconComponent`. A
351
+ * project with a template that `MigrateInlineSvgTemplates` flags still needs `ng-inline-svg` for it, so there each
352
+ * stays, and its counterpart joins it in the `imports` of each decorator: the `<svg-icon>` of the migrated templates
353
+ * then compiles beside the `[inlineSVG]` left.
354
+ */
355
+ class MoveInlineSvgImports extends rewrite_1.ScanningRecipe {
356
+ /** @param requires a package that the `package.json` of a project must declare for its code to move. */
357
+ constructor(requires) {
358
+ super();
359
+ this.requires = requires;
360
+ this.name = "org.openrewrite.angular.nginlinesvg.MoveInlineSvgImports";
361
+ this.displayName = "Move the `ng-inline-svg` imports to `angular-svg-icon`";
362
+ this.description = "Replaces `InlineSVGModule` with `AngularSvgIconModule` and `InlineSVGDirective` with " +
363
+ "`SvgIconComponent`, or with `AngularSvgIconModule` before Angular 16, where `SvgIconComponent` is not standalone. A project with a template whose `[inlineSVG]` is left for a hand migration keeps both, with " +
364
+ "their counterparts beside them in the `imports` of each decorator. So does a project with a component whose " +
365
+ "`templateUrl` names a file that is not among the sources, whose `[inlineSVG]` nothing could move.";
366
+ }
367
+ initialValue(_ctx) {
368
+ return { dirs: new Map(), majors: new Map(), flagged: new Set(), flaggedEarlier: new Set(), html: new Set(), templateUrls: new Map() };
369
+ }
370
+ scanner(templates) {
371
+ return __awaiter(this, void 0, void 0, function* () {
372
+ const packages = packagesScanner(templates, this.requires);
373
+ const record = (sourcePath, template) => {
374
+ if (migrateTemplate(template, LAST_CARRIED_MAJOR).flagged)
375
+ templates.flagged.add(sourcePath);
376
+ if (migrateTemplate(template, FIRST_MAJOR).flagged)
377
+ templates.flaggedEarlier.add(sourcePath);
378
+ };
379
+ const html = new class extends text_1.PlainTextVisitor {
380
+ visitText(text, _p) {
381
+ return __awaiter(this, void 0, void 0, function* () {
382
+ if (text.sourcePath.endsWith('.html')) {
383
+ templates.html.add(path_1.posix.normalize(text.sourcePath));
384
+ record(text.sourcePath, text.text);
385
+ }
386
+ return text;
387
+ });
388
+ }
389
+ }();
390
+ const inline = new class extends javascript_1.JavaScriptVisitor {
391
+ constructor() {
392
+ super(...arguments);
393
+ this.sourcePath = '';
394
+ }
395
+ visitJsCompilationUnit(cu, p) {
396
+ const _super = Object.create(null, {
397
+ visitJsCompilationUnit: { get: () => super.visitJsCompilationUnit }
398
+ });
399
+ return __awaiter(this, void 0, void 0, function* () {
400
+ this.sourcePath = cu.sourcePath;
401
+ return _super.visitJsCompilationUnit.call(this, cu, p);
402
+ });
403
+ }
404
+ visitPropertyAssignment(property, p) {
405
+ const _super = Object.create(null, {
406
+ visitPropertyAssignment: { get: () => super.visitPropertyAssignment }
407
+ });
408
+ return __awaiter(this, void 0, void 0, function* () {
409
+ var _a;
410
+ const initializer = property.initializer;
411
+ const name = (0, lst_1.propertyName)((_a = property.name) === null || _a === void 0 ? void 0 : _a.element);
412
+ if (initializer && (0, java_1.isLiteral)(initializer) && typeof initializer.value === 'string') {
413
+ if (name === 'template')
414
+ record(this.sourcePath, initializer.value);
415
+ if (name === 'templateUrl') {
416
+ templates.templateUrls.set(this.sourcePath, path_1.posix.normalize(path_1.posix.join(path_1.posix.dirname(this.sourcePath), initializer.value)));
417
+ }
418
+ }
419
+ return _super.visitPropertyAssignment.call(this, property, p);
420
+ });
421
+ }
422
+ }();
423
+ return (0, lst_1.inSequence)(packages, (0, lst_1.inSequence)(html, inline));
424
+ });
425
+ }
426
+ editorWithData(templates) {
427
+ return __awaiter(this, void 0, void 0, function* () {
428
+ // A template that is not among the sources, such as one the LST left out, may still use `[inlineSVG]`.
429
+ for (const [component, template] of templates.templateUrls) {
430
+ if (!templates.html.has(template)) {
431
+ templates.flagged.add(component);
432
+ templates.flaggedEarlier.add(component);
433
+ }
434
+ }
435
+ const majorOf = (sourcePath) => (0, package_version_1.majorFor)(templates.majors, sourcePath);
436
+ const flaggedAt = (sourcePath) => {
437
+ const major = majorOf(sourcePath);
438
+ return supported(major) && (major >= LAST_CARRIED_MAJOR ? templates.flagged : templates.flaggedEarlier).has(sourcePath);
439
+ };
440
+ const keeping = new Set([...templates.flagged, ...templates.flaggedEarlier].filter(flaggedAt).map(f => projectOf(templates, f)));
441
+ const movesTo = (directive) => Promise.all(MODULES.flatMap(oldModule => [
442
+ new javascript_1.ChangeImport({ oldModule, oldMember: 'InlineSVGModule', newModule: ANGULAR_SVG_ICON, newMember: 'AngularSvgIconModule' }),
443
+ new javascript_1.ChangeImport({ oldModule, oldMember: 'InlineSVGDirective', newModule: ANGULAR_SVG_ICON, newMember: directive }),
444
+ ].map(change => change.editor())));
445
+ const standaloneMoves = yield movesTo('SvgIconComponent');
446
+ const moduleMoves = yield movesTo('AngularSvgIconModule');
447
+ const [module, moduleForRoot, component] = yield counterparts();
448
+ return new class extends javascript_1.JavaScriptVisitor {
449
+ constructor() {
450
+ super(...arguments);
451
+ this.standalone = true;
452
+ }
453
+ visitJsCompilationUnit(cu, p) {
454
+ const _super = Object.create(null, {
455
+ visitJsCompilationUnit: { get: () => super.visitJsCompilationUnit }
456
+ });
457
+ return __awaiter(this, void 0, void 0, function* () {
458
+ var _a;
459
+ const major = majorOf(cu.sourcePath);
460
+ const imports = cu.statements.some(({ element }) => element.kind === javascript_1.JS.Kind.Import && isNgInlineSvg((0, symbols_1.moduleOf)(element)));
461
+ if (!supported(major) || !imports)
462
+ return cu;
463
+ this.standalone = major >= FIRST_STANDALONE_MAJOR;
464
+ if (keeping.has(projectOf(templates, cu.sourcePath)))
465
+ return _super.visitJsCompilationUnit.call(this, cu, p);
466
+ let result = cu;
467
+ for (const move of this.standalone ? standaloneMoves : moduleMoves) {
468
+ result = (_a = (yield move.visit(result, p, this.cursor.parent))) !== null && _a !== void 0 ? _a : result;
469
+ }
470
+ return result;
471
+ });
472
+ }
473
+ visitAnnotation(annotation, p) {
474
+ const _super = Object.create(null, {
475
+ visitAnnotation: { get: () => super.visitAnnotation }
476
+ });
477
+ return __awaiter(this, void 0, void 0, function* () {
478
+ var _a;
479
+ const a = yield _super.visitAnnotation.call(this, annotation, p);
480
+ const decorator = a && decoratorMetadata(a);
481
+ const imports = decorator && arrayProperty(decorator.metadata, 'imports');
482
+ const elements = (_a = imports === null || imports === void 0 ? void 0 : imports.array.initializer) === null || _a === void 0 ? void 0 : _a.elements;
483
+ if (!decorator || !imports || !elements)
484
+ return a;
485
+ const directive = this.standalone ?
486
+ { member: 'SvgIconComponent', element: component } : { member: 'AngularSvgIconModule', element: module };
487
+ const besideOf = (e) => {
488
+ var _a;
489
+ if (isNamed(e, ['InlineSVGModule']))
490
+ return { member: 'AngularSvgIconModule', element: module };
491
+ if (isNamed(e, ['InlineSVGDirective']))
492
+ return directive;
493
+ if (e.kind === java_1.J.Kind.MethodInvocation && e.name.simpleName === 'forRoot' &&
494
+ isNamed((_a = e.select) === null || _a === void 0 ? void 0 : _a.element, ['InlineSVGModule'])) {
495
+ return { member: 'AngularSvgIconModule', element: moduleForRoot };
496
+ }
497
+ return undefined;
498
+ };
499
+ // a counterpart already there, as one a cycle before added, is not added again
500
+ const present = (b) => elements.some(e => {
501
+ var _a;
502
+ return isNamed(e.element, [b.member]) ||
503
+ (e.element.kind === java_1.J.Kind.MethodInvocation &&
504
+ isNamed((_a = e.element.select) === null || _a === void 0 ? void 0 : _a.element, [b.member]));
505
+ });
506
+ const changed = [];
507
+ const members = new Set();
508
+ for (const e of elements) {
509
+ const beside = besideOf(e.element);
510
+ if (!beside || present(beside)) {
511
+ changed.push(e);
512
+ continue;
513
+ }
514
+ const prefix = e.element.prefix.whitespace.includes('\n') ? e.element.prefix : Object.assign(Object.assign({}, java_1.emptySpace), { whitespace: ' ' });
515
+ changed.push(Object.assign(Object.assign({}, e), { after: java_1.emptySpace, markers: rewrite_1.emptyMarkers }));
516
+ changed.push(Object.assign(Object.assign({}, e), { element: Object.assign(Object.assign({}, beside.element), { prefix }) }));
517
+ members.add(beside.member);
518
+ }
519
+ if (members.size === 0)
520
+ return a;
521
+ for (const member of members)
522
+ (0, javascript_1.maybeAddImport)(this, { module: ANGULAR_SVG_ICON, member });
523
+ const array = Object.assign(Object.assign({}, imports.array), { initializer: Object.assign(Object.assign({}, imports.array.initializer), { elements: changed }) });
524
+ const statements = [...decorator.metadata.body.statements];
525
+ const prop = statements[imports.index].element;
526
+ statements[imports.index] = Object.assign(Object.assign({}, statements[imports.index]), { element: Object.assign(Object.assign({}, prop), { initializer: array }) });
527
+ const metadata = Object.assign(Object.assign({}, decorator.metadata), { body: Object.assign(Object.assign({}, decorator.metadata.body), { statements }) });
528
+ const args = [...a.arguments.elements];
529
+ args[0] = Object.assign(Object.assign({}, args[0]), { element: metadata });
530
+ return Object.assign(Object.assign({}, a), { arguments: Object.assign(Object.assign({}, a.arguments), { elements: args }) });
531
+ });
532
+ }
533
+ }();
534
+ });
535
+ }
536
+ }
537
+ exports.MoveInlineSvgImports = MoveInlineSvgImports;
538
+ /** `AngularSvgIconModule`, `AngularSvgIconModule.forRoot()` and `SvgIconComponent`, parsed. */
539
+ function counterparts() {
540
+ return __awaiter(this, void 0, void 0, function* () {
541
+ const cu = yield new javascript_1.JavaScriptParser().parseOnly({
542
+ sourcePath: 'counterparts.ts',
543
+ text: '[AngularSvgIconModule, AngularSvgIconModule.forRoot(), SvgIconComponent];\n'
544
+ });
545
+ let statement = cu.statements[0].element;
546
+ while (statement.kind !== java_1.J.Kind.NewArray && statement.expression)
547
+ statement = statement.expression;
548
+ return statement.initializer.elements.map(e => (Object.assign(Object.assign({}, e.element), { prefix: java_1.emptySpace })));
549
+ });
550
+ }
551
+ /** `InlineSVGModule.forRoot()`, parsed. */
552
+ function forRootCall() {
553
+ return __awaiter(this, void 0, void 0, function* () {
554
+ const cu = yield new javascript_1.JavaScriptParser().parseOnly({ sourcePath: 'forRoot.ts', text: 'InlineSVGModule.forRoot();\n' });
555
+ let statement = cu.statements[0].element;
556
+ while (statement.kind !== java_1.J.Kind.MethodInvocation && statement.expression)
557
+ statement = statement.expression;
558
+ return statement;
559
+ });
560
+ }
561
+ /** Flags `InlineSVGModule.forRoot(config)`, whose options `angular-svg-icon` does not have. */
562
+ class FindInlineSvgForRootConfig extends rewrite_1.Recipe {
563
+ constructor() {
564
+ super(...arguments);
565
+ this.name = "org.openrewrite.angular.nginlinesvg.FindInlineSvgForRootConfig";
566
+ this.displayName = "Find `InlineSVGModule.forRoot` configuration";
567
+ this.description = "Flags `InlineSVGModule.forRoot` with a configuration, since `angular-svg-icon` has " +
568
+ "no `baseUrl` or `clientOnly` option.";
569
+ }
570
+ editor() {
571
+ return __awaiter(this, void 0, void 0, function* () {
572
+ return new class extends javascript_1.JavaScriptVisitor {
573
+ visitMethodInvocation(method, p) {
574
+ const _super = Object.create(null, {
575
+ visitMethodInvocation: { get: () => super.visitMethodInvocation }
576
+ });
577
+ return __awaiter(this, void 0, void 0, function* () {
578
+ var _a;
579
+ const m = yield _super.visitMethodInvocation.call(this, method, p);
580
+ const select = (_a = m === null || m === void 0 ? void 0 : m.select) === null || _a === void 0 ? void 0 : _a.element;
581
+ if (!select || !(0, java_1.isIdentifier)(select) || select.simpleName !== 'InlineSVGModule' || m.name.simpleName !== 'forRoot')
582
+ return m;
583
+ return (0, lst_1.argumentCount)(m.arguments) > 0 && !(0, lst_1.hasSearchResult)(m, 'angular-svg-icon') ? (0, rewrite_1.foundSearchResult)(m, FOR_ROOT) : m;
584
+ });
585
+ }
586
+ }();
587
+ });
588
+ }
589
+ }
590
+ exports.FindInlineSvgForRootConfig = FindInlineSvgForRootConfig;
591
+ /** Flags each import of `ng-inline-svg` that the migration left, such as `SVGCacheService` or `InlineSVGConfig`. */
592
+ class FindNgInlineSvgUsage extends rewrite_1.Recipe {
593
+ constructor() {
594
+ super(...arguments);
595
+ this.name = "org.openrewrite.angular.nginlinesvg.FindNgInlineSvgUsage";
596
+ this.displayName = "Find `ng-inline-svg` imports";
597
+ this.description = "Flags each import of the unmaintained `ng-inline-svg`, which `angular-svg-icon` replaces, " +
598
+ "including a CommonJS `require`, such as in compiled JavaScript checked in beside its TypeScript.";
599
+ }
600
+ editor() {
601
+ return __awaiter(this, void 0, void 0, function* () {
602
+ return new class extends javascript_1.JavaScriptVisitor {
603
+ visitImportDeclaration(imp, _p) {
604
+ return __awaiter(this, void 0, void 0, function* () {
605
+ // A full migration flags an import before the next cycle moves it, once `angular-svg-icon` is added.
606
+ if (!isNgInlineSvg((0, symbols_1.moduleOf)(imp)))
607
+ return (0, lst_1.withoutSearchResult)(imp, LEFTOVER);
608
+ return (0, lst_1.hasSearchResult)(imp, 'ng-inline-svg') ? imp : (0, rewrite_1.foundSearchResult)(imp, LEFTOVER);
609
+ });
610
+ }
611
+ visitMethodInvocation(method, p) {
612
+ const _super = Object.create(null, {
613
+ visitMethodInvocation: { get: () => super.visitMethodInvocation }
614
+ });
615
+ return __awaiter(this, void 0, void 0, function* () {
616
+ const m = yield _super.visitMethodInvocation.call(this, method, p);
617
+ return m && isNgInlineSvg((0, symbols_1.requiredModule)(m)) && !(0, lst_1.hasSearchResult)(m, 'ng-inline-svg') ? (0, rewrite_1.foundSearchResult)(m, LEFTOVER) : m;
618
+ });
619
+ }
620
+ }();
621
+ });
622
+ }
623
+ }
624
+ exports.FindNgInlineSvgUsage = FindNgInlineSvgUsage;
625
+ /**
626
+ * Migrates `ng-inline-svg`, unmaintained since Angular 12, to `angular-svg-icon`.
627
+ *
628
+ * Both fetch the SVG with `HttpClient` and insert it into the page. `InlineSVGModule` becomes `AngularSvgIconModule`
629
+ * (with `forRoot()` where it had it), `InlineSVGDirective` in the `imports` of a standalone component becomes
630
+ * `SvgIconComponent`, and each `[inlineSVG]` of a template becomes an `<svg-icon [src]>` inside its element.
631
+ *
632
+ * @see https://github.com/arkon/ng-inline-svg
633
+ * @see https://github.com/czeckd/angular-svg-icon
634
+ */
635
+ class MigrateNgInlineSvgCodeChanges extends rewrite_1.Recipe {
636
+ /** @param requires a package that the `package.json` of a project must declare for its code to move. */
637
+ constructor(requires) {
638
+ super();
639
+ this.requires = requires;
640
+ this.name = "org.openrewrite.angular.nginlinesvg.MigrateNgInlineSvgCodeChanges";
641
+ this.displayName = "Migrate `ng-inline-svg` to `angular-svg-icon` (code changes)";
642
+ this.description = "Replaces the unmaintained `ng-inline-svg` with `angular-svg-icon`: `InlineSVGModule` " +
643
+ "becomes `AngularSvgIconModule`, `InlineSVGDirective` becomes `SvgIconComponent` (`AngularSvgIconModule` before " +
644
+ "Angular 16), and each `[inlineSVG]` of a " +
645
+ "template becomes an `<svg-icon [src]>` inside the element it was on. A project that never called `forRoot()` " +
646
+ "imports `AngularSvgIconModule.forRoot()` in its `@NgModule`s, since `<svg-icon>` needs the registry it provides. " +
647
+ "`forRoot` configuration, template " +
648
+ "options without a counterpart, and the imports left over are flagged for a hand migration. A project with a " +
649
+ "flagged template keeps `InlineSVGModule` and `InlineSVGDirective` for it, with their `angular-svg-icon` " +
650
+ "counterparts beside them. Projects before Angular 7, or whose `package.json` does not tell the Angular major " +
651
+ "within one major, are left as they are, with their imports flagged.";
652
+ }
653
+ recipeList() {
654
+ return __awaiter(this, void 0, void 0, function* () {
655
+ return [
656
+ new MigrateInlineSvgTemplates(this.requires),
657
+ new ProvideSvgIconRegistry(this.requires),
658
+ new FindInlineSvgForRootConfig(),
659
+ new MoveInlineSvgImports(this.requires),
660
+ new FindNgInlineSvgUsage(),
661
+ ];
662
+ });
663
+ }
664
+ }
665
+ exports.MigrateNgInlineSvgCodeChanges = MigrateNgInlineSvgCodeChanges;
666
+ /**
667
+ * `angular-svg-icon` of each Angular major from 7 on: 7.x and 8.x take Angular 7 and later as their peer, 9 to 18 are
668
+ * released in step with Angular, and 19 takes Angular 19 and every later major as its peer. Each has `<svg-icon [src]>`
669
+ * and `AngularSvgIconModule.forRoot()`.
670
+ */
671
+ const ANGULAR_SVG_ICON_VERSIONS = [
672
+ { major: 7, version: '^7.2.1' },
673
+ { major: 8, version: '^8.0.0' },
674
+ { major: 9, version: '^9.2.0' },
675
+ ...(0, package_major_1.caretMajors)(10, 18),
676
+ { major: 19, version: '^19.1.1', andLater: true },
677
+ ];
678
+ /**
679
+ * `angular-svg-icon` at the Angular major of each `package.json` that declares `ng-inline-svg`, then the code changes
680
+ * where it was added, in the next cycle, and then `ng-inline-svg` removed where nothing imports it any more.
681
+ */
682
+ class MigrateNgInlineSvgToAngularSvgIcon extends rewrite_1.Recipe {
683
+ constructor() {
684
+ super(...arguments);
685
+ this.name = "org.openrewrite.angular.nginlinesvg.MigrateNgInlineSvgToAngularSvgIcon";
686
+ this.displayName = "Migrate `ng-inline-svg` to `angular-svg-icon`";
687
+ this.description = "Runs `MigrateNgInlineSvgCodeChanges`, which replaces `InlineSVGModule`, " +
688
+ "`InlineSVGDirective` and each `[inlineSVG]` of a template with their `angular-svg-icon` counterparts. Each " +
689
+ "`package.json` that declares `ng-inline-svg` or `ng-inline-svg-2` gets `angular-svg-icon` of its Angular major, " +
690
+ "from 7 on, and loses `ng-inline-svg` and `ng-inline-svg-2` once none of its sources imports them. Code moves " +
691
+ "only in a project whose `package.json` declares `angular-svg-icon`, so not in one where npm refused to add it, " +
692
+ "such as one whose lock already has a peer conflict.";
693
+ // The code changes read the `angular-svg-icon` this run adds, and the removals the imports they leave, so each
694
+ // lands a cycle after the step before it.
695
+ this.causesAnotherCycle = true;
696
+ }
697
+ recipeList() {
698
+ return __awaiter(this, void 0, void 0, function* () {
699
+ return [
700
+ ...yield (0, package_major_1.addDependencyByMajor)(ANGULAR_SVG_ICON, 'dependencies', MODULES, '@angular/core', ANGULAR_SVG_ICON_VERSIONS),
701
+ new MigrateNgInlineSvgCodeChanges(ANGULAR_SVG_ICON),
702
+ ...yield Promise.all(MODULES.map(package_major_1.removeDependencyUnlessImported)),
703
+ ];
704
+ });
705
+ }
706
+ }
707
+ exports.MigrateNgInlineSvgToAngularSvgIcon = MigrateNgInlineSvgToAngularSvgIcon;
708
+ //# sourceMappingURL=migrate-ng-inline-svg.js.map