binding.js 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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") && isDeclaredInLoop(path || (() => {
  27. throw new Error("Internal Babel error: unreachable ");
  28. })())) {
  29. this.reassign(path);
  30. }
  31. this.clearValue();
  32. }
  33. deoptValue() {
  34. this.clearValue();
  35. this.hasDeoptedValue = true;
  36. }
  37. setValue(value) {
  38. if (this.hasDeoptedValue) return;
  39. this.hasValue = true;
  40. this.value = value;
  41. }
  42. clearValue() {
  43. this.hasDeoptedValue = false;
  44. this.hasValue = false;
  45. this.value = null;
  46. }
  47. reassign(path) {
  48. this.constant = false;
  49. if (this.constantViolations.indexOf(path) !== -1) {
  50. return;
  51. }
  52. this.constantViolations.push(path);
  53. }
  54. reference(path) {
  55. if (this.referencePaths.indexOf(path) !== -1) {
  56. return;
  57. }
  58. this.referenced = true;
  59. this.references++;
  60. this.referencePaths.push(path);
  61. }
  62. dereference() {
  63. this.references--;
  64. this.referenced = !!this.references;
  65. }
  66. }
  67. exports.default = Binding;
  68. function isDeclaredInLoop(path) {
  69. for (let {
  70. parentPath,
  71. key
  72. } = path; parentPath; ({
  73. parentPath,
  74. key
  75. } = parentPath)) {
  76. if (parentPath.isFunctionParent()) return false;
  77. if (parentPath.isWhile() || parentPath.isForXStatement() || parentPath.isForStatement() && key === "body") {
  78. return true;
  79. }
  80. }
  81. return false;
  82. }
  83. //# sourceMappingURL=binding.js.map