index.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. 'use strict';
  2. Object.defineProperty(exports, '__esModule', { value: true });
  3. var core = require('@babel/core');
  4. var helperPluginUtils = require('@babel/helper-plugin-utils');
  5. var helperEnvironmentVisitor = require('@babel/helper-environment-visitor');
  6. function isNameOrLength(key) {
  7. if (core.types.isIdentifier(key)) {
  8. return key.name === "name" || key.name === "length";
  9. }
  10. if (core.types.isStringLiteral(key)) {
  11. return key.value === "name" || key.value === "length";
  12. }
  13. return false;
  14. }
  15. function isStaticFieldWithValue(node) {
  16. return (core.types.isClassProperty(node) || core.types.isClassPrivateProperty(node)) && node.static && !!node.value;
  17. }
  18. const hasReferenceVisitor = {
  19. ReferencedIdentifier(path, state) {
  20. if (path.node.name === state.name) {
  21. state.ref();
  22. path.stop();
  23. }
  24. },
  25. Scope(path, {
  26. name
  27. }) {
  28. if (path.scope.hasOwnBinding(name)) {
  29. path.skip();
  30. }
  31. }
  32. };
  33. function isReferenceOrThis(node, name) {
  34. return core.types.isThisExpression(node) || name && core.types.isIdentifier(node, {
  35. name
  36. });
  37. }
  38. const hasReferenceOrThisVisitor = {
  39. "ThisExpression|ReferencedIdentifier"(path, state) {
  40. if (isReferenceOrThis(path.node, state.name)) {
  41. state.ref();
  42. path.stop();
  43. }
  44. },
  45. FunctionParent(path, state) {
  46. if (path.isArrowFunctionExpression()) return;
  47. if (state.name && !path.scope.hasOwnBinding(state.name)) {
  48. path.traverse(hasReferenceVisitor, state);
  49. }
  50. path.skip();
  51. if (path.isMethod()) {
  52. helperEnvironmentVisitor.requeueComputedKeyAndDecorators(path);
  53. }
  54. }
  55. };
  56. function getPotentiallyBuggyFieldsIndexes(path) {
  57. var _path$node$id;
  58. const buggyPublicStaticFieldsIndexes = [];
  59. let classReferenced = false;
  60. const className = (_path$node$id = path.node.id) == null ? void 0 : _path$node$id.name;
  61. const hasReferenceState = {
  62. name: className,
  63. ref: () => classReferenced = true
  64. };
  65. if (className) {
  66. for (const el of path.get("body.body")) {
  67. if (el.node.computed) {
  68. el.get("key").traverse(hasReferenceVisitor, hasReferenceState);
  69. if (classReferenced) break;
  70. }
  71. }
  72. }
  73. let nextPotentiallyBuggy = false;
  74. const {
  75. body
  76. } = path.node.body;
  77. for (let i = 0; i < body.length; i++) {
  78. const node = body[i];
  79. if (!nextPotentiallyBuggy) {
  80. if (core.types.isStaticBlock(node)) {
  81. classReferenced = true;
  82. nextPotentiallyBuggy = true;
  83. } else if (isStaticFieldWithValue(node)) {
  84. if (!classReferenced) {
  85. if (isReferenceOrThis(node.value, className)) {
  86. classReferenced = true;
  87. } else {
  88. path.get(`body.body.${i}.value`).traverse(hasReferenceOrThisVisitor, hasReferenceState);
  89. }
  90. }
  91. if (classReferenced) {
  92. nextPotentiallyBuggy = !path.scope.isPure(node.value);
  93. }
  94. }
  95. }
  96. if (core.types.isClassProperty(node, {
  97. static: true
  98. }) && (nextPotentiallyBuggy || node.computed || isNameOrLength(node.key))) {
  99. buggyPublicStaticFieldsIndexes.push(i);
  100. }
  101. }
  102. return buggyPublicStaticFieldsIndexes;
  103. }
  104. function getNameOrLengthStaticFieldsIndexes(path) {
  105. const indexes = [];
  106. const {
  107. body
  108. } = path.node.body;
  109. for (let i = 0; i < body.length; i++) {
  110. const node = body[i];
  111. if (core.types.isClassProperty(node, {
  112. static: true,
  113. computed: false
  114. }) && isNameOrLength(node.key)) {
  115. indexes.push(i);
  116. }
  117. }
  118. return indexes;
  119. }
  120. function toRanges(nums) {
  121. const ranges = [];
  122. if (nums.length === 0) return ranges;
  123. let start = nums[0];
  124. let end = start + 1;
  125. for (let i = 1; i < nums.length; i++) {
  126. if (nums[i] <= nums[i - 1]) {
  127. throw new Error("Internal Babel error: nums must be in ascending order");
  128. }
  129. if (nums[i] === end) {
  130. end++;
  131. } else {
  132. ranges.push([start, end]);
  133. start = nums[i];
  134. end = start + 1;
  135. }
  136. }
  137. ranges.push([start, end]);
  138. return ranges;
  139. }
  140. function buildFieldsReplacement(fields, scope, file) {
  141. return core.types.staticBlock(fields.map(field => {
  142. const key = field.computed || !core.types.isIdentifier(field.key) ? field.key : core.types.stringLiteral(field.key.name);
  143. return core.types.expressionStatement(core.types.callExpression(file.addHelper("defineProperty"), [core.types.thisExpression(), key, field.value || scope.buildUndefinedNode()]));
  144. }));
  145. }
  146. var index = helperPluginUtils.declare(api => {
  147. api.assertVersion(7);
  148. const setPublicClassFields = api.assumption("setPublicClassFields");
  149. return {
  150. name: "bugfix-v8-static-class-fields-redefine-readonly",
  151. visitor: {
  152. Class(path) {
  153. const ranges = toRanges(setPublicClassFields ? getNameOrLengthStaticFieldsIndexes(path) : getPotentiallyBuggyFieldsIndexes(path));
  154. for (let i = ranges.length - 1; i >= 0; i--) {
  155. const [start, end] = ranges[i];
  156. const startPath = path.get("body.body")[start];
  157. startPath.replaceWith(buildFieldsReplacement(path.node.body.body.slice(start, end), path.scope, this.file));
  158. for (let j = end - 1; j > start; j--) {
  159. path.get("body.body")[j].remove();
  160. }
  161. }
  162. }
  163. }
  164. };
  165. });
  166. exports.default = index;
  167. //# sourceMappingURL=index.js.map