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