element-selectors-rule.js 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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 ts = require("typescript");
  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 string literal, template and stylesheet in order
  16. * to migrate outdated element selectors to the new one.
  17. */
  18. class ElementSelectorsRule extends migration_rule_1.MigrationRule {
  19. constructor() {
  20. super(...arguments);
  21. /** Change data that upgrades to the specified target version. */
  22. this.data = upgrade_data_1.getVersionUpgradeData(this, 'elementSelectors');
  23. // Only enable the migration rule if there is upgrade data.
  24. this.ruleEnabled = this.data.length !== 0;
  25. }
  26. visitNode(node) {
  27. if (ts.isStringLiteralLike(node)) {
  28. this._visitStringLiteralLike(node);
  29. }
  30. }
  31. visitTemplate(template) {
  32. this.data.forEach(selector => {
  33. literal_1.findAllSubstringIndices(template.content, selector.replace)
  34. .map(offset => template.start + offset)
  35. .forEach(start => this._replaceSelector(template.filePath, start, selector));
  36. });
  37. }
  38. visitStylesheet(stylesheet) {
  39. this.data.forEach(selector => {
  40. literal_1.findAllSubstringIndices(stylesheet.content, selector.replace)
  41. .map(offset => stylesheet.start + offset)
  42. .forEach(start => this._replaceSelector(stylesheet.filePath, start, selector));
  43. });
  44. }
  45. _visitStringLiteralLike(node) {
  46. if (node.parent && node.parent.kind !== ts.SyntaxKind.CallExpression) {
  47. return;
  48. }
  49. const textContent = node.getText();
  50. const filePath = node.getSourceFile().fileName;
  51. this.data.forEach(selector => {
  52. literal_1.findAllSubstringIndices(textContent, selector.replace)
  53. .map(offset => node.getStart() + offset)
  54. .forEach(start => this._replaceSelector(filePath, start, selector));
  55. });
  56. }
  57. _replaceSelector(filePath, start, data) {
  58. const updateRecorder = this.getUpdateRecorder(filePath);
  59. updateRecorder.remove(start, data.replace.length);
  60. updateRecorder.insertRight(start, data.replaceWith);
  61. }
  62. }
  63. exports.ElementSelectorsRule = ElementSelectorsRule;
  64. //# sourceMappingURL=element-selectors-rule.js.map