binding.js 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.default = void 0;
  6. class Binding {
  7. constructor({
  8. identifier,
  9. scope,
  10. path,
  11. kind
  12. }) {
  13. this.identifier = void 0;
  14. this.scope = void 0;
  15. this.path = void 0;
  16. this.kind = void 0;
  17. this.constantViolations = [];
  18. this.constant = true;
  19. this.referencePaths = [];
  20. this.referenced = false;
  21. this.references = 0;
  22. this.identifier = identifier;
  23. this.scope = scope;
  24. this.path = path;
  25. this.kind = kind;
  26. if ((kind === "var" || kind === "hoisted") &&
  27. isDeclaredInLoop(path || (() => {
  28. throw new Error("Internal Babel error: unreachable ");
  29. })())) {
  30. this.reassign(path);
  31. }
  32. this.clearValue();
  33. }
  34. deoptValue() {
  35. this.clearValue();
  36. this.hasDeoptedValue = true;
  37. }
  38. setValue(value) {
  39. if (this.hasDeoptedValue) return;
  40. this.hasValue = true;
  41. this.value = value;
  42. }
  43. clearValue() {
  44. this.hasDeoptedValue = false;
  45. this.hasValue = false;
  46. this.value = null;
  47. }
  48. reassign(path) {
  49. this.constant = false;
  50. if (this.constantViolations.indexOf(path) !== -1) {
  51. return;
  52. }
  53. this.constantViolations.push(path);
  54. }
  55. reference(path) {
  56. if (this.referencePaths.indexOf(path) !== -1) {
  57. return;
  58. }
  59. this.referenced = true;
  60. this.references++;
  61. this.referencePaths.push(path);
  62. }
  63. dereference() {
  64. this.references--;
  65. this.referenced = !!this.references;
  66. }
  67. }
  68. exports.default = Binding;
  69. function isDeclaredInLoop(path) {
  70. for (let {
  71. parentPath,
  72. key
  73. } = path; parentPath; ({
  74. parentPath,
  75. key
  76. } = parentPath)) {
  77. if (parentPath.isFunctionParent()) return false;
  78. if (parentPath.isWhile() || parentPath.isForXStatement() || parentPath.isForStatement() && key === "body") {
  79. return true;
  80. }
  81. }
  82. return false;
  83. }
  84. //# sourceMappingURL=binding.js.map