index.js 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.default = parser;
  6. function _parser() {
  7. const data = require("@babel/parser");
  8. _parser = function () {
  9. return data;
  10. };
  11. return data;
  12. }
  13. function _codeFrame() {
  14. const data = require("@babel/code-frame");
  15. _codeFrame = function () {
  16. return data;
  17. };
  18. return data;
  19. }
  20. var _missingPluginHelper = require("./util/missing-plugin-helper.js");
  21. function* parser(pluginPasses, {
  22. parserOpts,
  23. highlightCode = true,
  24. filename = "unknown"
  25. }, code) {
  26. try {
  27. const results = [];
  28. for (const plugins of pluginPasses) {
  29. for (const plugin of plugins) {
  30. const {
  31. parserOverride
  32. } = plugin;
  33. if (parserOverride) {
  34. const ast = parserOverride(code, parserOpts, _parser().parse);
  35. if (ast !== undefined) results.push(ast);
  36. }
  37. }
  38. }
  39. if (results.length === 0) {
  40. return (0, _parser().parse)(code, parserOpts);
  41. } else if (results.length === 1) {
  42. yield* [];
  43. if (typeof results[0].then === "function") {
  44. throw new Error(`You appear to be using an async parser plugin, ` + `which your current version of Babel does not support. ` + `If you're using a published plugin, you may need to upgrade ` + `your @babel/core version.`);
  45. }
  46. return results[0];
  47. }
  48. throw new Error("More than one plugin attempted to override parsing.");
  49. } catch (err) {
  50. if (err.code === "BABEL_PARSER_SOURCETYPE_MODULE_REQUIRED") {
  51. err.message += "\nConsider renaming the file to '.mjs', or setting sourceType:module " + "or sourceType:unambiguous in your Babel config for this file.";
  52. }
  53. const {
  54. loc,
  55. missingPlugin
  56. } = err;
  57. if (loc) {
  58. const codeFrame = (0, _codeFrame().codeFrameColumns)(code, {
  59. start: {
  60. line: loc.line,
  61. column: loc.column + 1
  62. }
  63. }, {
  64. highlightCode
  65. });
  66. if (missingPlugin) {
  67. err.message = `${filename}: ` + (0, _missingPluginHelper.default)(missingPlugin[0], loc, codeFrame, filename);
  68. } else {
  69. err.message = `${filename}: ${err.message}\n\n` + codeFrame;
  70. }
  71. err.code = "BABEL_PARSE_ERROR";
  72. }
  73. throw err;
  74. }
  75. }
  76. 0 && 0;
  77. //# sourceMappingURL=index.js.map