input-names.js 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  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.InputNamesMigration = void 0;
  11. const angular_1 = require("../html-parsing/angular");
  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 template or stylesheet and replaces outdated input
  17. * names to the new input name. Selectors in stylesheets could also target input
  18. * bindings declared as static attribute. See for example:
  19. *
  20. * e.g. `<my-component color="primary">` becomes `my-component[color]`
  21. */
  22. class InputNamesMigration extends migration_1.Migration {
  23. constructor() {
  24. super(...arguments);
  25. /** Change data that upgrades to the specified target version. */
  26. this.data = (0, upgrade_data_1.getVersionUpgradeData)(this, 'inputNames');
  27. // Only enable the migration rule if there is upgrade data.
  28. this.enabled = this.data.length !== 0;
  29. }
  30. visitStylesheet(stylesheet) {
  31. this.data.forEach(name => {
  32. const currentSelector = `[${name.replace}]`;
  33. const updatedSelector = `[${name.replaceWith}]`;
  34. (0, literal_1.findAllSubstringIndices)(stylesheet.content, currentSelector)
  35. .map(offset => stylesheet.start + offset)
  36. .forEach(start => this._replaceInputName(stylesheet.filePath, start, currentSelector.length, updatedSelector));
  37. });
  38. }
  39. visitTemplate(template) {
  40. this.data.forEach(name => {
  41. const limitedTo = name.limitedTo;
  42. const relativeOffsets = [];
  43. if (limitedTo.attributes) {
  44. relativeOffsets.push(...(0, angular_1.findInputsOnElementWithAttr)(template.content, name.replace, limitedTo.attributes));
  45. }
  46. if (limitedTo.elements) {
  47. relativeOffsets.push(...(0, angular_1.findInputsOnElementWithTag)(template.content, name.replace, limitedTo.elements));
  48. }
  49. relativeOffsets
  50. .map(offset => template.start + offset)
  51. .forEach(start => this._replaceInputName(template.filePath, start, name.replace.length, name.replaceWith));
  52. });
  53. }
  54. _replaceInputName(filePath, start, width, newName) {
  55. this.fileSystem.edit(filePath).remove(start, width).insertRight(start, newName);
  56. }
  57. }
  58. exports.InputNamesMigration = InputNamesMigration;
  59. //# sourceMappingURL=input-names.js.map