removal.js 1.9 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. var _modification = require("./modification.js");
  17. var _context = require("./context.js");
  18. function remove() {
  19. var _this$opts;
  20. _assertUnremoved.call(this);
  21. _context.resync.call(this);
  22. if (_callRemovalHooks.call(this)) {
  23. _markRemoved.call(this);
  24. return;
  25. }
  26. if (!((_this$opts = this.opts) != null && _this$opts.noScope)) {
  27. _removeFromScope.call(this);
  28. }
  29. this.shareCommentsWithSiblings();
  30. _remove.call(this);
  31. _markRemoved.call(this);
  32. }
  33. function _removeFromScope() {
  34. const bindings = t.getBindingIdentifiers(this.node, false, false, true);
  35. Object.keys(bindings).forEach(name => this.scope.removeBinding(name));
  36. }
  37. function _callRemovalHooks() {
  38. if (this.parentPath) {
  39. for (const fn of _removalHooks.hooks) {
  40. if (fn(this, this.parentPath)) return true;
  41. }
  42. }
  43. }
  44. function _remove() {
  45. if (Array.isArray(this.container)) {
  46. this.container.splice(this.key, 1);
  47. _modification.updateSiblingKeys.call(this, this.key, -1);
  48. } else {
  49. _replacement._replaceWith.call(this, null);
  50. }
  51. }
  52. function _markRemoved() {
  53. this._traverseFlags |= _index.SHOULD_SKIP | _index.REMOVED;
  54. if (this.parent) {
  55. var _getCachedPaths;
  56. (_getCachedPaths = (0, _cache.getCachedPaths)(this)) == null || _getCachedPaths.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