forked from prettier/prettier
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparser-babylon.js
More file actions
62 lines (56 loc) · 1.44 KB
/
Copy pathparser-babylon.js
File metadata and controls
62 lines (56 loc) · 1.44 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
"use strict";
const createError = require("./parser-create-error");
function parse(text, parsers, opts) {
// Inline the require to avoid loading all the JS if we don't use it
const babylon = require("babylon");
const babylonOptions = {
sourceType: "module",
allowImportExportEverywhere: true,
allowReturnOutsideFunction: true,
plugins: [
"jsx",
"flow",
"doExpressions",
"objectRestSpread",
"decorators",
"classProperties",
"exportExtensions",
"asyncGenerators",
"functionBind",
"functionSent",
"dynamicImport",
"numericSeparator",
"importMeta",
"optionalCatchBinding",
"optionalChaining"
]
};
const parseMethod =
opts && opts.parser === "json" ? "parseExpression" : "parse";
let ast;
try {
ast = babylon[parseMethod](text, babylonOptions);
} catch (originalError) {
try {
ast = babylon[parseMethod](
text,
Object.assign({}, babylonOptions, { strictMode: false })
);
} catch (nonStrictError) {
throw createError(
// babel error prints (l:c) with cols that are zero indexed
// so we need our custom error
originalError.message.replace(/ \(.*\)/, ""),
{
start: {
line: originalError.loc.line,
column: originalError.loc.column + 1
}
}
);
}
}
delete ast.tokens;
return ast;
}
module.exports = parse;