Sha256: f677750d3e4b6f6b2cfc99858f7f30eb0bd04c4ef823eb1be92b6fc562d4a742

Contents?: true

Size: 1.76 KB

Versions: 7

Compression:

Stored size: 1.76 KB

Contents

"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = loadBlockHoistPlugin;

function _traverse() {
  const data = require("@babel/traverse");

  _traverse = function () {
    return data;
  };

  return data;
}

var _plugin = require("../config/plugin");

let LOADED_PLUGIN;

function loadBlockHoistPlugin() {
  if (!LOADED_PLUGIN) {
    LOADED_PLUGIN = new _plugin.default(Object.assign({}, blockHoistPlugin, {
      visitor: _traverse().default.explode(blockHoistPlugin.visitor)
    }), {});
  }

  return LOADED_PLUGIN;
}

function priority(bodyNode) {
  const priority = bodyNode == null ? void 0 : bodyNode._blockHoist;
  if (priority == null) return 1;
  if (priority === true) return 2;
  return priority;
}

function stableSort(body) {
  const buckets = Object.create(null);

  for (let i = 0; i < body.length; i++) {
    const n = body[i];
    const p = priority(n);
    const bucket = buckets[p] || (buckets[p] = []);
    bucket.push(n);
  }

  const keys = Object.keys(buckets).map(k => +k).sort((a, b) => b - a);
  let index = 0;

  for (const key of keys) {
    const bucket = buckets[key];

    for (const n of bucket) {
      body[index++] = n;
    }
  }

  return body;
}

const blockHoistPlugin = {
  name: "internal.blockHoist",
  visitor: {
    Block: {
      exit({
        node
      }) {
        const {
          body
        } = node;
        let max = Math.pow(2, 30) - 1;
        let hasChange = false;

        for (let i = 0; i < body.length; i++) {
          const n = body[i];
          const p = priority(n);

          if (p > max) {
            hasChange = true;
            break;
          }

          max = p;
        }

        if (!hasChange) return;
        node.body = stableSort(body.slice());
      }

    }
  }
};

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
disco_app-0.16.1 test/dummy/node_modules/@babel/core/lib/transformation/block-hoist-plugin.js
disco_app-0.15.2 test/dummy/node_modules/@babel/core/lib/transformation/block-hoist-plugin.js
disco_app-0.18.4 test/dummy/node_modules/@babel/core/lib/transformation/block-hoist-plugin.js
disco_app-0.18.1 test/dummy/node_modules/@babel/core/lib/transformation/block-hoist-plugin.js
disco_app-0.12.7.pre.puma.pre.3 test/dummy/node_modules/@babel/core/lib/transformation/block-hoist-plugin.js
disco_app-0.14.0 test/dummy/node_modules/@babel/core/lib/transformation/block-hoist-plugin.js
disco_app-0.13.6.pre.puma.pre.3 test/dummy/node_modules/@babel/core/lib/transformation/block-hoist-plugin.js