css-selectors.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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.CssSelectorsMigration = 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
  17. * order to migrate outdated CSS selectors to the new selector.
  18. */
  19. class CssSelectorsMigration 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, 'cssSelectors');
  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(data => {
  34. if (data.replaceIn && !data.replaceIn.html) {
  35. return;
  36. }
  37. (0, literal_1.findAllSubstringIndices)(template.content, data.replace)
  38. .map(offset => template.start + offset)
  39. .forEach(start => this._replaceSelector(template.filePath, start, data));
  40. });
  41. }
  42. visitStylesheet(stylesheet) {
  43. this.data.forEach(data => {
  44. if (data.replaceIn && !data.replaceIn.stylesheet) {
  45. return;
  46. }
  47. (0, literal_1.findAllSubstringIndices)(stylesheet.content, data.replace)
  48. .map(offset => stylesheet.start + offset)
  49. .forEach(start => this._replaceSelector(stylesheet.filePath, start, data));
  50. });
  51. }
  52. _visitStringLiteralLike(node) {
  53. if (node.parent && node.parent.kind !== ts.SyntaxKind.CallExpression) {
  54. return;
  55. }
  56. const textContent = node.getText();
  57. const filePath = this.fileSystem.resolve(node.getSourceFile().fileName);
  58. this.data.forEach(data => {
  59. if (data.replaceIn && !data.replaceIn.tsStringLiterals) {
  60. return;
  61. }
  62. (0, literal_1.findAllSubstringIndices)(textContent, data.replace)
  63. .map(offset => node.getStart() + offset)
  64. .forEach(start => this._replaceSelector(filePath, start, data));
  65. });
  66. }
  67. _replaceSelector(filePath, start, data) {
  68. this.fileSystem
  69. .edit(filePath)
  70. .remove(start, data.replace.length)
  71. .insertRight(start, data.replaceWith);
  72. }
  73. }
  74. exports.CssSelectorsMigration = CssSelectorsMigration;
  75. //# sourceMappingURL=css-selectors.js.map