2017-01-20 00:23:04 +03:00
|
|
|
"use strict";
|
2017-01-28 18:50:22 +03:00
|
|
|
|
2017-05-09 04:16:35 +03:00
|
|
|
function parse(text, opts) {
|
|
|
|
let parseFunction;
|
|
|
|
|
|
|
|
if (opts.parser === "flow") {
|
2017-05-29 07:49:41 +03:00
|
|
|
parseFunction = eval("require")("./src/parser-flow");
|
2017-06-05 22:34:08 +03:00
|
|
|
} else if (opts.parser === "graphql") {
|
|
|
|
parseFunction = eval("require")("./src/parser-graphql");
|
2017-05-09 04:16:35 +03:00
|
|
|
} else if (opts.parser === "typescript") {
|
2017-05-29 07:49:41 +03:00
|
|
|
parseFunction = eval("require")("./src/parser-typescript");
|
2017-05-24 20:52:12 +03:00
|
|
|
} else if (opts.parser === "postcss") {
|
2017-05-29 07:49:41 +03:00
|
|
|
parseFunction = eval("require")("./src/parser-postcss");
|
2017-05-09 04:16:35 +03:00
|
|
|
} else {
|
2017-05-29 07:49:41 +03:00
|
|
|
parseFunction = eval("require")("./src/parser-babylon");
|
2017-05-09 04:16:35 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
return parseFunction(text);
|
|
|
|
} catch (error) {
|
|
|
|
const loc = error.loc;
|
|
|
|
|
|
|
|
if (loc) {
|
|
|
|
const codeFrame = require("babel-code-frame");
|
2017-06-03 00:11:05 +03:00
|
|
|
error.codeFrame = codeFrame.codeFrameColumns(text, loc, {
|
2017-05-09 04:16:35 +03:00
|
|
|
highlightCode: true
|
|
|
|
});
|
|
|
|
error.message += "\n" + error.codeFrame;
|
2017-05-27 01:29:33 +03:00
|
|
|
throw error;
|
2017-05-09 04:16:35 +03:00
|
|
|
}
|
|
|
|
|
2017-05-27 01:29:33 +03:00
|
|
|
throw error.stack;
|
2017-05-09 04:16:35 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = { parse };
|