removal.js 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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 _cache = require("../cache");
  13. var _index = _interopRequireWildcard(require("./index"));
  14. function _getRequireWildcardCache() { if (typeof WeakMap !== "function") return null; var cache = new WeakMap(); _getRequireWildcardCache = function () { return cache; }; return cache; }
  15. function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
  16. function remove() {
  17. var _this$opts;
  18. this._assertUnremoved();
  19. this.resync();
  20. if (!((_this$opts = this.opts) != null && _this$opts.noScope)) {
  21. this._removeFromScope();
  22. }
  23. if (this._callRemovalHooks()) {
  24. this._markRemoved();
  25. return;
  26. }
  27. this.shareCommentsWithSiblings();
  28. this._remove();
  29. this._markRemoved();
  30. }
  31. function _removeFromScope() {
  32. const bindings = this.getBindingIdentifiers();
  33. Object.keys(bindings).forEach(name => this.scope.removeBinding(name));
  34. }
  35. function _callRemovalHooks() {
  36. for (const fn of _removalHooks.hooks) {
  37. if (fn(this, this.parentPath)) return true;
  38. }
  39. }
  40. function _remove() {
  41. if (Array.isArray(this.container)) {
  42. this.container.splice(this.key, 1);
  43. this.updateSiblingKeys(this.key, -1);
  44. } else {
  45. this._replaceWith(null);
  46. }
  47. }
  48. function _markRemoved() {
  49. this._traverseFlags |= _index.SHOULD_SKIP | _index.REMOVED;
  50. if (this.parent) _cache.path.get(this.parent).delete(this.node);
  51. this.node = null;
  52. }
  53. function _assertUnremoved() {
  54. if (this.removed) {
  55. throw this.buildCodeFrameError("NodePath has been removed so is read-only.");
  56. }
  57. }