attribute-selectors.js 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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.AttributeSelectorsMigration = 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
  17. * in order to switch deprecated attribute selectors to the updated selector.
  18. */
  19. class AttributeSelectorsMigration extends migration_1.Migration {
  20. constructor() {
  21. super(...arguments);
  22. /** Required upgrade changes for specified target version. */
  23. this.data = (0, upgrade_data_1.getVersionUpgradeData)(this, 'attributeSelectors');
  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. const currentSelector = `[${selector.replace}]`;
  42. const updatedSelector = `[${selector.replaceWith}]`;
  43. (0, literal_1.findAllSubstringIndices)(stylesheet.content, currentSelector)
  44. .map(offset => stylesheet.start + offset)
  45. .forEach(start => this._replaceSelector(stylesheet.filePath, start, {
  46. replace: currentSelector,
  47. replaceWith: updatedSelector,
  48. }));
  49. });
  50. }
  51. _visitStringLiteralLike(literal) {
  52. if (literal.parent && literal.parent.kind !== ts.SyntaxKind.CallExpression) {
  53. return;
  54. }
  55. const literalText = literal.getText();
  56. const filePath = this.fileSystem.resolve(literal.getSourceFile().fileName);
  57. this.data.forEach(selector => {
  58. (0, literal_1.findAllSubstringIndices)(literalText, selector.replace)
  59. .map(offset => literal.getStart() + offset)
  60. .forEach(start => this._replaceSelector(filePath, start, selector));
  61. });
  62. }
  63. _replaceSelector(filePath, start, data) {
  64. this.fileSystem
  65. .edit(filePath)
  66. .remove(start, data.replace.length)
  67. .insertRight(start, data.replaceWith);
  68. }
  69. }
  70. exports.AttributeSelectorsMigration = AttributeSelectorsMigration;
  71. //# sourceMappingURL=attribute-selectors.js.map