binding.js 1.8 KB

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