forked from prettier/prettier
-
Notifications
You must be signed in to change notification settings - Fork 0
/
parse-postprocess.js
271 lines (247 loc) · 7.77 KB
/
parse-postprocess.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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
"use strict";
const {
getLast,
getNextNonSpaceNonCommentCharacter,
getShebang,
} = require("../common/util");
const createError = require("../common/parser-create-error");
const { locStart, locEnd } = require("./loc");
const { isTypeCastComment } = require("./comments");
function postprocess(ast, options) {
if (
options.parser === "typescript" ||
options.parser === "flow" ||
options.parser === "espree"
) {
includeShebang(ast, options);
}
// Invalid decorators are removed since `@typescript-eslint/typescript-estree` v4
// https://github.com/typescript-eslint/typescript-eslint/pull/2375
if (options.parser === "typescript" && options.originalText.includes("@")) {
const {
esTreeNodeToTSNodeMap,
tsNodeToESTreeNodeMap,
} = options.tsParseResult;
ast = visitNode(ast, (node) => {
const tsNode = esTreeNodeToTSNodeMap.get(node);
if (!tsNode) {
return;
}
const tsDecorators = tsNode.decorators;
if (!Array.isArray(tsDecorators)) {
return;
}
// `esTreeNodeToTSNodeMap.get(ClassBody)` and `esTreeNodeToTSNodeMap.get(ClassDeclaration)` has the same tsNode
const esTreeNode = tsNodeToESTreeNodeMap.get(tsNode);
if (esTreeNode !== node) {
return;
}
const esTreeDecorators = esTreeNode.decorators;
if (
!Array.isArray(esTreeDecorators) ||
esTreeDecorators.length !== tsDecorators.length ||
tsDecorators.some((tsDecorator) => {
const esTreeDecorator = tsNodeToESTreeNodeMap.get(tsDecorator);
return (
!esTreeDecorator || !esTreeDecorators.includes(esTreeDecorator)
);
})
) {
const { start, end } = esTreeNode.loc;
throw createError(
"Leading decorators must be attached to a class declaration",
{
start: { line: start.line, column: start.column + 1 },
end: { line: end.line, column: end.column + 1 },
}
);
}
});
}
// Keep Babel's non-standard ParenthesizedExpression nodes only if they have Closure-style type cast comments.
if (
options.parser !== "typescript" &&
options.parser !== "flow" &&
options.parser !== "espree" &&
options.parser !== "meriyah"
) {
const startOffsetsOfTypeCastedNodes = new Set();
// Comments might be attached not directly to ParenthesizedExpression but to its ancestor.
// E.g.: /** @type {Foo} */ (foo).bar();
// Let's use the fact that those ancestors and ParenthesizedExpression have the same start offset.
ast = visitNode(ast, (node) => {
if (
node.leadingComments &&
node.leadingComments.some(isTypeCastComment)
) {
startOffsetsOfTypeCastedNodes.add(locStart(node));
}
});
ast = visitNode(ast, (node) => {
if (node.type === "ParenthesizedExpression") {
const { expression } = node;
// Align range with `flow`
if (expression.type === "TypeCastExpression") {
expression.range = node.range;
return expression;
}
const start = locStart(node);
if (!startOffsetsOfTypeCastedNodes.has(start)) {
expression.extra = { ...expression.extra, parenthesized: true };
return expression;
}
}
});
}
ast = visitNode(ast, (node) => {
switch (node.type) {
// Espree
case "ChainExpression": {
return transformChainExpression(node.expression);
}
case "LogicalExpression": {
// We remove unneeded parens around same-operator LogicalExpressions
if (isUnbalancedLogicalTree(node)) {
return rebalanceLogicalTree(node);
}
break;
}
// fix unexpected locEnd caused by --no-semi style
case "VariableDeclaration": {
const lastDeclaration = getLast(node.declarations);
if (lastDeclaration && lastDeclaration.init) {
overrideLocEnd(node, lastDeclaration);
}
break;
}
// remove redundant TypeScript nodes
case "TSParenthesizedType": {
node.typeAnnotation.range = [locStart(node), locEnd(node)];
return node.typeAnnotation;
}
case "TSTypeParameter":
// babel-ts
if (typeof node.name === "string") {
const start = locStart(node);
node.name = {
type: "Identifier",
name: node.name,
range: [start, start + node.name.length],
};
}
break;
case "SequenceExpression": {
// Babel (unlike other parsers) includes spaces and comments in the range. Let's unify this.
const lastExpression = getLast(node.expressions);
node.range = [
locStart(node),
Math.min(locEnd(lastExpression), locEnd(node)),
];
break;
}
case "ClassProperty":
// TODO: Temporary auto-generated node type. To remove when typescript-estree has proper support for private fields.
if (
node.key &&
node.key.type === "TSPrivateIdentifier" &&
getNextNonSpaceNonCommentCharacter(
options.originalText,
node.key,
locEnd
) === "?"
) {
node.optional = true;
}
break;
}
});
return ast;
/**
* - `toOverrideNode` must be the last thing in `toBeOverriddenNode`
* - do nothing if there's a semicolon on `toOverrideNode.end` (no need to fix)
*/
function overrideLocEnd(toBeOverriddenNode, toOverrideNode) {
if (options.originalText[locEnd(toOverrideNode)] === ";") {
return;
}
toBeOverriddenNode.range = [
locStart(toBeOverriddenNode),
locEnd(toOverrideNode),
];
}
}
// This is a workaround to transform `ChainExpression` from `espree`, `meriyah`,
// and `typescript` into `babel` shape AST, we should do the opposite,
// since `ChainExpression` is the standard `estree` AST for `optional chaining`
// https://github.com/estree/estree/blob/master/es2020.md
function transformChainExpression(node) {
if (node.type === "CallExpression") {
node.type = "OptionalCallExpression";
node.callee = transformChainExpression(node.callee);
} else if (node.type === "MemberExpression") {
node.type = "OptionalMemberExpression";
node.object = transformChainExpression(node.object);
}
// typescript
else if (node.type === "TSNonNullExpression") {
node.expression = transformChainExpression(node.expression);
}
return node;
}
function visitNode(node, fn) {
let entries;
if (Array.isArray(node)) {
entries = node.entries();
} else if (
node &&
typeof node === "object" &&
typeof node.type === "string"
) {
entries = Object.entries(node);
} else {
return node;
}
for (const [key, child] of entries) {
node[key] = visitNode(child, fn);
}
if (Array.isArray(node)) {
return node;
}
return fn(node) || node;
}
function isUnbalancedLogicalTree(node) {
return (
node.type === "LogicalExpression" &&
node.right.type === "LogicalExpression" &&
node.operator === node.right.operator
);
}
function rebalanceLogicalTree(node) {
if (!isUnbalancedLogicalTree(node)) {
return node;
}
return rebalanceLogicalTree({
type: "LogicalExpression",
operator: node.operator,
left: rebalanceLogicalTree({
type: "LogicalExpression",
operator: node.operator,
left: node.left,
right: node.right.left,
range: [locStart(node.left), locEnd(node.right.left)],
}),
right: node.right.right,
range: [locStart(node), locEnd(node)],
});
}
function includeShebang(ast, options) {
const shebang = getShebang(options.originalText);
if (shebang) {
ast.comments.unshift({
type: "Line",
value: shebang.slice(2),
range: [0, shebang.length],
});
}
}
module.exports = postprocess;