Files
test-repo/src/parser.js
T
Simon Lydell 133303f47a Ignore some things in coverage (#2741)
- If statements for old deprecated stuff.
- "This should never happen" (and never happens in the tests) code paths.
2017-09-03 13:40:44 -07:00

71 lines
1.5 KiB
JavaScript

"use strict";
const path = require("path");
const parsers = {
get flow() {
return eval("require")("./parser-flow");
},
get graphql() {
return eval("require")("./parser-graphql");
},
get parse5() {
return eval("require")("./parser-parse5");
},
get babylon() {
return eval("require")("./parser-babylon");
},
get typescript() {
return eval("require")("./parser-typescript");
},
get postcss() {
return eval("require")("./parser-postcss");
},
get json() {
return eval("require")("./parser-babylon");
}
};
function resolveParseFunction(opts) {
if (typeof opts.parser === "function") {
return opts.parser;
}
if (typeof opts.parser === "string") {
if (parsers.hasOwnProperty(opts.parser)) {
return parsers[opts.parser];
}
try {
return eval("require")(path.resolve(process.cwd(), opts.parser));
} catch (err) {
/* istanbul ignore next */
throw new Error(`Couldn't resolve parser "${opts.parser}"`);
}
}
/* istanbul ignore next */
return parsers.babylon;
}
function parse(text, opts) {
const parseFunction = resolveParseFunction(opts);
try {
return parseFunction(text, parsers, opts);
} catch (error) {
const loc = error.loc;
if (loc) {
const codeFrame = require("babel-code-frame");
error.codeFrame = codeFrame.codeFrameColumns(text, loc, {
highlightCode: true
});
error.message += "\n" + error.codeFrame;
throw error;
}
/* istanbul ignore next */
throw error.stack;
}
}
module.exports = { parse };