| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300 | "use strict";Object.defineProperty(exports, "__esModule", {  value: true});exports.default = void 0;exports.ensure = ensure;exports.get = get;exports.getDependencies = getDependencies;exports.list = void 0;exports.minVersion = minVersion;var _traverse = require("@babel/traverse");var _t = require("@babel/types");var _helpers = require("./helpers");const {  assignmentExpression,  cloneNode,  expressionStatement,  file: t_file,  identifier,  variableDeclaration,  variableDeclarator} = _t;function makePath(path) {  const parts = [];  for (; path.parentPath; path = path.parentPath) {    parts.push(path.key);    if (path.inList) parts.push(path.listKey);  }  return parts.reverse().join(".");}let fileClass = undefined;function getHelperMetadata(file) {  const globals = new Set();  const localBindingNames = new Set();  const dependencies = new Map();  let exportName;  let exportPath;  const exportBindingAssignments = [];  const importPaths = [];  const importBindingsReferences = [];  const dependencyVisitor = {    ImportDeclaration(child) {      const name = child.node.source.value;      if (!_helpers.default[name]) {        throw child.buildCodeFrameError(`Unknown helper ${name}`);      }      if (child.get("specifiers").length !== 1 || !child.get("specifiers.0").isImportDefaultSpecifier()) {        throw child.buildCodeFrameError("Helpers can only import a default value");      }      const bindingIdentifier = child.node.specifiers[0].local;      dependencies.set(bindingIdentifier, name);      importPaths.push(makePath(child));    },    ExportDefaultDeclaration(child) {      const decl = child.get("declaration");      if (decl.isFunctionDeclaration()) {        if (!decl.node.id) {          throw decl.buildCodeFrameError("Helpers should give names to their exported func declaration");        }        exportName = decl.node.id.name;      }      exportPath = makePath(child);    },    ExportAllDeclaration(child) {      throw child.buildCodeFrameError("Helpers can only export default");    },    ExportNamedDeclaration(child) {      throw child.buildCodeFrameError("Helpers can only export default");    },    Statement(child) {      if (child.isModuleDeclaration()) return;      child.skip();    }  };  const referenceVisitor = {    Program(path) {      const bindings = path.scope.getAllBindings();      Object.keys(bindings).forEach(name => {        if (name === exportName) return;        if (dependencies.has(bindings[name].identifier)) return;        localBindingNames.add(name);      });    },    ReferencedIdentifier(child) {      const name = child.node.name;      const binding = child.scope.getBinding(name);      if (!binding) {        globals.add(name);      } else if (dependencies.has(binding.identifier)) {        importBindingsReferences.push(makePath(child));      }    },    AssignmentExpression(child) {      const left = child.get("left");      if (!(exportName in left.getBindingIdentifiers())) return;      if (!left.isIdentifier()) {        throw left.buildCodeFrameError("Only simple assignments to exports are allowed in helpers");      }      const binding = child.scope.getBinding(exportName);      if (binding != null && binding.scope.path.isProgram()) {        exportBindingAssignments.push(makePath(child));      }    }  };  (0, _traverse.default)(file.ast, dependencyVisitor, file.scope);  (0, _traverse.default)(file.ast, referenceVisitor, file.scope);  if (!exportPath) throw new Error("Helpers must default-export something.");  exportBindingAssignments.reverse();  return {    globals: Array.from(globals),    localBindingNames: Array.from(localBindingNames),    dependencies,    exportBindingAssignments,    exportPath,    exportName,    importBindingsReferences,    importPaths  };}function permuteHelperAST(file, metadata, id, localBindings, getDependency) {  if (localBindings && !id) {    throw new Error("Unexpected local bindings for module-based helpers.");  }  if (!id) return;  const {    localBindingNames,    dependencies,    exportBindingAssignments,    exportPath,    exportName,    importBindingsReferences,    importPaths  } = metadata;  const dependenciesRefs = {};  dependencies.forEach((name, id) => {    dependenciesRefs[id.name] = typeof getDependency === "function" && getDependency(name) || id;  });  const toRename = {};  const bindings = new Set(localBindings || []);  localBindingNames.forEach(name => {    let newName = name;    while (bindings.has(newName)) newName = "_" + newName;    if (newName !== name) toRename[name] = newName;  });  if (id.type === "Identifier" && exportName !== id.name) {    toRename[exportName] = id.name;  }  const visitor = {    Program(path) {      const exp = path.get(exportPath);      const imps = importPaths.map(p => path.get(p));      const impsBindingRefs = importBindingsReferences.map(p => path.get(p));      const decl = exp.get("declaration");      if (id.type === "Identifier") {        if (decl.isFunctionDeclaration()) {          exp.replaceWith(decl);        } else {          exp.replaceWith(variableDeclaration("var", [variableDeclarator(id, decl.node)]));        }      } else if (id.type === "MemberExpression") {        if (decl.isFunctionDeclaration()) {          exportBindingAssignments.forEach(assignPath => {            const assign = path.get(assignPath);            assign.replaceWith(assignmentExpression("=", id, assign.node));          });          exp.replaceWith(decl);          path.pushContainer("body", expressionStatement(assignmentExpression("=", id, identifier(exportName))));        } else {          exp.replaceWith(expressionStatement(assignmentExpression("=", id, decl.node)));        }      } else {        throw new Error("Unexpected helper format.");      }      Object.keys(toRename).forEach(name => {        path.scope.rename(name, toRename[name]);      });      for (const path of imps) path.remove();      for (const path of impsBindingRefs) {        const node = cloneNode(dependenciesRefs[path.node.name]);        path.replaceWith(node);      }      path.stop();    }  };  (0, _traverse.default)(file.ast, visitor, file.scope);}const helperData = Object.create(null);function loadHelper(name) {  if (!helperData[name]) {    const helper = _helpers.default[name];    if (!helper) {      throw Object.assign(new ReferenceError(`Unknown helper ${name}`), {        code: "BABEL_HELPER_UNKNOWN",        helper: name      });    }    const fn = () => {      const file = {        ast: t_file(helper.ast())      };      if (fileClass) {        return new fileClass({          filename: `babel-helper://${name}`        }, file);      }      return file;    };    const metadata = getHelperMetadata(fn());    helperData[name] = {      build(getDependency, id, localBindings) {        const file = fn();        permuteHelperAST(file, metadata, id, localBindings, getDependency);        return {          nodes: file.ast.program.body,          globals: metadata.globals        };      },      minVersion() {        return helper.minVersion;      },      dependencies: metadata.dependencies    };  }  return helperData[name];}function get(name, getDependency, id, localBindings) {  return loadHelper(name).build(getDependency, id, localBindings);}function minVersion(name) {  return loadHelper(name).minVersion();}function getDependencies(name) {  return Array.from(loadHelper(name).dependencies.values());}function ensure(name, newFileClass) {  if (!fileClass) {    fileClass = newFileClass;  }  loadHelper(name);}const list = Object.keys(_helpers.default).map(name => name.replace(/^_/, ""));exports.list = list;var _default = get;exports.default = _default;
 |