Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(material/schematics): initial setup for template migrations (#24515
) * feat(material/schematics): initial setup for template migrations * created TemplateMigration for migrating templates * added it to the migrations for the mdc-migration schematic
- Loading branch information
1 parent
a12f268
commit f24a49f
Showing
5 changed files
with
83 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
23 changes: 23 additions & 0 deletions
23
src/material/schematics/ng-generate/mdc-migration/rules/template-migration.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import {createTestApp, patchDevkitTreeToExposeTypeScript} from '@angular/cdk/schematics/testing'; | ||
import {SchematicTestRunner, UnitTestTree} from '@angular-devkit/schematics/testing'; | ||
import {createNewTestRunner, migrateComponent, TEMPLATE_FILE} from './components/test-setup-helper'; | ||
|
||
describe('template migrations', () => { | ||
let runner: SchematicTestRunner; | ||
let cliAppTree: UnitTestTree; | ||
|
||
async function runMigrationTest(oldFileContent: string, newFileContent: string) { | ||
cliAppTree.overwrite(TEMPLATE_FILE, oldFileContent); | ||
const tree = await migrateComponent('card', runner, cliAppTree); | ||
expect(tree.readContent(TEMPLATE_FILE)).toBe(newFileContent); | ||
} | ||
|
||
beforeEach(async () => { | ||
runner = createNewTestRunner(); | ||
cliAppTree = patchDevkitTreeToExposeTypeScript(await createTestApp(runner)); | ||
}); | ||
|
||
it('should do nothing yet', async () => { | ||
await runMigrationTest('<h1>Hello</h1>', '<h1>Hello</h1>'); | ||
}); | ||
}); |
56 changes: 56 additions & 0 deletions
56
src/material/schematics/ng-generate/mdc-migration/rules/template-migration.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
/** | ||
* @license | ||
* Copyright Google LLC All Rights Reserved. | ||
* | ||
* Use of this source code is governed by an MIT-style license that can be | ||
* found in the LICENSE file at https://angular.io/license | ||
*/ | ||
|
||
import {Migration, ResolvedResource} from '@angular/cdk/schematics'; | ||
import {SchematicContext} from '@angular-devkit/schematics'; | ||
import {StyleMigrator} from './style-migrator'; | ||
import * as compiler from '@angular/compiler'; | ||
|
||
/** | ||
* Traverses the given tree of nodes and runs the given callbacks for each Element node encountered. | ||
* | ||
* Note that updates to the start tags of html element should be done in the postorder callback, | ||
* and updates to the end tags of html elements should be done in the preorder callback to avoid | ||
* issues with line collisions. | ||
* | ||
* @param nodes The nodes of the ast from a parsed template. | ||
* @param preorderCallback A function that gets run for each Element node in a preorder traversal. | ||
* @param postorderCallback A function that gets run for each Element node in a postorder traversal. | ||
*/ | ||
function visitElements( | ||
nodes: compiler.TmplAstNode[], | ||
preorderCallback: (node: compiler.TmplAstElement) => void = () => {}, | ||
postorderCallback: (node: compiler.TmplAstElement) => void = () => {}, | ||
): void { | ||
for (let i = 0; i < nodes.length; i++) { | ||
const node = nodes[i]; | ||
if (node instanceof compiler.TmplAstElement) { | ||
preorderCallback(node); | ||
visitElements(node.children, preorderCallback, postorderCallback); | ||
postorderCallback(node); | ||
} | ||
} | ||
} | ||
|
||
export class TemplateMigration extends Migration<StyleMigrator[], SchematicContext> { | ||
enabled = true; | ||
|
||
override visitTemplate(template: ResolvedResource) { | ||
const ast = compiler.parseTemplate(template.content, template.filePath, { | ||
preserveWhitespaces: true, | ||
preserveLineEndings: true, | ||
leadingTriviaChars: [], | ||
}); | ||
|
||
visitElements(ast.nodes, node => { | ||
// TODO(wagnermaciel): implement the migration updates. | ||
}); | ||
|
||
this.fileSystem.overwrite(template.filePath, template.content); | ||
} | ||
} |