prettier
Version:
Prettier is an opinionated JavaScript formatter
54 lines (46 loc) • 1.18 kB
JavaScript
;
function parseWithFlow(text, filename) {
// Inline the require to avoid loading all the JS if we don't use it
const flowParser = require("flow-parser");
const ast = flowParser.parse(text, {
esproposal_class_instance_fields: true,
esproposal_class_static_fields: true,
esproposal_export_star_as: true
});
if (ast.errors.length > 0) {
let msg = ast.errors[0].message +
" on line " +
ast.errors[0].loc.start.line;
if (filename) {
msg += " in file " + filename;
}
throw new Error(msg);
}
return ast;
}
function parseWithBabylon(text) {
// Inline the require to avoid loading all the JS if we don't use it
const babylon = require("babylon");
return babylon.parse(text, {
sourceType: "module",
allowImportExportEverywhere: false,
allowReturnOutsideFunction: false,
plugins: [
"jsx",
"flow",
"doExpressions",
"objectRestSpread",
"decorators",
"classProperties",
"exportExtensions",
"asyncGenerators",
"functionBind",
"functionSent",
"dynamicImport"
]
});
}
module.exports = {
parseWithFlow,
parseWithBabylon
};