maybeParse.cjs 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. "use strict";
  2. const babel = require("./babel-core.cjs");
  3. const convert = require("../convert/index.cjs");
  4. const astInfo = require("./ast-info.cjs");
  5. const extractParserOptionsPlugin = require("./extract-parser-options-plugin.cjs");
  6. const {
  7. getVisitorKeys,
  8. getTokLabels
  9. } = astInfo;
  10. const ref = {};
  11. let extractParserOptionsConfigItem;
  12. const MULTIPLE_OVERRIDES = /More than one plugin attempted to override parsing/;
  13. module.exports = function maybeParse(code, options) {
  14. if (!extractParserOptionsConfigItem) {
  15. extractParserOptionsConfigItem = babel.createConfigItemSync([extractParserOptionsPlugin, ref], {
  16. dirname: __dirname,
  17. type: "plugin"
  18. });
  19. }
  20. const {
  21. plugins
  22. } = options;
  23. options.plugins = plugins.concat(extractParserOptionsConfigItem);
  24. let ast;
  25. try {
  26. return {
  27. parserOptions: babel.parseSync(code, options),
  28. ast: null
  29. };
  30. } catch (err) {
  31. if (!MULTIPLE_OVERRIDES.test(err.message)) {
  32. throw err;
  33. }
  34. }
  35. options.plugins = plugins;
  36. try {
  37. ast = babel.parseSync(code, options);
  38. } catch (err) {
  39. throw convert.convertError(err);
  40. }
  41. return {
  42. ast: convert.convertFile(ast, code, getTokLabels(), getVisitorKeys()),
  43. parserOptions: null
  44. };
  45. };
  46. //# sourceMappingURL=maybeParse.cjs.map