class-names.js 4.7 KB

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