output-names-rule.js 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  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. const migration_rule_1 = require("../../update-tool/migration-rule");
  11. const angular_1 = require("../html-parsing/angular");
  12. const upgrade_data_1 = require("../upgrade-data");
  13. /**
  14. * Rule that walks through every inline or external HTML template and switches
  15. * changed output binding names to the proper new output name.
  16. */
  17. class OutputNamesRule extends migration_rule_1.MigrationRule {
  18. constructor() {
  19. super(...arguments);
  20. /** Change data that upgrades to the specified target version. */
  21. this.data = upgrade_data_1.getVersionUpgradeData(this, 'outputNames');
  22. // Only enable the migration rule if there is upgrade data.
  23. this.ruleEnabled = this.data.length !== 0;
  24. }
  25. visitTemplate(template) {
  26. this.data.forEach(name => {
  27. const whitelist = name.whitelist;
  28. const relativeOffsets = [];
  29. if (whitelist.attributes) {
  30. relativeOffsets.push(...angular_1.findOutputsOnElementWithAttr(template.content, name.replace, whitelist.attributes));
  31. }
  32. if (whitelist.elements) {
  33. relativeOffsets.push(...angular_1.findOutputsOnElementWithTag(template.content, name.replace, whitelist.elements));
  34. }
  35. relativeOffsets.map(offset => template.start + offset)
  36. .forEach(start => this._replaceOutputName(template.filePath, start, name.replace.length, name.replaceWith));
  37. });
  38. }
  39. _replaceOutputName(filePath, start, width, newName) {
  40. const updateRecorder = this.getUpdateRecorder(filePath);
  41. updateRecorder.remove(start, width);
  42. updateRecorder.insertRight(start, newName);
  43. }
  44. }
  45. exports.OutputNamesRule = OutputNamesRule;
  46. //# sourceMappingURL=output-names-rule.js.map