forked from babel/babel
-
Notifications
You must be signed in to change notification settings - Fork 0
/
types.js
173 lines (141 loc) 路 3.78 KB
/
types.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
import * as t from "@babel/types";
import jsesc from "jsesc";
export function Identifier(node: Object) {
this.exactSource(node.loc, () => {
this.word(node.name);
});
}
export function ArgumentPlaceholder() {
this.token("?");
}
export function RestElement(node: Object) {
this.token("...");
this.print(node.argument, node);
}
export { RestElement as SpreadElement };
export function ObjectExpression(node: Object) {
const props = node.properties;
this.token("{");
this.printInnerComments(node);
if (props.length) {
this.space();
this.printList(props, node, { indent: true, statement: true });
this.space();
}
this.token("}");
}
export { ObjectExpression as ObjectPattern };
export function ObjectMethod(node: Object) {
this.printJoin(node.decorators, node);
this._methodHead(node);
this.space();
this.print(node.body, node);
}
export function ObjectProperty(node: Object) {
this.printJoin(node.decorators, node);
if (node.computed) {
this.token("[");
this.print(node.key, node);
this.token("]");
} else {
// print `({ foo: foo = 5 } = {})` as `({ foo = 5 } = {});`
if (
t.isAssignmentPattern(node.value) &&
t.isIdentifier(node.key) &&
node.key.name === node.value.left.name
) {
this.print(node.value, node);
return;
}
this.print(node.key, node);
// shorthand!
if (
node.shorthand &&
(t.isIdentifier(node.key) &&
t.isIdentifier(node.value) &&
node.key.name === node.value.name)
) {
return;
}
}
this.token(":");
this.space();
this.print(node.value, node);
}
export function ArrayExpression(node: Object) {
const elems = node.elements;
const len = elems.length;
this.token("[");
this.printInnerComments(node);
for (let i = 0; i < elems.length; i++) {
const elem = elems[i];
if (elem) {
if (i > 0) this.space();
this.print(elem, node);
if (i < len - 1) this.token(",");
} else {
// If the array expression ends with a hole, that hole
// will be ignored by the interpreter, but if it ends with
// two (or more) holes, we need to write out two (or more)
// commas so that the resulting code is interpreted with
// both (all) of the holes.
this.token(",");
}
}
this.token("]");
}
export { ArrayExpression as ArrayPattern };
export function RegExpLiteral(node: Object) {
this.word(`/${node.pattern}/${node.flags}`);
}
export function BooleanLiteral(node: Object) {
this.word(node.value ? "true" : "false");
}
export function NullLiteral() {
this.word("null");
}
export function NumericLiteral(node: Object) {
const raw = this.getPossibleRaw(node);
const value = node.value + "";
if (raw == null) {
this.number(value); // normalize
} else if (this.format.minified) {
this.number(raw.length < value.length ? raw : value);
} else {
this.number(raw);
}
}
export function StringLiteral(node: Object) {
const raw = this.getPossibleRaw(node);
if (!this.format.minified && raw != null) {
this.token(raw);
return;
}
// ensure the output is ASCII-safe
const opts = this.format.jsescOption;
if (this.format.jsonCompatibleStrings) {
opts.json = true;
}
const val = jsesc(node.value, opts);
return this.token(val);
}
export function BigIntLiteral(node: Object) {
const raw = this.getPossibleRaw(node);
if (!this.format.minified && raw != null) {
this.token(raw);
return;
}
this.token(node.value);
}
export function PipelineBody(node: Object) {
this.print(node.body, node);
}
export function PipelineTopicExpression(node: Object) {
this.print(node.expression, node);
}
export function PipelineBareFunction(node: Object) {
this.print(node.callee, node);
}
export function PipelinePrimaryTopicReference() {
this.token("#");
}