binding.js 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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") && isInitInLoop(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 isInitInLoop(path) {
  67. const isFunctionDeclarationOrHasInit = !path.isVariableDeclarator() || path.node.init;
  68. for (let {
  69. parentPath,
  70. key
  71. } = path; parentPath; {
  72. parentPath,
  73. key
  74. } = parentPath) {
  75. if (parentPath.isFunctionParent()) return false;
  76. if (key === "left" && parentPath.isForXStatement() || isFunctionDeclarationOrHasInit && key === "body" && parentPath.isLoop()) {
  77. return true;
  78. }
  79. }
  80. return false;
  81. }
  82. //# sourceMappingURL=binding.js.map