/
index.ts
129 lines (108 loc) 路 3.07 KB
/
index.ts
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
import * as whitespace from "./whitespace";
import * as parens from "./parentheses";
import {
FLIPPED_ALIAS_KEYS,
isCallExpression,
isExpressionStatement,
isMemberExpression,
isNewExpression,
} from "@babel/types";
import type * as t from "@babel/types";
export type NodeHandlers<R> = {
[K in string]?: (
node: K extends t.Node["type"] ? Extract<t.Node, { type: K }> : t.Node,
// todo:
// node: K extends keyof typeof t
// ? Extract<typeof t[K], { type: "string" }>
// : t.Node,
parent: t.Node,
stack: t.Node[],
) => R;
};
function expandAliases<R>(obj: NodeHandlers<R>) {
const newObj: NodeHandlers<R> = {};
function add(
type: string,
func: (node: t.Node, parent: t.Node, stack: t.Node[]) => R,
) {
const fn = newObj[type];
newObj[type] = fn
? function (node, parent, stack) {
const result = fn(node, parent, stack);
return result == null ? func(node, parent, stack) : result;
}
: func;
}
for (const type of Object.keys(obj)) {
const aliases = FLIPPED_ALIAS_KEYS[type];
if (aliases) {
for (const alias of aliases) {
add(alias, obj[type]);
}
} else {
add(type, obj[type]);
}
}
return newObj;
}
// Rather than using `t.is` on each object property, we pre-expand any type aliases
// into concrete types so that the 'find' call below can be as fast as possible.
const expandedParens = expandAliases(parens);
const expandedWhitespaceNodes = expandAliases(whitespace.nodes);
const expandedWhitespaceList = expandAliases(whitespace.list);
function find<R>(
obj: NodeHandlers<R>,
node: t.Node,
parent: t.Node,
printStack?: t.Node[],
): R | null {
const fn = obj[node.type];
return fn ? fn(node, parent, printStack) : null;
}
function isOrHasCallExpression(node: t.Node): boolean {
if (isCallExpression(node)) {
return true;
}
return isMemberExpression(node) && isOrHasCallExpression(node.object);
}
export function needsWhitespace(
node: t.Node,
parent: t.Node,
type: number,
): boolean {
if (!node) return false;
if (isExpressionStatement(node)) {
node = node.expression;
}
let linesInfo: any = find(expandedWhitespaceNodes, node, parent);
if (!linesInfo) {
const items = find(expandedWhitespaceList, node, parent);
if (items) {
for (let i = 0; i < items.length; i++) {
linesInfo = needsWhitespace(items[i], node, type);
if (linesInfo) break;
}
}
}
if (typeof linesInfo === "number") {
return (linesInfo & type) !== 0;
}
return linesInfo || false;
}
export function needsWhitespaceBefore(node: t.Node, parent: t.Node) {
return needsWhitespace(node, parent, 1);
}
export function needsWhitespaceAfter(node: t.Node, parent: t.Node) {
return needsWhitespace(node, parent, 2);
}
export function needsParens(
node: t.Node,
parent: t.Node,
printStack?: t.Node[],
) {
if (!parent) return false;
if (isNewExpression(parent) && parent.callee === node) {
if (isOrHasCallExpression(node)) return true;
}
return find(expandedParens, node, parent, printStack);
}