maybeParse.cjs 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. "use strict";
  2. function asyncGeneratorStep(n, t, e, r, o, a, c) { try { var i = n[a](c), u = i.value; } catch (n) { return void e(n); } i.done ? t(u) : Promise.resolve(u).then(r, o); }
  3. function _asyncToGenerator(n) { return function () { var t = this, e = arguments; return new Promise(function (r, o) { var a = n.apply(t, e); function _next(n) { asyncGeneratorStep(a, r, o, _next, _throw, "next", n); } function _throw(n) { asyncGeneratorStep(a, r, o, _next, _throw, "throw", n); } _next(void 0); }); }; }
  4. const babel = require("./babel-core.cjs");
  5. const convert = require("../convert/index.cjs");
  6. const astInfo = require("./ast-info.cjs");
  7. const extractParserOptionsPlugin = require("./extract-parser-options-plugin.cjs");
  8. const {
  9. getVisitorKeys,
  10. getTokLabels
  11. } = astInfo;
  12. const ref = {};
  13. let extractParserOptionsConfigItem;
  14. const MULTIPLE_OVERRIDES = /More than one plugin attempted to override parsing/;
  15. module.exports = function () {
  16. var _asyncMaybeParse = _asyncToGenerator(function* (code, options) {
  17. if (!extractParserOptionsConfigItem) {
  18. extractParserOptionsConfigItem = yield babel.createConfigItemAsync([extractParserOptionsPlugin, ref], {
  19. dirname: __dirname,
  20. type: "plugin"
  21. });
  22. }
  23. const {
  24. plugins
  25. } = options;
  26. options.plugins = plugins.concat(extractParserOptionsConfigItem);
  27. let ast;
  28. try {
  29. return {
  30. parserOptions: yield babel.parseAsync(code, options),
  31. ast: null
  32. };
  33. } catch (err) {
  34. if (!MULTIPLE_OVERRIDES.test(err.message)) {
  35. throw err;
  36. }
  37. }
  38. options.plugins = plugins;
  39. try {
  40. ast = yield babel.parseAsync(code, options);
  41. } catch (err) {
  42. throw convert.convertError(err);
  43. }
  44. return {
  45. ast: convert.convertFile(ast, code, getTokLabels(), getVisitorKeys()),
  46. parserOptions: null
  47. };
  48. });
  49. function asyncMaybeParse(_x, _x2) {
  50. return _asyncMaybeParse.apply(this, arguments);
  51. }
  52. return asyncMaybeParse;
  53. }();
  54. //# sourceMappingURL=maybeParse.cjs.map