removal.js 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports._assertUnremoved = _assertUnremoved;
  6. exports._callRemovalHooks = _callRemovalHooks;
  7. exports._markRemoved = _markRemoved;
  8. exports._remove = _remove;
  9. exports._removeFromScope = _removeFromScope;
  10. exports.remove = remove;
  11. var _removalHooks = require("./lib/removal-hooks.js");
  12. var _cache = require("../cache.js");
  13. var _index = require("./index.js");
  14. var _t = require("@babel/types");
  15. const {
  16. getBindingIdentifiers
  17. } = _t;
  18. function remove() {
  19. var _this$opts;
  20. this._assertUnremoved();
  21. this.resync();
  22. if (!((_this$opts = this.opts) != null && _this$opts.noScope)) {
  23. this._removeFromScope();
  24. }
  25. if (this._callRemovalHooks()) {
  26. this._markRemoved();
  27. return;
  28. }
  29. this.shareCommentsWithSiblings();
  30. this._remove();
  31. this._markRemoved();
  32. }
  33. function _removeFromScope() {
  34. const bindings = getBindingIdentifiers(this.node, false, false, true);
  35. Object.keys(bindings).forEach(name => this.scope.removeBinding(name));
  36. }
  37. function _callRemovalHooks() {
  38. if (this.parentPath) {
  39. for (const fn of _removalHooks.hooks) {
  40. if (fn(this, this.parentPath)) return true;
  41. }
  42. }
  43. }
  44. function _remove() {
  45. if (Array.isArray(this.container)) {
  46. this.container.splice(this.key, 1);
  47. this.updateSiblingKeys(this.key, -1);
  48. } else {
  49. this._replaceWith(null);
  50. }
  51. }
  52. function _markRemoved() {
  53. this._traverseFlags |= _index.SHOULD_SKIP | _index.REMOVED;
  54. if (this.parent) {
  55. (0, _cache.getCachedPaths)(this.hub, this.parent).delete(this.node);
  56. }
  57. this.node = null;
  58. }
  59. function _assertUnremoved() {
  60. if (this.removed) {
  61. throw this.buildCodeFrameError("NodePath has been removed so is read-only.");
  62. }
  63. }
  64. //# sourceMappingURL=removal.js.map