input-names.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. "use strict";
  2. /**
  3. * Use of this source code is governed by an MIT-style license that can be
  4. * found in the LICENSE file at https://github.com/NG-ZORRO/ng-zorro-antd/blob/master/LICENSE
  5. */
  6. Object.defineProperty(exports, "__esModule", { value: true });
  7. exports.InputNamesMigration = void 0;
  8. const schematics_1 = require("@angular/cdk/schematics");
  9. const upgrade_data_1 = require("../upgrade-data");
  10. /**
  11. * Migration that walks through every template or stylesheet and replaces outdated input
  12. * names to the new input name. Selectors in stylesheets could also target input
  13. * bindings declared as static attribute. See for example:
  14. *
  15. * e.g. `<my-component color="primary">` becomes `my-component[color]`
  16. */
  17. class InputNamesMigration extends schematics_1.Migration {
  18. constructor() {
  19. super(...arguments);
  20. /** Change data that upgrades to the specified target version. */
  21. this.data = (0, upgrade_data_1.getVersionUpgradeData)(this, 'inputNames');
  22. // Only enable the migration rule if there is upgrade data.
  23. this.enabled = this.data.length !== 0;
  24. }
  25. visitStylesheet(stylesheet) {
  26. this.data.forEach(name => {
  27. const currentSelector = `[${name.replace}]`;
  28. const updatedSelector = `[${name.replaceWith}]`;
  29. (0, schematics_1.findAllSubstringIndices)(stylesheet.content, currentSelector)
  30. .map(offset => stylesheet.start + offset)
  31. .forEach(start => this._replaceInputName(stylesheet.filePath, start, currentSelector.length, updatedSelector));
  32. });
  33. }
  34. visitTemplate(template) {
  35. this.data.forEach(name => {
  36. const limitedTo = name.limitedTo;
  37. const relativeOffsets = [];
  38. if (limitedTo.attributes) {
  39. relativeOffsets.push(...(0, schematics_1.findInputsOnElementWithAttr)(template.content, name.replace, limitedTo.attributes));
  40. }
  41. if (limitedTo.elements) {
  42. relativeOffsets.push(...(0, schematics_1.findInputsOnElementWithTag)(template.content, name.replace, limitedTo.elements));
  43. }
  44. relativeOffsets
  45. .map(offset => template.start + offset)
  46. .forEach(start => this._replaceInputName(template.filePath, start, name.replace.length, name.replaceWith));
  47. });
  48. }
  49. _replaceInputName(filePath, start, width, newName) {
  50. this.fileSystem.edit(filePath).remove(start, width).insertRight(start, newName);
  51. }
  52. }
  53. exports.InputNamesMigration = InputNamesMigration;
  54. //# sourceMappingURL=input-names.js.map