8ad5691e96b8da7f69a525ca729ec7efe65410c2b8f4236be1b4f2a023897a9368246dfc4bd013b64461351cad3ad8257699f822b69455c4495a211ab76202 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.default = void 0;
  6. function helpers() {
  7. const data = require("@babel/helpers");
  8. helpers = function () {
  9. return data;
  10. };
  11. return data;
  12. }
  13. function _traverse() {
  14. const data = require("@babel/traverse");
  15. _traverse = function () {
  16. return data;
  17. };
  18. return data;
  19. }
  20. function _codeFrame() {
  21. const data = require("@babel/code-frame");
  22. _codeFrame = function () {
  23. return data;
  24. };
  25. return data;
  26. }
  27. function _t() {
  28. const data = require("@babel/types");
  29. _t = function () {
  30. return data;
  31. };
  32. return data;
  33. }
  34. function _semver() {
  35. const data = require("semver");
  36. _semver = function () {
  37. return data;
  38. };
  39. return data;
  40. }
  41. var _babel7Helpers = require("./babel-7-helpers.cjs");
  42. const {
  43. cloneNode,
  44. interpreterDirective
  45. } = _t();
  46. const errorVisitor = {
  47. enter(path, state) {
  48. const loc = path.node.loc;
  49. if (loc) {
  50. state.loc = loc;
  51. path.stop();
  52. }
  53. }
  54. };
  55. class File {
  56. constructor(options, {
  57. code,
  58. ast,
  59. inputMap
  60. }) {
  61. this._map = new Map();
  62. this.opts = void 0;
  63. this.declarations = {};
  64. this.path = void 0;
  65. this.ast = void 0;
  66. this.scope = void 0;
  67. this.metadata = {};
  68. this.code = "";
  69. this.inputMap = void 0;
  70. this.hub = {
  71. file: this,
  72. getCode: () => this.code,
  73. getScope: () => this.scope,
  74. addHelper: this.addHelper.bind(this),
  75. buildError: this.buildCodeFrameError.bind(this)
  76. };
  77. this.opts = options;
  78. this.code = code;
  79. this.ast = ast;
  80. this.inputMap = inputMap;
  81. this.path = _traverse().NodePath.get({
  82. hub: this.hub,
  83. parentPath: null,
  84. parent: this.ast,
  85. container: this.ast,
  86. key: "program"
  87. }).setContext();
  88. this.scope = this.path.scope;
  89. }
  90. get shebang() {
  91. const {
  92. interpreter
  93. } = this.path.node;
  94. return interpreter ? interpreter.value : "";
  95. }
  96. set shebang(value) {
  97. if (value) {
  98. this.path.get("interpreter").replaceWith(interpreterDirective(value));
  99. } else {
  100. this.path.get("interpreter").remove();
  101. }
  102. }
  103. set(key, val) {
  104. {
  105. if (key === "helpersNamespace") {
  106. throw new Error("Babel 7.0.0-beta.56 has dropped support for the 'helpersNamespace' utility." + "If you are using @babel/plugin-external-helpers you will need to use a newer " + "version than the one you currently have installed. " + "If you have your own implementation, you'll want to explore using 'helperGenerator' " + "alongside 'file.availableHelper()'.");
  107. }
  108. }
  109. this._map.set(key, val);
  110. }
  111. get(key) {
  112. return this._map.get(key);
  113. }
  114. has(key) {
  115. return this._map.has(key);
  116. }
  117. availableHelper(name, versionRange) {
  118. let minVersion;
  119. try {
  120. minVersion = helpers().minVersion(name);
  121. } catch (err) {
  122. if (err.code !== "BABEL_HELPER_UNKNOWN") throw err;
  123. return false;
  124. }
  125. if (typeof versionRange !== "string") return true;
  126. if (_semver().valid(versionRange)) versionRange = `^${versionRange}`;
  127. {
  128. return !_semver().intersects(`<${minVersion}`, versionRange) && !_semver().intersects(`>=8.0.0`, versionRange);
  129. }
  130. }
  131. addHelper(name) {
  132. const declar = this.declarations[name];
  133. if (declar) return cloneNode(declar);
  134. const generator = this.get("helperGenerator");
  135. if (generator) {
  136. const res = generator(name);
  137. if (res) return res;
  138. }
  139. helpers().minVersion(name);
  140. const uid = this.declarations[name] = this.scope.generateUidIdentifier(name);
  141. const dependencies = {};
  142. for (const dep of helpers().getDependencies(name)) {
  143. dependencies[dep] = this.addHelper(dep);
  144. }
  145. const {
  146. nodes,
  147. globals
  148. } = helpers().get(name, dep => dependencies[dep], uid.name, Object.keys(this.scope.getAllBindings()));
  149. globals.forEach(name => {
  150. if (this.path.scope.hasBinding(name, true)) {
  151. this.path.scope.rename(name);
  152. }
  153. });
  154. nodes.forEach(node => {
  155. node._compact = true;
  156. });
  157. const added = this.path.unshiftContainer("body", nodes);
  158. for (const path of added) {
  159. if (path.isVariableDeclaration()) this.scope.registerDeclaration(path);
  160. }
  161. return uid;
  162. }
  163. buildCodeFrameError(node, msg, _Error = SyntaxError) {
  164. let loc = node == null ? void 0 : node.loc;
  165. if (!loc && node) {
  166. const state = {
  167. loc: null
  168. };
  169. (0, _traverse().default)(node, errorVisitor, this.scope, state);
  170. loc = state.loc;
  171. let txt = "This is an error on an internal node. Probably an internal error.";
  172. if (loc) txt += " Location has been estimated.";
  173. msg += ` (${txt})`;
  174. }
  175. if (loc) {
  176. const {
  177. highlightCode = true
  178. } = this.opts;
  179. msg += "\n" + (0, _codeFrame().codeFrameColumns)(this.code, {
  180. start: {
  181. line: loc.start.line,
  182. column: loc.start.column + 1
  183. },
  184. end: loc.end && loc.start.line === loc.end.line ? {
  185. line: loc.end.line,
  186. column: loc.end.column + 1
  187. } : undefined
  188. }, {
  189. highlightCode
  190. });
  191. }
  192. return new _Error(msg);
  193. }
  194. }
  195. exports.default = File;
  196. {
  197. File.prototype.addImport = function addImport() {
  198. throw new Error("This API has been removed. If you're looking for this " + "functionality in Babel 7, you should import the " + "'@babel/helper-module-imports' module and use the functions exposed " + " from that module, such as 'addNamed' or 'addDefault'.");
  199. };
  200. File.prototype.addTemplateObject = function addTemplateObject() {
  201. throw new Error("This function has been moved into the template literal transform itself.");
  202. };
  203. {
  204. File.prototype.getModuleName = function getModuleName() {
  205. return _babel7Helpers.getModuleName()(this.opts, this.opts);
  206. };
  207. }
  208. }
  209. 0 && 0;
  210. //# sourceMappingURL=file.js.map