removal.js 1.8 KB

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