import-specifiers.js 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  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.ImportSpecifiersMigration = 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 ImportSpecifiersMigration 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, 'importSpecifiers');
  17. // Only enable the migration rule if there is upgrade data.
  18. this.enabled = this.data.length !== 0;
  19. }
  20. visitNode(node) {
  21. if (ts.isImportDeclaration(node)) {
  22. this.visitImportDeclaration(node);
  23. }
  24. }
  25. visitImportDeclaration(node) {
  26. if ((0, module_specifiers_1.isNgZorroImportDeclaration)(node)) {
  27. return this._createFailureWithReplacement(node);
  28. }
  29. }
  30. /** Creates a failure and replacement for the specified identifier. */
  31. _createFailureWithReplacement(identifier) {
  32. const upgradeData = this.data.find(({ replace }) => identifier.moduleSpecifier.getText().indexOf(replace) !== -1);
  33. if (upgradeData) {
  34. const filePath = this.fileSystem.resolve(identifier.getSourceFile().fileName);
  35. this.fileSystem.edit(filePath)
  36. .remove(identifier.moduleSpecifier.getStart() + 1, identifier.moduleSpecifier.getWidth() - 2) // quotes
  37. .insertRight(identifier.moduleSpecifier.getStart() + 1, upgradeData.replaceWith);
  38. }
  39. }
  40. }
  41. exports.ImportSpecifiersMigration = ImportSpecifiersMigration;
  42. //# sourceMappingURL=import-specifiers.js.map