<commit_before>"use strict";

const path = require("path");
const outputFile = require("output-file-sync");
const helpers = require("@babel/helpers");
const babel = require("@babel/core");
const template = require("@babel/template");
const t = require("@babel/types");

const transformRuntime = require("../");
const corejs2Definitions = require("../lib/definitions").default;

const runtimeRoot = path.resolve(__dirname, "..", "..", "babel-runtime");

writeCoreJS2(runtimeRoot);

writeHelpers(runtimeRoot, { esm: false, corejs2: false });
writeHelpers(runtimeRoot, { esm: false, corejs2: true });
writeHelpers(runtimeRoot, { esm: true, corejs2: false });
writeHelpers(runtimeRoot, { esm: true, corejs2: true });

function writeCoreJS2(pkgDirname) {
  const paths = ["is-iterable", "get-iterator"];

  Object.keys(corejs2Definitions.builtins).forEach(key => {
    const path = corejs2Definitions.builtins[key];
    paths.push(path);
  });

  Object.keys(corejs2Definitions.methods).forEach(key => {
    const props = corejs2Definitions.methods[key];
    Object.keys(props).forEach(key2 => {
      paths.push(props[key2]);
    });
  });

  paths.forEach(function(corePath) {
    outputFile(
      path.join(pkgDirname, "core-js", `${corePath}.js`),
      `module.exports = require("core-js/library/fn/${corePath}");`
    );
  });
}

function writeHelpers(pkgDirname, { esm, corejs2 }) {
  for (const helperName of helpers.list) {
    const helperFilename = path.join(
      pkgDirname,
      "helpers",
      corejs2 ? "" : "builtin",
      esm ? "es6" : "",
      `${helperName}.js`
    );

    outputFile(
      helperFilename,
      buildHelper(pkgDirname, helperFilename, helperName, { esm, corejs2 })
    );
  }
}

function buildHelper(pkgDirname, helperFilename, helperName, { esm, corejs2 }) {
  const tree = t.program([], [], esm ? "module" : "script");
  const dependencies = {};
  let bindings = null;

  if (!esm) {
    bindings = [];
    for (const dep of helpers.getDependencies(helperName)) {
      const id = (dependencies[dep] = t.identifier(t.toIdentifier(dep)));
      tree.body.push(template.statement.ast`
        var ${id} = require("${`./${dep}`}");
      `);
      bindings.push(id.name);
    }
  }

  const helper = helpers.get(
    helperName,
    dep => dependencies[dep],
    esm ? null : template.expression.ast`module.exports`,
    bindings
  );
  tree.body.push(...helper.nodes);

  return babel.transformFromAst(tree, null, {
    presets: [[require("@babel/preset-env"), { modules: false }]],
    plugins: [
      [transformRuntime, { useBuiltIns: !corejs2, useESModules: esm }],
      buildRuntimeRewritePlugin(
        path.relative(path.dirname(helperFilename), pkgDirname),
        helperName
      ),
    ],
  }).code;
}

function buildRuntimeRewritePlugin(relativePath, helperName) {
  function adjustImportPath(node, relativePath) {
    node.value =
      helpers.list.indexOf(node.value) !== -1
        ? `./${node.value}`
        : node.value.replace(/^@babel\/runtime/, relativePath);
  }

  return {
    pre(file) {
      const original = file.get("helperGenerator");
      file.set("helperGenerator", name => {
        // make sure that helpers won't insert circular references to themselves
        if (name === helperName) return false;
<commit_msg>Build an explicit runtime.<commit_after>"use strict";

const path = require("path");
const outputFile = require("output-file-sync");
const helpers = require("@babel/helpers");
const babel = require("@babel/core");
const template = require("@babel/template");
const t = require("@babel/types");

const transformRuntime = require("../");
const corejs2Definitions = require("../lib/definitions").default;

writeHelpers("@babel/runtime", { corejs2: false });

writeCoreJS2("@babel/runtime");
writeHelpers("@babel/runtime", { corejs2: true });

function writeCoreJS2(runtimeName) {
  const pkgDirname = getRuntimeRoot(runtimeName);

  const paths = ["is-iterable", "get-iterator"];

  Object.keys(corejs2Definitions.builtins).forEach(key => {
    const path = corejs2Definitions.builtins[key];
    paths.push(path);
  });

  Object.keys(corejs2Definitions.methods).forEach(key => {
    const props = corejs2Definitions.methods[key];
    Object.keys(props).forEach(key2 => {
      paths.push(props[key2]);
    });
  });

  paths.forEach(function(corePath) {
    outputFile(
      path.join(pkgDirname, "core-js", `${corePath}.js`),
      `module.exports = require("core-js/library/fn/${corePath}");`
    );
  });
}

function writeHelpers(runtimeName, { corejs2 } = {}) {
  writeHelperFiles(runtimeName, { corejs2, esm: false });
  writeHelperFiles(runtimeName, { corejs2, esm: true });
}

function writeHelperFiles(runtimeName, { esm, corejs2 }) {
  const pkgDirname = getRuntimeRoot(runtimeName);

  for (const helperName of helpers.list) {
    const helperFilename = path.join(
      pkgDirname,
      "helpers",
      corejs2 ? "" : "builtin",
      esm ? "es6" : "",
      `${helperName}.js`
    );

    outputFile(
      helperFilename,
      buildHelper(
        runtimeName,
        pkgDirname,
        helperFilename,
        helperName,
        { esm, corejs2 }
      )
    );
  }
}

function getRuntimeRoot(runtimeName) {
  return path.resolve(
    __dirname,
    "..",
    "..",
    runtimeName.replace(/^@babel\//, "babel-")
  );
}

function buildHelper(
  runtimeName,
  pkgDirname,
  helperFilename,
  helperName,
  { esm, corejs2 }
) {
  const tree = t.program([], [], esm ? "module" : "script");
  const dependencies = {};
  let bindings = null;

  if (!esm) {
    bindings = [];
    for (const dep of helpers.getDependencies(helperName)) {
      const id = (dependencies[dep] = t.identifier(t.toIdentifier(dep)));
      tree.body.push(template.statement.ast`
        var ${id} = require("${`./${dep}`}");
      `);
      bindings.push(id.name);
    }
  }

  const helper = helpers.get(
    helperName,
    dep => dependencies[dep],
    esm ? null : template.expression.ast`module.exports`,
    bindings
  );
  tree.body.push(...helper.nodes);

  return babel.transformFromAst(tree, null, {
    presets: [[require("@babel/preset-env"), { modules: false }]],
    plugins: [
      [transformRuntime, { useBuiltIns: !corejs2, useESModules: esm }],
      buildRuntimeRewritePlugin(
        runtimeName,
        path.relative(path.dirname(helperFilename), pkgDirname),
        helperName
      ),
    ],
  }).code;
}

function buildRuntimeRewritePlugin(runtimeName, relativePath, helperName) {
  function adjustImportPath(node, relativePath) {
    node.value =
      helpers.list.indexOf(node.value) !== -1
        ? `./${node.value}`
        : node.value.replace(runtimeName, relativePath);
  }

  return {
    pre(file) {
      const original = file.get("helperGenerator");
      file.set("helperGenerator", name => {
        // make sure that helpers won't insert circular references to themselves
        if (name === helperName) return false;

{'fraction_non_alphanumeric': 0.1606680103656781, 'fraction_numerical': 0.005902677800172762, 'mean_word_length': 3.42484076433121, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '13909', 'n_tokens_mistral': 2235, 'n_tokens_neox': 2186, 'n_words': 411}