element-selectors.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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.dev/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 = (0, 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. (0, 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. (0, 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. (0, 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
  60. .edit(filePath)
  61. .remove(start, data.replace.length)
  62. .insertRight(start, data.replaceWith);
  63. }
  64. }
  65. exports.ElementSelectorsMigration = ElementSelectorsMigration;
  66. //# sourceMappingURL=element-selectors.js.map