forked from prettier/prettier
-
Notifications
You must be signed in to change notification settings - Fork 1
/
parser.js
34 lines (28 loc) · 834 Bytes
/
parser.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
"use strict";
function parse(text, opts) {
let parseFunction;
if (opts.parser === "flow") {
parseFunction = eval("require")("./src/parser-flow");
} else if (opts.parser === "typescript") {
parseFunction = eval("require")("./src/parser-typescript");
} else if (opts.parser === "postcss") {
parseFunction = eval("require")("./src/parser-postcss");
} else {
parseFunction = eval("require")("./src/parser-babylon");
}
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, {
highlightCode: true
});
error.message += "\n" + error.codeFrame;
throw error;
}
throw error.stack;
}
}
module.exports = { parse };