removal.js 1.4 KB

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