attribute-selectors-rule.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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. * Migration rule that walks through every string literal, template and stylesheet
  16. * in order to switch deprecated attribute selectors to the updated selector.
  17. */
  18. class AttributeSelectorsRule extends migration_rule_1.MigrationRule {
  19. constructor() {
  20. super(...arguments);
  21. /** Required upgrade changes for specified target version. */
  22. this.data = upgrade_data_1.getVersionUpgradeData(this, 'attributeSelectors');
  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. const currentSelector = `[${selector.replace}]`;
  41. const updatedSelector = `[${selector.replaceWith}]`;
  42. literal_1.findAllSubstringIndices(stylesheet.content, currentSelector)
  43. .map(offset => stylesheet.start + offset)
  44. .forEach(start => this._replaceSelector(stylesheet.filePath, start, { replace: currentSelector, replaceWith: updatedSelector }));
  45. });
  46. }
  47. _visitStringLiteralLike(literal) {
  48. if (literal.parent && literal.parent.kind !== ts.SyntaxKind.CallExpression) {
  49. return;
  50. }
  51. const literalText = literal.getText();
  52. const filePath = literal.getSourceFile().fileName;
  53. this.data.forEach(selector => {
  54. literal_1.findAllSubstringIndices(literalText, selector.replace)
  55. .map(offset => literal.getStart() + offset)
  56. .forEach(start => this._replaceSelector(filePath, start, selector));
  57. });
  58. }
  59. _replaceSelector(filePath, start, data) {
  60. const updateRecorder = this.getUpdateRecorder(filePath);
  61. updateRecorder.remove(start, data.replace.length);
  62. updateRecorder.insertRight(start, data.replaceWith);
  63. }
  64. }
  65. exports.AttributeSelectorsRule = AttributeSelectorsRule;
  66. //# sourceMappingURL=attribute-selectors-rule.js.map