removal.js 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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. var _index = require("./index");
  13. function remove() {
  14. this._assertUnremoved();
  15. this.resync();
  16. if (!this.opts || !this.opts.noScope) {
  17. this._removeFromScope();
  18. }
  19. if (this._callRemovalHooks()) {
  20. this._markRemoved();
  21. return;
  22. }
  23. this.shareCommentsWithSiblings();
  24. this._remove();
  25. this._markRemoved();
  26. }
  27. function _removeFromScope() {
  28. const bindings = this.getBindingIdentifiers();
  29. Object.keys(bindings).forEach(name => this.scope.removeBinding(name));
  30. }
  31. function _callRemovalHooks() {
  32. for (const fn of _removalHooks.hooks) {
  33. if (fn(this, this.parentPath)) return true;
  34. }
  35. }
  36. function _remove() {
  37. if (Array.isArray(this.container)) {
  38. this.container.splice(this.key, 1);
  39. this.updateSiblingKeys(this.key, -1);
  40. } else {
  41. this._replaceWith(null);
  42. }
  43. }
  44. function _markRemoved() {
  45. this._traverseFlags |= _index.SHOULD_SKIP | _index.REMOVED;
  46. this.node = null;
  47. }
  48. function _assertUnremoved() {
  49. if (this.removed) {
  50. throw this.buildCodeFrameError("NodePath has been removed so is read-only.");
  51. }
  52. }