|
6 | 6 | * found in the LICENSE file at https://angular.dev/license |
7 | 7 | */ |
8 | 8 |
|
9 | | -import {Path} from '@angular-devkit/core'; |
10 | | -import {Rule, Tree} from '@angular-devkit/schematics'; |
11 | | -import ts from 'typescript'; |
12 | | - |
13 | | -/** Tag name of the clusterer component. */ |
14 | | -const TAG_NAME = 'map-marker-clusterer'; |
15 | | - |
16 | | -/** Module from which the clusterer is being imported. */ |
17 | | -const MODULE_NAME = '@angular/google-maps'; |
18 | | - |
19 | | -/** Old name of the clusterer class. */ |
20 | | -const CLASS_NAME = 'MapMarkerClusterer'; |
21 | | - |
22 | | -/** New name of the clusterer class. */ |
23 | | -const DEPRECATED_CLASS_NAME = 'DeprecatedMapMarkerClusterer'; |
24 | | - |
25 | | -/** Entry point for the migration schematics with target of Angular Material v19 */ |
26 | | -export function updateToV19(): Rule { |
27 | | - return tree => { |
28 | | - tree.visit(path => { |
29 | | - if (path.includes('node_modules')) { |
30 | | - return; |
31 | | - } |
32 | | - |
33 | | - if (path.endsWith('.html')) { |
34 | | - const content = tree.read(path)?.toString(); |
35 | | - |
36 | | - if (content && content.includes('<' + TAG_NAME)) { |
37 | | - tree.overwrite(path, migrateHtml(content)); |
38 | | - } |
39 | | - } else if (path.endsWith('.ts') && !path.endsWith('.d.ts')) { |
40 | | - migrateTypeScript(path, tree); |
41 | | - } |
42 | | - }); |
43 | | - }; |
44 | | -} |
45 | | - |
46 | | -/** Migrates an HTML template from the old tag name to the new one. */ |
47 | | -function migrateHtml(content: string): string { |
48 | | - return content |
49 | | - .replace(/<map-marker-clusterer/g, '<deprecated-map-marker-clusterer') |
50 | | - .replace(/<\/map-marker-clusterer/g, '</deprecated-map-marker-clusterer'); |
51 | | -} |
52 | | - |
53 | | -/** Migrates a TypeScript file from the old tag and class names to the new ones. */ |
54 | | -function migrateTypeScript(path: Path, tree: Tree) { |
55 | | - const content = tree.read(path)?.toString(); |
56 | | - |
57 | | - // Exit early if none of the symbols we're looking for are mentioned. |
58 | | - if ( |
59 | | - !content || |
60 | | - (!content.includes('<' + TAG_NAME) && |
61 | | - !content.includes(MODULE_NAME) && |
62 | | - !content.includes(CLASS_NAME)) |
63 | | - ) { |
64 | | - return; |
65 | | - } |
66 | | - |
67 | | - const sourceFile = ts.createSourceFile(path, content, ts.ScriptTarget.Latest, true); |
68 | | - const toMigrate = findTypeScriptNodesToMigrate(sourceFile); |
69 | | - |
70 | | - if (toMigrate.length === 0) { |
71 | | - return; |
72 | | - } |
73 | | - |
74 | | - const printer = ts.createPrinter(); |
75 | | - const update = tree.beginUpdate(path); |
76 | | - |
77 | | - for (const node of toMigrate) { |
78 | | - let replacement: ts.Node; |
79 | | - |
80 | | - if (ts.isStringLiteralLike(node)) { |
81 | | - // Strings should be migrated as if they're HTML. |
82 | | - if (ts.isStringLiteral(node)) { |
83 | | - replacement = ts.factory.createStringLiteral( |
84 | | - migrateHtml(node.text), |
85 | | - node.getText()[0] === `'`, |
86 | | - ); |
87 | | - } else { |
88 | | - replacement = ts.factory.createNoSubstitutionTemplateLiteral(migrateHtml(node.text)); |
89 | | - } |
90 | | - } else { |
91 | | - // Imports/exports should preserve the old name, but import the clusterer using the new one. |
92 | | - const propertyName = ts.factory.createIdentifier(DEPRECATED_CLASS_NAME); |
93 | | - const name = node.name as ts.Identifier; |
94 | | - |
95 | | - replacement = ts.isImportSpecifier(node) |
96 | | - ? ts.factory.updateImportSpecifier(node, node.isTypeOnly, propertyName, name) |
97 | | - : ts.factory.updateExportSpecifier(node, node.isTypeOnly, propertyName, name); |
98 | | - } |
99 | | - |
100 | | - update |
101 | | - .remove(node.getStart(), node.getWidth()) |
102 | | - .insertLeft( |
103 | | - node.getStart(), |
104 | | - printer.printNode(ts.EmitHint.Unspecified, replacement, sourceFile), |
105 | | - ); |
106 | | - } |
107 | | - |
108 | | - tree.commitUpdate(update); |
109 | | -} |
110 | | - |
111 | | -/** Finds the TypeScript nodes that need to be migrated from a specific file. */ |
112 | | -function findTypeScriptNodesToMigrate(sourceFile: ts.SourceFile) { |
113 | | - const results: (ts.StringLiteralLike | ts.ImportSpecifier | ts.ExportSpecifier)[] = []; |
114 | | - |
115 | | - sourceFile.forEachChild(function walk(node) { |
116 | | - // Most likely a template using the clusterer. |
117 | | - if (ts.isStringLiteral(node) && node.text.includes('<' + TAG_NAME)) { |
118 | | - results.push(node); |
119 | | - } else if ( |
120 | | - // Import/export referencing the clusterer. |
121 | | - (ts.isImportDeclaration(node) || ts.isExportDeclaration(node)) && |
122 | | - node.moduleSpecifier && |
123 | | - ts.isStringLiteralLike(node.moduleSpecifier) && |
124 | | - node.moduleSpecifier.text === MODULE_NAME |
125 | | - ) { |
126 | | - const bindings = ts.isImportDeclaration(node) |
127 | | - ? node.importClause?.namedBindings |
128 | | - : node.exportClause; |
129 | | - |
130 | | - if (bindings && (ts.isNamedImports(bindings) || ts.isNamedExports(bindings))) { |
131 | | - bindings.elements.forEach(element => { |
132 | | - const symbolName = element.propertyName || element.name; |
133 | | - |
134 | | - if (ts.isIdentifier(symbolName) && symbolName.text === CLASS_NAME) { |
135 | | - results.push(element); |
136 | | - } |
137 | | - }); |
138 | | - } |
139 | | - } else { |
140 | | - node.forEachChild(walk); |
141 | | - } |
142 | | - }); |
143 | | - |
144 | | - // Sort the results in reverse order to make applying the updates easier. |
145 | | - return results.sort((a, b) => b.getStart() - a.getStart()); |
146 | | -} |
| 9 | +/** Entry point for the migration schematics with target of Angular Material v20 */ |
| 10 | +export function updateToV20(): void {} |
0 commit comments