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