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