css-selectors-rule.js 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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
  16. * order to migrate outdated CSS selectors to the new selector.
  17. */
  18. class CssSelectorsRule 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, 'cssSelectors');
  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(data => {
  33. if (data.whitelist && !data.whitelist.html) {
  34. return;
  35. }
  36. literal_1.findAllSubstringIndices(template.content, data.replace)
  37. .map(offset => template.start + offset)
  38. .forEach(start => this._replaceSelector(template.filePath, start, data));
  39. });
  40. }
  41. visitStylesheet(stylesheet) {
  42. this.data.forEach(data => {
  43. if (data.whitelist && !data.whitelist.stylesheet) {
  44. return;
  45. }
  46. literal_1.findAllSubstringIndices(stylesheet.content, data.replace)
  47. .map(offset => stylesheet.start + offset)
  48. .forEach(start => this._replaceSelector(stylesheet.filePath, start, data));
  49. });
  50. }
  51. _visitStringLiteralLike(node) {
  52. if (node.parent && node.parent.kind !== ts.SyntaxKind.CallExpression) {
  53. return;
  54. }
  55. const textContent = node.getText();
  56. const filePath = node.getSourceFile().fileName;
  57. this.data.forEach(data => {
  58. if (data.whitelist && !data.whitelist.strings) {
  59. return;
  60. }
  61. literal_1.findAllSubstringIndices(textContent, data.replace)
  62. .map(offset => node.getStart() + offset)
  63. .forEach(start => this._replaceSelector(filePath, start, data));
  64. });
  65. }
  66. _replaceSelector(filePath, start, data) {
  67. const updateRecorder = this.getUpdateRecorder(filePath);
  68. updateRecorder.remove(start, data.replace.length);
  69. updateRecorder.insertRight(start, data.replaceWith);
  70. }
  71. }
  72. exports.CssSelectorsRule = CssSelectorsRule;
  73. //# sourceMappingURL=css-selectors-rule.js.map