forked from OKEAMAH/prettier
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparse.js
More file actions
42 lines (34 loc) · 958 Bytes
/
parse.js
File metadata and controls
42 lines (34 loc) · 958 Bytes
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
import { codeFrameColumns } from "@babel/code-frame";
import { resolveParser } from "./parser-and-printer.js";
async function parse(originalText, options) {
const parser = await resolveParser(options);
const text = parser.preprocess
? parser.preprocess(originalText, options)
: originalText;
options.originalText = text;
let ast;
try {
ast = await parser.parse(
text,
options,
// TODO: remove the third argument in v4
// The duplicated argument is passed as intended, see #10156
options,
);
} catch (error) {
handleParseError(error, originalText);
}
return { text, ast };
}
function handleParseError(error, text) {
const { loc } = error;
if (loc) {
const codeFrame = codeFrameColumns(text, loc, { highlightCode: true });
error.message += "\n" + codeFrame;
error.codeFrame = codeFrame;
throw error;
}
/* c8 ignore next */
throw error;
}
export default parse;