forked from prettier/prettier
/
index.js
227 lines (210 loc) · 6.81 KB
/
index.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
"use strict";
const { locStart, locEnd } = require("../../loc.js");
const isTsKeywordType = require("../../utils/is-ts-keyword-type.js");
const isTypeCastComment = require("../../utils/is-type-cast-comment.js");
const getLast = require("../../../utils/get-last.js");
const visitNode = require("./visit-node.js");
const { throwErrorForInvalidNodes } = require("./typescript.js");
const throwSyntaxError = require("./throw-syntax-error.js");
function postprocess(ast, options) {
if (
options.parser === "typescript" &&
// decorators or abstract properties
/@|abstract/.test(options.originalText)
) {
throwErrorForInvalidNodes(ast, options);
}
// 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 !== "acorn" &&
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": {
if (
!(
isTsKeywordType(node.typeAnnotation) ||
node.typeAnnotation.type === "TSThisType"
)
) {
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 "ObjectExpression":
// #12963
if (options.parser === "typescript") {
const invalidProperty = node.properties.find(
(property) =>
property.type === "Property" &&
property.value.type === "TSEmptyBodyFunctionExpression"
);
if (invalidProperty) {
throwSyntaxError(invalidProperty.value, "Unexpected token.");
}
}
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;
}
// For hack-style pipeline
case "TopicReference":
options.__isUsingHackPipeline = true;
break;
// TODO: Remove this when https://github.com/meriyah/meriyah/issues/200 get fixed
case "ExportAllDeclaration": {
const { exported } = node;
if (
options.parser === "meriyah" &&
exported &&
exported.type === "Identifier"
) {
const raw = options.originalText.slice(
locStart(exported),
locEnd(exported)
);
if (raw.startsWith('"') || raw.startsWith("'")) {
node.exported = {
...node.exported,
type: "Literal",
value: node.exported.name,
raw,
};
}
}
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 `acorn`, `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) {
switch (node.type) {
case "CallExpression":
node.type = "OptionalCallExpression";
node.callee = transformChainExpression(node.callee);
break;
case "MemberExpression":
node.type = "OptionalMemberExpression";
node.object = transformChainExpression(node.object);
break;
// typescript
case "TSNonNullExpression":
node.expression = transformChainExpression(node.expression);
break;
// No default
}
return 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)],
});
}
module.exports = postprocess;