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