2017-01-19 13:23:04 -08:00
|
|
|
"use strict";
|
2017-01-28 07:50:22 -08:00
|
|
|
|
2017-05-08 21:16:35 -04:00
|
|
|
function parse(text, opts) {
|
|
|
|
|
let parseFunction;
|
|
|
|
|
|
|
|
|
|
if (opts.parser === "flow") {
|
2017-05-28 21:49:41 -07:00
|
|
|
parseFunction = eval("require")("./src/parser-flow");
|
2017-05-08 21:16:35 -04:00
|
|
|
} else if (opts.parser === "typescript") {
|
2017-05-28 21:49:41 -07:00
|
|
|
parseFunction = eval("require")("./src/parser-typescript");
|
2017-05-24 10:52:12 -07:00
|
|
|
} else if (opts.parser === "postcss") {
|
2017-05-28 21:49:41 -07:00
|
|
|
parseFunction = eval("require")("./src/parser-postcss");
|
2017-05-08 21:16:35 -04:00
|
|
|
} else {
|
2017-05-28 21:49:41 -07:00
|
|
|
parseFunction = eval("require")("./src/parser-babylon");
|
2017-05-08 21:16:35 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
return parseFunction(text);
|
|
|
|
|
} catch (error) {
|
|
|
|
|
const loc = error.loc;
|
|
|
|
|
|
|
|
|
|
if (loc) {
|
|
|
|
|
const codeFrame = require("babel-code-frame");
|
|
|
|
|
error.codeFrame = codeFrame(text, loc.line, loc.column + 1, {
|
|
|
|
|
highlightCode: true
|
|
|
|
|
});
|
|
|
|
|
error.message += "\n" + error.codeFrame;
|
2017-05-26 15:29:33 -07:00
|
|
|
throw error;
|
2017-05-08 21:16:35 -04:00
|
|
|
}
|
|
|
|
|
|
2017-05-26 15:29:33 -07:00
|
|
|
throw error.stack;
|
2017-05-08 21:16:35 -04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
module.exports = { parse };
|