1 | "use strict";
|
2 |
|
3 | Object.defineProperty(exports, "__esModule", {
|
4 | value: true
|
5 | });
|
6 | exports.default = parser;
|
7 |
|
8 | function _parser() {
|
9 | const data = require("@babel/parser");
|
10 |
|
11 | _parser = function () {
|
12 | return data;
|
13 | };
|
14 |
|
15 | return data;
|
16 | }
|
17 |
|
18 | function _codeFrame() {
|
19 | const data = require("@babel/code-frame");
|
20 |
|
21 | _codeFrame = function () {
|
22 | return data;
|
23 | };
|
24 |
|
25 | return data;
|
26 | }
|
27 |
|
28 | var _missingPluginHelper = require("./util/missing-plugin-helper");
|
29 |
|
30 | function* parser(pluginPasses, {
|
31 | parserOpts,
|
32 | highlightCode = true,
|
33 | filename = "unknown"
|
34 | }, code) {
|
35 | try {
|
36 | const results = [];
|
37 |
|
38 | for (const plugins of pluginPasses) {
|
39 | for (const plugin of plugins) {
|
40 | const {
|
41 | parserOverride
|
42 | } = plugin;
|
43 |
|
44 | if (parserOverride) {
|
45 | const ast = parserOverride(code, parserOpts, _parser().parse);
|
46 | if (ast !== undefined) results.push(ast);
|
47 | }
|
48 | }
|
49 | }
|
50 |
|
51 | if (results.length === 0) {
|
52 | return (0, _parser().parse)(code, parserOpts);
|
53 | } else if (results.length === 1) {
|
54 | yield* [];
|
55 |
|
56 | if (typeof results[0].then === "function") {
|
57 | 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.`);
|
58 | }
|
59 |
|
60 | return results[0];
|
61 | }
|
62 |
|
63 | throw new Error("More than one plugin attempted to override parsing.");
|
64 | } catch (err) {
|
65 | if (err.code === "BABEL_PARSER_SOURCETYPE_MODULE_REQUIRED") {
|
66 | err.message += "\nConsider renaming the file to '.mjs', or setting sourceType:module " + "or sourceType:unambiguous in your Babel config for this file.";
|
67 | }
|
68 |
|
69 | const {
|
70 | loc,
|
71 | missingPlugin
|
72 | } = err;
|
73 |
|
74 | if (loc) {
|
75 | const codeFrame = (0, _codeFrame().codeFrameColumns)(code, {
|
76 | start: {
|
77 | line: loc.line,
|
78 | column: loc.column + 1
|
79 | }
|
80 | }, {
|
81 | highlightCode
|
82 | });
|
83 |
|
84 | if (missingPlugin) {
|
85 | err.message = `${filename}: ` + (0, _missingPluginHelper.default)(missingPlugin[0], loc, codeFrame);
|
86 | } else {
|
87 | err.message = `${filename}: ${err.message}\n\n` + codeFrame;
|
88 | }
|
89 |
|
90 | err.code = "BABEL_PARSE_ERROR";
|
91 | }
|
92 |
|
93 | throw err;
|
94 | }
|
95 | } |
\ | No newline at end of file |