index.js 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.get = get;
  6. exports.minVersion = minVersion;
  7. exports.getDependencies = getDependencies;
  8. exports.ensure = ensure;
  9. exports.default = exports.list = void 0;
  10. var _traverse = require("@babel/traverse");
  11. var _t = require("@babel/types");
  12. var _helpers = require("./helpers");
  13. const {
  14. assignmentExpression,
  15. cloneNode,
  16. expressionStatement,
  17. file: t_file,
  18. identifier,
  19. variableDeclaration,
  20. variableDeclarator
  21. } = _t;
  22. function makePath(path) {
  23. const parts = [];
  24. for (; path.parentPath; path = path.parentPath) {
  25. parts.push(path.key);
  26. if (path.inList) parts.push(path.listKey);
  27. }
  28. return parts.reverse().join(".");
  29. }
  30. let fileClass = undefined;
  31. function getHelperMetadata(file) {
  32. const globals = new Set();
  33. const localBindingNames = new Set();
  34. const dependencies = new Map();
  35. let exportName;
  36. let exportPath;
  37. const exportBindingAssignments = [];
  38. const importPaths = [];
  39. const importBindingsReferences = [];
  40. const dependencyVisitor = {
  41. ImportDeclaration(child) {
  42. const name = child.node.source.value;
  43. if (!_helpers.default[name]) {
  44. throw child.buildCodeFrameError(`Unknown helper ${name}`);
  45. }
  46. if (child.get("specifiers").length !== 1 || !child.get("specifiers.0").isImportDefaultSpecifier()) {
  47. throw child.buildCodeFrameError("Helpers can only import a default value");
  48. }
  49. const bindingIdentifier = child.node.specifiers[0].local;
  50. dependencies.set(bindingIdentifier, name);
  51. importPaths.push(makePath(child));
  52. },
  53. ExportDefaultDeclaration(child) {
  54. const decl = child.get("declaration");
  55. if (decl.isFunctionDeclaration()) {
  56. if (!decl.node.id) {
  57. throw decl.buildCodeFrameError("Helpers should give names to their exported func declaration");
  58. }
  59. exportName = decl.node.id.name;
  60. }
  61. exportPath = makePath(child);
  62. },
  63. ExportAllDeclaration(child) {
  64. throw child.buildCodeFrameError("Helpers can only export default");
  65. },
  66. ExportNamedDeclaration(child) {
  67. throw child.buildCodeFrameError("Helpers can only export default");
  68. },
  69. Statement(child) {
  70. if (child.isModuleDeclaration()) return;
  71. child.skip();
  72. }
  73. };
  74. const referenceVisitor = {
  75. Program(path) {
  76. const bindings = path.scope.getAllBindings();
  77. Object.keys(bindings).forEach(name => {
  78. if (name === exportName) return;
  79. if (dependencies.has(bindings[name].identifier)) return;
  80. localBindingNames.add(name);
  81. });
  82. },
  83. ReferencedIdentifier(child) {
  84. const name = child.node.name;
  85. const binding = child.scope.getBinding(name);
  86. if (!binding) {
  87. globals.add(name);
  88. } else if (dependencies.has(binding.identifier)) {
  89. importBindingsReferences.push(makePath(child));
  90. }
  91. },
  92. AssignmentExpression(child) {
  93. const left = child.get("left");
  94. if (!(exportName in left.getBindingIdentifiers())) return;
  95. if (!left.isIdentifier()) {
  96. throw left.buildCodeFrameError("Only simple assignments to exports are allowed in helpers");
  97. }
  98. const binding = child.scope.getBinding(exportName);
  99. if (binding != null && binding.scope.path.isProgram()) {
  100. exportBindingAssignments.push(makePath(child));
  101. }
  102. }
  103. };
  104. (0, _traverse.default)(file.ast, dependencyVisitor, file.scope);
  105. (0, _traverse.default)(file.ast, referenceVisitor, file.scope);
  106. if (!exportPath) throw new Error("Helpers must default-export something.");
  107. exportBindingAssignments.reverse();
  108. return {
  109. globals: Array.from(globals),
  110. localBindingNames: Array.from(localBindingNames),
  111. dependencies,
  112. exportBindingAssignments,
  113. exportPath,
  114. exportName,
  115. importBindingsReferences,
  116. importPaths
  117. };
  118. }
  119. function permuteHelperAST(file, metadata, id, localBindings, getDependency) {
  120. if (localBindings && !id) {
  121. throw new Error("Unexpected local bindings for module-based helpers.");
  122. }
  123. if (!id) return;
  124. const {
  125. localBindingNames,
  126. dependencies,
  127. exportBindingAssignments,
  128. exportPath,
  129. exportName,
  130. importBindingsReferences,
  131. importPaths
  132. } = metadata;
  133. const dependenciesRefs = {};
  134. dependencies.forEach((name, id) => {
  135. dependenciesRefs[id.name] = typeof getDependency === "function" && getDependency(name) || id;
  136. });
  137. const toRename = {};
  138. const bindings = new Set(localBindings || []);
  139. localBindingNames.forEach(name => {
  140. let newName = name;
  141. while (bindings.has(newName)) newName = "_" + newName;
  142. if (newName !== name) toRename[name] = newName;
  143. });
  144. if (id.type === "Identifier" && exportName !== id.name) {
  145. toRename[exportName] = id.name;
  146. }
  147. const visitor = {
  148. Program(path) {
  149. const exp = path.get(exportPath);
  150. const imps = importPaths.map(p => path.get(p));
  151. const impsBindingRefs = importBindingsReferences.map(p => path.get(p));
  152. const decl = exp.get("declaration");
  153. if (id.type === "Identifier") {
  154. if (decl.isFunctionDeclaration()) {
  155. exp.replaceWith(decl);
  156. } else {
  157. exp.replaceWith(variableDeclaration("var", [variableDeclarator(id, decl.node)]));
  158. }
  159. } else if (id.type === "MemberExpression") {
  160. if (decl.isFunctionDeclaration()) {
  161. exportBindingAssignments.forEach(assignPath => {
  162. const assign = path.get(assignPath);
  163. assign.replaceWith(assignmentExpression("=", id, assign.node));
  164. });
  165. exp.replaceWith(decl);
  166. path.pushContainer("body", expressionStatement(assignmentExpression("=", id, identifier(exportName))));
  167. } else {
  168. exp.replaceWith(expressionStatement(assignmentExpression("=", id, decl.node)));
  169. }
  170. } else {
  171. throw new Error("Unexpected helper format.");
  172. }
  173. Object.keys(toRename).forEach(name => {
  174. path.scope.rename(name, toRename[name]);
  175. });
  176. for (const path of imps) path.remove();
  177. for (const path of impsBindingRefs) {
  178. const node = cloneNode(dependenciesRefs[path.node.name]);
  179. path.replaceWith(node);
  180. }
  181. path.stop();
  182. }
  183. };
  184. (0, _traverse.default)(file.ast, visitor, file.scope);
  185. }
  186. const helperData = Object.create(null);
  187. function loadHelper(name) {
  188. if (!helperData[name]) {
  189. const helper = _helpers.default[name];
  190. if (!helper) {
  191. throw Object.assign(new ReferenceError(`Unknown helper ${name}`), {
  192. code: "BABEL_HELPER_UNKNOWN",
  193. helper: name
  194. });
  195. }
  196. const fn = () => {
  197. const file = {
  198. ast: t_file(helper.ast())
  199. };
  200. if (fileClass) {
  201. return new fileClass({
  202. filename: `babel-helper://${name}`
  203. }, file);
  204. }
  205. return file;
  206. };
  207. const metadata = getHelperMetadata(fn());
  208. helperData[name] = {
  209. build(getDependency, id, localBindings) {
  210. const file = fn();
  211. permuteHelperAST(file, metadata, id, localBindings, getDependency);
  212. return {
  213. nodes: file.ast.program.body,
  214. globals: metadata.globals
  215. };
  216. },
  217. minVersion() {
  218. return helper.minVersion;
  219. },
  220. dependencies: metadata.dependencies
  221. };
  222. }
  223. return helperData[name];
  224. }
  225. function get(name, getDependency, id, localBindings) {
  226. return loadHelper(name).build(getDependency, id, localBindings);
  227. }
  228. function minVersion(name) {
  229. return loadHelper(name).minVersion();
  230. }
  231. function getDependencies(name) {
  232. return Array.from(loadHelper(name).dependencies.values());
  233. }
  234. function ensure(name, newFileClass) {
  235. if (!fileClass) {
  236. fileClass = newFileClass;
  237. }
  238. loadHelper(name);
  239. }
  240. const list = Object.keys(_helpers.default).map(name => name.replace(/^_/, "")).filter(name => name !== "__esModule");
  241. exports.list = list;
  242. var _default = get;
  243. exports.default = _default;