1 | "use strict";
|
---|
2 | /**
|
---|
3 | * @license
|
---|
4 | * Copyright Google LLC All Rights Reserved.
|
---|
5 | *
|
---|
6 | * Use of this source code is governed by an MIT-style license that can be
|
---|
7 | * found in the LICENSE file at https://angular.io/license
|
---|
8 | */
|
---|
9 | Object.defineProperty(exports, "__esModule", { value: true });
|
---|
10 | exports.ElementSelectorsMigration = void 0;
|
---|
11 | const ts = require("typescript");
|
---|
12 | const migration_1 = require("../../update-tool/migration");
|
---|
13 | const literal_1 = require("../typescript/literal");
|
---|
14 | const upgrade_data_1 = require("../upgrade-data");
|
---|
15 | /**
|
---|
16 | * Migration that walks through every string literal, template and stylesheet in order
|
---|
17 | * to migrate outdated element selectors to the new one.
|
---|
18 | */
|
---|
19 | class ElementSelectorsMigration extends migration_1.Migration {
|
---|
20 | constructor() {
|
---|
21 | super(...arguments);
|
---|
22 | /** Change data that upgrades to the specified target version. */
|
---|
23 | this.data = upgrade_data_1.getVersionUpgradeData(this, 'elementSelectors');
|
---|
24 | // Only enable the migration rule if there is upgrade data.
|
---|
25 | this.enabled = this.data.length !== 0;
|
---|
26 | }
|
---|
27 | visitNode(node) {
|
---|
28 | if (ts.isStringLiteralLike(node)) {
|
---|
29 | this._visitStringLiteralLike(node);
|
---|
30 | }
|
---|
31 | }
|
---|
32 | visitTemplate(template) {
|
---|
33 | this.data.forEach(selector => {
|
---|
34 | literal_1.findAllSubstringIndices(template.content, selector.replace)
|
---|
35 | .map(offset => template.start + offset)
|
---|
36 | .forEach(start => this._replaceSelector(template.filePath, start, selector));
|
---|
37 | });
|
---|
38 | }
|
---|
39 | visitStylesheet(stylesheet) {
|
---|
40 | this.data.forEach(selector => {
|
---|
41 | literal_1.findAllSubstringIndices(stylesheet.content, selector.replace)
|
---|
42 | .map(offset => stylesheet.start + offset)
|
---|
43 | .forEach(start => this._replaceSelector(stylesheet.filePath, start, selector));
|
---|
44 | });
|
---|
45 | }
|
---|
46 | _visitStringLiteralLike(node) {
|
---|
47 | if (node.parent && node.parent.kind !== ts.SyntaxKind.CallExpression) {
|
---|
48 | return;
|
---|
49 | }
|
---|
50 | const textContent = node.getText();
|
---|
51 | const filePath = this.fileSystem.resolve(node.getSourceFile().fileName);
|
---|
52 | this.data.forEach(selector => {
|
---|
53 | literal_1.findAllSubstringIndices(textContent, selector.replace)
|
---|
54 | .map(offset => node.getStart() + offset)
|
---|
55 | .forEach(start => this._replaceSelector(filePath, start, selector));
|
---|
56 | });
|
---|
57 | }
|
---|
58 | _replaceSelector(filePath, start, data) {
|
---|
59 | this.fileSystem.edit(filePath)
|
---|
60 | .remove(start, data.replace.length)
|
---|
61 | .insertRight(start, data.replaceWith);
|
---|
62 | }
|
---|
63 | }
|
---|
64 | exports.ElementSelectorsMigration = ElementSelectorsMigration;
|
---|
65 | //# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiZWxlbWVudC1zZWxlY3RvcnMuanMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi8uLi8uLi8uLi8uLi8uLi8uLi8uLi9zcmMvY2RrL3NjaGVtYXRpY3MvbmctdXBkYXRlL21pZ3JhdGlvbnMvZWxlbWVudC1zZWxlY3RvcnMudHMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6IjtBQUFBOzs7Ozs7R0FNRzs7O0FBRUgsaUNBQWlDO0FBR2pDLDJEQUFzRDtBQUV0RCxtREFBOEQ7QUFDOUQsa0RBQW1FO0FBRW5FOzs7R0FHRztBQUNILE1BQWEseUJBQTBCLFNBQVEscUJBQXNCO0lBQXJFOztRQUNFLGlFQUFpRTtRQUNqRSxTQUFJLEdBQUcsb0NBQXFCLENBQUMsSUFBSSxFQUFFLGtCQUFrQixDQUFDLENBQUM7UUFFdkQsMkRBQTJEO1FBQzNELFlBQU8sR0FBWSxJQUFJLENBQUMsSUFBSSxDQUFDLE1BQU0sS0FBSyxDQUFDLENBQUM7SUE2QzVDLENBQUM7SUEzQ1UsU0FBUyxDQUFDLElBQ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