class-names-rule.js 4.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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 imports_1 = require("../typescript/imports");
  13. const module_specifiers_1 = require("../typescript/module-specifiers");
  14. const upgrade_data_1 = require("../upgrade-data");
  15. /**
  16. * Rule that walks through every identifier that is part of Angular Material or thr CDK
  17. * and replaces the outdated name with the new one if specified in the upgrade data.
  18. */
  19. class ClassNamesRule extends migration_rule_1.MigrationRule {
  20. constructor() {
  21. super(...arguments);
  22. /** Change data that upgrades to the specified target version. */
  23. this.data = upgrade_data_1.getVersionUpgradeData(this, 'classNames');
  24. /**
  25. * List of identifier names that have been imported from `@angular/material` or `@angular/cdk`
  26. * in the current source file and therefore can be considered trusted.
  27. */
  28. this.trustedIdentifiers = new Set();
  29. /** List of namespaces that have been imported from `@angular/material` or `@angular/cdk`. */
  30. this.trustedNamespaces = new Set();
  31. // Only enable the migration rule if there is upgrade data.
  32. this.ruleEnabled = this.data.length !== 0;
  33. }
  34. visitNode(node) {
  35. if (ts.isIdentifier(node)) {
  36. this._visitIdentifier(node);
  37. }
  38. }
  39. /** Method that is called for every identifier inside of the specified project. */
  40. _visitIdentifier(identifier) {
  41. // For identifiers that aren't listed in the className data, the whole check can be
  42. // skipped safely.
  43. if (!this.data.some(data => data.replace === identifier.text)) {
  44. return;
  45. }
  46. // For namespace imports that are referring to Angular Material or the CDK, we store the
  47. // namespace name in order to be able to safely find identifiers that don't belong to the
  48. // developer's application.
  49. if (imports_1.isNamespaceImportNode(identifier) && module_specifiers_1.isMaterialImportDeclaration(identifier)) {
  50. this.trustedNamespaces.add(identifier.text);
  51. return this._createFailureWithReplacement(identifier);
  52. }
  53. // For export declarations that are referring to Angular Material or the CDK, the identifier
  54. // can be immediately updated to the new name.
  55. if (imports_1.isExportSpecifierNode(identifier) && module_specifiers_1.isMaterialExportDeclaration(identifier)) {
  56. return this._createFailureWithReplacement(identifier);
  57. }
  58. // For import declarations that are referring to Angular Material or the CDK, the name of
  59. // the import identifiers. This allows us to identify identifiers that belong to Material and
  60. // the CDK, and we won't accidentally touch a developer's identifier.
  61. if (imports_1.isImportSpecifierNode(identifier) && module_specifiers_1.isMaterialImportDeclaration(identifier)) {
  62. this.trustedIdentifiers.add(identifier.text);
  63. return this._createFailureWithReplacement(identifier);
  64. }
  65. // In case the identifier is part of a property access expression, we need to verify that the
  66. // property access originates from a namespace that has been imported from Material or the CDK.
  67. if (ts.isPropertyAccessExpression(identifier.parent)) {
  68. const expression = identifier.parent.expression;
  69. if (ts.isIdentifier(expression) && this.trustedNamespaces.has(expression.text)) {
  70. return this._createFailureWithReplacement(identifier);
  71. }
  72. }
  73. else if (this.trustedIdentifiers.has(identifier.text)) {
  74. return this._createFailureWithReplacement(identifier);
  75. }
  76. }
  77. /** Creates a failure and replacement for the specified identifier. */
  78. _createFailureWithReplacement(identifier) {
  79. const classData = this.data.find(data => data.replace === identifier.text);
  80. const updateRecorder = this.getUpdateRecorder(identifier.getSourceFile().fileName);
  81. updateRecorder.remove(identifier.getStart(), identifier.getWidth());
  82. updateRecorder.insertRight(identifier.getStart(), classData.replaceWith);
  83. }
  84. }
  85. exports.ClassNamesRule = ClassNamesRule;
  86. //# sourceMappingURL=class-names-rule.js.map