ba5948d7d8e15e746b57036a21ba89d6c178a0592b726be5f87ad163a62c2aecabdbe8bf64d50402c192cb440b7764b160b91947cb7db67e910d2f4659ae5b 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.default = loadBlockHoistPlugin;
  6. function _traverse() {
  7. const data = require("@babel/traverse");
  8. _traverse = function () {
  9. return data;
  10. };
  11. return data;
  12. }
  13. var _plugin = require("../config/plugin.js");
  14. let LOADED_PLUGIN;
  15. const blockHoistPlugin = {
  16. name: "internal.blockHoist",
  17. visitor: {
  18. Block: {
  19. exit({
  20. node
  21. }) {
  22. node.body = performHoisting(node.body);
  23. }
  24. },
  25. SwitchCase: {
  26. exit({
  27. node
  28. }) {
  29. node.consequent = performHoisting(node.consequent);
  30. }
  31. }
  32. }
  33. };
  34. function performHoisting(body) {
  35. let max = Math.pow(2, 30) - 1;
  36. let hasChange = false;
  37. for (let i = 0; i < body.length; i++) {
  38. const n = body[i];
  39. const p = priority(n);
  40. if (p > max) {
  41. hasChange = true;
  42. break;
  43. }
  44. max = p;
  45. }
  46. if (!hasChange) return body;
  47. return stableSort(body.slice());
  48. }
  49. function loadBlockHoistPlugin() {
  50. if (!LOADED_PLUGIN) {
  51. LOADED_PLUGIN = new _plugin.default(Object.assign({}, blockHoistPlugin, {
  52. visitor: _traverse().default.explode(blockHoistPlugin.visitor)
  53. }), {});
  54. }
  55. return LOADED_PLUGIN;
  56. }
  57. function priority(bodyNode) {
  58. const priority = bodyNode == null ? void 0 : bodyNode._blockHoist;
  59. if (priority == null) return 1;
  60. if (priority === true) return 2;
  61. return priority;
  62. }
  63. function stableSort(body) {
  64. const buckets = Object.create(null);
  65. for (let i = 0; i < body.length; i++) {
  66. const n = body[i];
  67. const p = priority(n);
  68. const bucket = buckets[p] || (buckets[p] = []);
  69. bucket.push(n);
  70. }
  71. const keys = Object.keys(buckets).map(k => +k).sort((a, b) => b - a);
  72. let index = 0;
  73. for (const key of keys) {
  74. const bucket = buckets[key];
  75. for (const n of bucket) {
  76. body[index++] = n;
  77. }
  78. }
  79. return body;
  80. }
  81. 0 && 0;
  82. //# sourceMappingURL=block-hoist-plugin.js.map