removal.js 1.9 KB

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