forked from prettier/prettier
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparser.js
More file actions
67 lines (59 loc) · 1.46 KB
/
Copy pathparser.js
File metadata and controls
67 lines (59 loc) · 1.46 KB
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
"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) {
throw new Error(`Couldn't resolve parser "${opts.parser}"`);
}
}
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;
}
throw error.stack;
}
}
module.exports = { parse };