class-names.js 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. "use strict";
  2. /**
  3. * Use of this source code is governed by an MIT-style license that can be
  4. * found in the LICENSE file at https://github.com/NG-ZORRO/ng-zorro-antd/blob/master/LICENSE
  5. */
  6. Object.defineProperty(exports, "__esModule", { value: true });
  7. exports.ClassNamesMigration = void 0;
  8. const schematics_1 = require("@angular/cdk/schematics");
  9. const ts = require("typescript");
  10. const module_specifiers_1 = require("../../utils/ng-update/module-specifiers");
  11. const upgrade_data_1 = require("../upgrade-data");
  12. class ClassNamesMigration extends schematics_1.Migration {
  13. constructor() {
  14. super(...arguments);
  15. /** Change data that upgrades to the specified target version. */
  16. this.data = (0, upgrade_data_1.getVersionUpgradeData)(this, 'classNames');
  17. /**
  18. * List of identifier names that have been imported from `@ng-zorro-antd`
  19. * in the current source file and therefore can be considered trusted.
  20. */
  21. this.trustedIdentifiers = new Set();
  22. /** List of namespaces that have been imported from `@ng-zorro-antd`. */
  23. this.trustedNamespaces = new Set();
  24. // Only enable the migration rule if there is upgrade data.
  25. this.enabled = this.data.length !== 0;
  26. }
  27. visitNode(node) {
  28. if (ts.isIdentifier(node)) {
  29. this.visitIdentifier(node);
  30. }
  31. }
  32. /** Method that is called for every identifier inside of the specified project. */
  33. visitIdentifier(identifier) {
  34. if (!this.data.some(data => data.replace === identifier.text)) {
  35. return;
  36. }
  37. if ((0, schematics_1.isNamespaceImportNode)(identifier) && (0, module_specifiers_1.isNgZorroImportDeclaration)(identifier)) {
  38. this.trustedNamespaces.add(identifier.text);
  39. return this._createFailureWithReplacement(identifier);
  40. }
  41. if ((0, schematics_1.isExportSpecifierNode)(identifier) && (0, module_specifiers_1.isNgZorroExportDeclaration)(identifier)) {
  42. return this._createFailureWithReplacement(identifier);
  43. }
  44. if ((0, schematics_1.isImportSpecifierNode)(identifier) && (0, module_specifiers_1.isNgZorroImportDeclaration)(identifier)) {
  45. this.trustedIdentifiers.add(identifier.text);
  46. return this._createFailureWithReplacement(identifier);
  47. }
  48. if (ts.isPropertyAccessExpression(identifier.parent)) {
  49. const expression = identifier.parent.expression;
  50. if (ts.isIdentifier(expression) && this.trustedNamespaces.has(expression.text)) {
  51. return this._createFailureWithReplacement(identifier);
  52. }
  53. }
  54. else if (this.trustedIdentifiers.has(identifier.text)) {
  55. return this._createFailureWithReplacement(identifier);
  56. }
  57. }
  58. /** Creates a failure and replacement for the specified identifier. */
  59. _createFailureWithReplacement(identifier) {
  60. const classData = this.data.find(data => data.replace === identifier.text);
  61. const filePath = this.fileSystem.resolve(identifier.getSourceFile().fileName);
  62. this.fileSystem.edit(filePath)
  63. .remove(identifier.getStart(), identifier.getWidth())
  64. .insertRight(identifier.getStart(), classData.replaceWith);
  65. }
  66. }
  67. exports.ClassNamesMigration = ClassNamesMigration;
  68. //# sourceMappingURL=class-names.js.map