-
-
Notifications
You must be signed in to change notification settings - Fork 4.2k
/
postprocess.js
185 lines (167 loc) · 5.06 KB
/
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
"use strict";
const {
getLast,
getNextNonSpaceNonCommentCharacter,
} = require("../common/util");
const { composeLoc, locEnd } = require("./loc");
const { isTypeCastComment } = require("./comments");
function postprocess(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") {
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.
visitNode(ast, (node) => {
if (
node.leadingComments &&
node.leadingComments.some(isTypeCastComment)
) {
startOffsetsOfTypeCastedNodes.add(node.start);
}
});
visitNode(ast, (node) => {
if (
node.type === "ParenthesizedExpression" &&
!startOffsetsOfTypeCastedNodes.has(node.start)
) {
const { expression } = node;
if (!expression.extra) {
expression.extra = {};
}
expression.extra.parenthesized = true;
expression.extra.parenStart = node.start;
return expression;
}
});
}
visitNode(ast, (node) => {
switch (node.type) {
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": {
return { ...node.typeAnnotation, ...composeLoc(node) };
}
case "TSUnionType":
case "TSIntersectionType":
if (node.types.length === 1) {
// override loc, so that comments are attached properly
return { ...node.types[0], ...composeLoc(node) };
}
break;
case "TSTypeParameter":
// babel-ts
if (typeof node.name === "string") {
node.name = {
type: "Identifier",
name: node.name,
...composeLoc(node, node.name.length),
};
}
break;
case "SequenceExpression":
// Babel (unlike other parsers) includes spaces and comments in the range. Let's unify this.
if (node.end && node.end > getLast(node.expressions).end) {
node.end = getLast(node.expressions).end;
}
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;
}
if (Array.isArray(toBeOverriddenNode.range)) {
toBeOverriddenNode.range = [
toBeOverriddenNode.range[0],
toOverrideNode.range[1],
];
} else {
toBeOverriddenNode.end = toOverrideNode.end;
}
toBeOverriddenNode.loc = {
...toBeOverriddenNode.loc,
end: toBeOverriddenNode.loc.end,
};
}
}
function visitNode(node, fn, parent, property) {
if (!node || typeof node !== "object") {
return;
}
if (Array.isArray(node)) {
for (let i = 0; i < node.length; i++) {
visitNode(node[i], fn, node, i);
}
return;
}
if (typeof node.type !== "string") {
return;
}
for (const key of Object.keys(node)) {
visitNode(node[key], fn, node, key);
}
const replacement = fn(node);
if (replacement) {
parent[property] = replacement;
}
}
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,
...composeLoc(node.left, node.right.left),
}),
right: node.right.right,
...composeLoc(node),
});
}
module.exports = postprocess;