forked from prettier/prettier
/
parser-babylon.js
212 lines (196 loc) · 5.69 KB
/
parser-babylon.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
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
"use strict";
// This file is currently named parser-babylon.js to maintain backwards compatibility.
// However, it should be named parser-babel.js in the next major release.
const createError = require("../common/parser-create-error");
const hasPragma = require("./pragma").hasPragma;
const locFns = require("./loc");
const postprocess = require("./postprocess");
function babelOptions(extraOptions, extraPlugins) {
return Object.assign(
{
sourceType: "module",
allowAwaitOutsideFunction: true,
allowImportExportEverywhere: true,
allowReturnOutsideFunction: true,
allowSuperOutsideMethod: true,
allowUndeclaredExports: true,
errorRecovery: true,
plugins: [
"jsx",
"doExpressions",
"objectRestSpread",
"classProperties",
"exportDefaultFrom",
"exportNamespaceFrom",
"asyncGenerators",
"functionBind",
"functionSent",
"dynamicImport",
"numericSeparator",
"importMeta",
"optionalCatchBinding",
"optionalChaining",
"classPrivateProperties",
["pipelineOperator", { proposal: "minimal" }],
"nullishCoalescingOperator",
"bigInt",
"throwExpressions",
"logicalAssignment",
"classPrivateMethods",
"v8intrinsic",
"partialApplication"
].concat(extraPlugins)
},
extraOptions
);
}
function createParse(parseMethod, extraPlugins) {
return (text, parsers, opts) => {
// Inline the require to avoid loading all the JS if we don't use it
const babel = require("@babel/parser");
const combinations = [
babelOptions(
{ strictMode: true },
["decorators-legacy"].concat(extraPlugins)
),
babelOptions(
{ strictMode: false },
["decorators-legacy"].concat(extraPlugins)
),
babelOptions(
{ strictMode: true },
[["decorators", { decoratorsBeforeExport: false }]].concat(extraPlugins)
),
babelOptions(
{ strictMode: false },
[["decorators", { decoratorsBeforeExport: false }]].concat(extraPlugins)
)
];
let ast;
try {
ast = tryCombinations(babel[parseMethod].bind(null, text), combinations);
} catch (error) {
throw createError(
// babel error prints (l:c) with cols that are zero indexed
// so we need our custom error
error.message.replace(/ \(.*\)/, ""),
{
start: {
line: error.loc.line,
column: error.loc.column + 1
}
}
);
}
delete ast.tokens;
return postprocess(ast, Object.assign({}, opts, { originalText: text }));
};
}
const parse = createParse("parse", ["flow"]);
const parseFlow = createParse("parse", [["flow", { all: true }]]);
const parseExpression = createParse("parseExpression");
function tryCombinations(fn, combinations) {
let error;
for (let i = 0; i < combinations.length; i++) {
try {
return fn(combinations[i]);
} catch (_error) {
if (!error) {
error = _error;
}
}
}
throw error;
}
function parseJson(text, parsers, opts) {
const ast = parseExpression(text, parsers, opts);
ast.comments.forEach(assertJsonNode);
assertJsonNode(ast);
return ast;
}
function assertJsonNode(node, parent) {
switch (node.type) {
case "ArrayExpression":
return node.elements.forEach(assertJsonChildNode);
case "ObjectExpression":
return node.properties.forEach(assertJsonChildNode);
case "ObjectProperty":
// istanbul ignore if
if (node.computed) {
throw createJsonError("computed");
}
// istanbul ignore if
if (node.shorthand) {
throw createJsonError("shorthand");
}
return [node.key, node.value].forEach(assertJsonChildNode);
case "UnaryExpression":
switch (node.operator) {
case "+":
case "-":
return assertJsonChildNode(node.argument);
// istanbul ignore next
default:
throw createJsonError("operator");
}
case "Identifier":
if (parent && parent.type === "ObjectProperty" && parent.key === node) {
return;
}
throw createJsonError();
case "NullLiteral":
case "BooleanLiteral":
case "NumericLiteral":
case "StringLiteral":
return;
// istanbul ignore next
default:
throw createJsonError();
}
function assertJsonChildNode(child) {
return assertJsonNode(child, node);
}
// istanbul ignore next
function createJsonError(attribute) {
const name = !attribute
? node.type
: `${node.type} with ${attribute}=${JSON.stringify(node[attribute])}`;
return createError(`${name} is not allowed in JSON.`, {
start: {
line: node.loc.start.line,
column: node.loc.start.column + 1
}
});
}
}
const babel = Object.assign({ parse, astFormat: "estree", hasPragma }, locFns);
const babelFlow = Object.assign({}, babel, { parse: parseFlow });
const babelExpression = Object.assign({}, babel, { parse: parseExpression });
// Export as a plugin so we can reuse the same bundle for UMD loading
module.exports = {
parsers: {
babel,
"babel-flow": babelFlow,
// aliased to keep backwards compatibility
babylon: babel,
json: Object.assign({}, babelExpression, {
hasPragma() {
return true;
}
}),
json5: babelExpression,
"json-stringify": Object.assign(
{
parse: parseJson,
astFormat: "estree-json"
},
locFns
),
/** @internal */
__js_expression: babelExpression,
/** for vue filter */
__vue_expression: babelExpression,
/** for vue event binding to handle semicolon */
__vue_event_binding: babel
}
};