removal.js 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.remove = remove;
  6. exports._removeFromScope = _removeFromScope;
  7. exports._callRemovalHooks = _callRemovalHooks;
  8. exports._remove = _remove;
  9. exports._markRemoved = _markRemoved;
  10. exports._assertUnremoved = _assertUnremoved;
  11. var _removalHooks = require("./lib/removal-hooks");
  12. function remove() {
  13. this._assertUnremoved();
  14. this.resync();
  15. this._removeFromScope();
  16. if (this._callRemovalHooks()) {
  17. this._markRemoved();
  18. return;
  19. }
  20. this.shareCommentsWithSiblings();
  21. this._remove();
  22. this._markRemoved();
  23. }
  24. function _removeFromScope() {
  25. const bindings = this.getBindingIdentifiers();
  26. Object.keys(bindings).forEach(name => this.scope.removeBinding(name));
  27. }
  28. function _callRemovalHooks() {
  29. for (const fn of _removalHooks.hooks) {
  30. if (fn(this, this.parentPath)) return true;
  31. }
  32. }
  33. function _remove() {
  34. if (Array.isArray(this.container)) {
  35. this.container.splice(this.key, 1);
  36. this.updateSiblingKeys(this.key, -1);
  37. } else {
  38. this._replaceWith(null);
  39. }
  40. }
  41. function _markRemoved() {
  42. this.shouldSkip = true;
  43. this.removed = true;
  44. this.node = null;
  45. }
  46. function _assertUnremoved() {
  47. if (this.removed) {
  48. throw this.buildCodeFrameError("NodePath has been removed so is read-only.");
  49. }
  50. }