1 | "use strict";
|
---|
2 |
|
---|
3 | Object.defineProperty(exports, "__esModule", {
|
---|
4 | value: true
|
---|
5 | });
|
---|
6 | exports.Identifier = Identifier;
|
---|
7 | exports.ArgumentPlaceholder = ArgumentPlaceholder;
|
---|
8 | exports.SpreadElement = exports.RestElement = RestElement;
|
---|
9 | exports.ObjectPattern = exports.ObjectExpression = ObjectExpression;
|
---|
10 | exports.ObjectMethod = ObjectMethod;
|
---|
11 | exports.ObjectProperty = ObjectProperty;
|
---|
12 | exports.ArrayPattern = exports.ArrayExpression = ArrayExpression;
|
---|
13 | exports.RecordExpression = RecordExpression;
|
---|
14 | exports.TupleExpression = TupleExpression;
|
---|
15 | exports.RegExpLiteral = RegExpLiteral;
|
---|
16 | exports.BooleanLiteral = BooleanLiteral;
|
---|
17 | exports.NullLiteral = NullLiteral;
|
---|
18 | exports.NumericLiteral = NumericLiteral;
|
---|
19 | exports.StringLiteral = StringLiteral;
|
---|
20 | exports.BigIntLiteral = BigIntLiteral;
|
---|
21 | exports.DecimalLiteral = DecimalLiteral;
|
---|
22 | exports.PipelineTopicExpression = PipelineTopicExpression;
|
---|
23 | exports.PipelineBareFunction = PipelineBareFunction;
|
---|
24 | exports.PipelinePrimaryTopicReference = PipelinePrimaryTopicReference;
|
---|
25 |
|
---|
26 | var t = require("@babel/types");
|
---|
27 |
|
---|
28 | var _jsesc = require("jsesc");
|
---|
29 |
|
---|
30 | function Identifier(node) {
|
---|
31 | this.exactSource(node.loc, () => {
|
---|
32 | this.word(node.name);
|
---|
33 | });
|
---|
34 | }
|
---|
35 |
|
---|
36 | function ArgumentPlaceholder() {
|
---|
37 | this.token("?");
|
---|
38 | }
|
---|
39 |
|
---|
40 | function RestElement(node) {
|
---|
41 | this.token("...");
|
---|
42 | this.print(node.argument, node);
|
---|
43 | }
|
---|
44 |
|
---|
45 | function ObjectExpression(node) {
|
---|
46 | const props = node.properties;
|
---|
47 | this.token("{");
|
---|
48 | this.printInnerComments(node);
|
---|
49 |
|
---|
50 | if (props.length) {
|
---|
51 | this.space();
|
---|
52 | this.printList(props, node, {
|
---|
53 | indent: true,
|
---|
54 | statement: true
|
---|
55 | });
|
---|
56 | this.space();
|
---|
57 | }
|
---|
58 |
|
---|
59 | this.token("}");
|
---|
60 | }
|
---|
61 |
|
---|
62 | function ObjectMethod(node) {
|
---|
63 | this.printJoin(node.decorators, node);
|
---|
64 |
|
---|
65 | this._methodHead(node);
|
---|
66 |
|
---|
67 | this.space();
|
---|
68 | this.print(node.body, node);
|
---|
69 | }
|
---|
70 |
|
---|
71 | function ObjectProperty(node) {
|
---|
72 | this.printJoin(node.decorators, node);
|
---|
73 |
|
---|
74 | if (node.computed) {
|
---|
75 | this.token("[");
|
---|
76 | this.print(node.key, node);
|
---|
77 | this.token("]");
|
---|
78 | } else {
|
---|
79 | if (t.isAssignmentPattern(node.value) && t.isIdentifier(node.key) && node.key.name === node.value.left.name) {
|
---|
80 | this.print(node.value, node);
|
---|
81 | return;
|
---|
82 | }
|
---|
83 |
|
---|
84 | this.print(node.key, node);
|
---|
85 |
|
---|
86 | if (node.shorthand && t.isIdentifier(node.key) && t.isIdentifier(node.value) && node.key.name === node.value.name) {
|
---|
87 | return;
|
---|
88 | }
|
---|
89 | }
|
---|
90 |
|
---|
91 | this.token(":");
|
---|
92 | this.space();
|
---|
93 | this.print(node.value, node);
|
---|
94 | }
|
---|
95 |
|
---|
96 | function ArrayExpression(node) {
|
---|
97 | const elems = node.elements;
|
---|
98 | const len = elems.length;
|
---|
99 | this.token("[");
|
---|
100 | this.printInnerComments(node);
|
---|
101 |
|
---|
102 | for (let i = 0; i < elems.length; i++) {
|
---|
103 | const elem = elems[i];
|
---|
104 |
|
---|
105 | if (elem) {
|
---|
106 | if (i > 0) this.space();
|
---|
107 | this.print(elem, node);
|
---|
108 | if (i < len - 1) this.token(",");
|
---|
109 | } else {
|
---|
110 | this.token(",");
|
---|
111 | }
|
---|
112 | }
|
---|
113 |
|
---|
114 | this.token("]");
|
---|
115 | }
|
---|
116 |
|
---|
117 | function RecordExpression(node) {
|
---|
118 | const props = node.properties;
|
---|
119 | let startToken;
|
---|
120 | let endToken;
|
---|
121 |
|
---|
122 | if (this.format.recordAndTupleSyntaxType === "bar") {
|
---|
123 | startToken = "{|";
|
---|
124 | endToken = "|}";
|
---|
125 | } else if (this.format.recordAndTupleSyntaxType === "hash") {
|
---|
126 | startToken = "#{";
|
---|
127 | endToken = "}";
|
---|
128 | } else {
|
---|
129 | throw new Error(`The "recordAndTupleSyntaxType" generator option must be "bar" or "hash" (${JSON.stringify(this.format.recordAndTupleSyntaxType)} received).`);
|
---|
130 | }
|
---|
131 |
|
---|
132 | this.token(startToken);
|
---|
133 | this.printInnerComments(node);
|
---|
134 |
|
---|
135 | if (props.length) {
|
---|
136 | this.space();
|
---|
137 | this.printList(props, node, {
|
---|
138 | indent: true,
|
---|
139 | statement: true
|
---|
140 | });
|
---|
141 | this.space();
|
---|
142 | }
|
---|
143 |
|
---|
144 | this.token(endToken);
|
---|
145 | }
|
---|
146 |
|
---|
147 | function TupleExpression(node) {
|
---|
148 | const elems = node.elements;
|
---|
149 | const len = elems.length;
|
---|
150 | let startToken;
|
---|
151 | let endToken;
|
---|
152 |
|
---|
153 | if (this.format.recordAndTupleSyntaxType === "bar") {
|
---|
154 | startToken = "[|";
|
---|
155 | endToken = "|]";
|
---|
156 | } else if (this.format.recordAndTupleSyntaxType === "hash") {
|
---|
157 | startToken = "#[";
|
---|
158 | endToken = "]";
|
---|
159 | } else {
|
---|
160 | throw new Error(`${this.format.recordAndTupleSyntaxType} is not a valid recordAndTuple syntax type`);
|
---|
161 | }
|
---|
162 |
|
---|
163 | this.token(startToken);
|
---|
164 | this.printInnerComments(node);
|
---|
165 |
|
---|
166 | for (let i = 0; i < elems.length; i++) {
|
---|
167 | const elem = elems[i];
|
---|
168 |
|
---|
169 | if (elem) {
|
---|
170 | if (i > 0) this.space();
|
---|
171 | this.print(elem, node);
|
---|
172 | if (i < len - 1) this.token(",");
|
---|
173 | }
|
---|
174 | }
|
---|
175 |
|
---|
176 | this.token(endToken);
|
---|
177 | }
|
---|
178 |
|
---|
179 | function RegExpLiteral(node) {
|
---|
180 | this.word(`/${node.pattern}/${node.flags}`);
|
---|
181 | }
|
---|
182 |
|
---|
183 | function BooleanLiteral(node) {
|
---|
184 | this.word(node.value ? "true" : "false");
|
---|
185 | }
|
---|
186 |
|
---|
187 | function NullLiteral() {
|
---|
188 | this.word("null");
|
---|
189 | }
|
---|
190 |
|
---|
191 | function NumericLiteral(node) {
|
---|
192 | const raw = this.getPossibleRaw(node);
|
---|
193 | const opts = this.format.jsescOption;
|
---|
194 | const value = node.value + "";
|
---|
195 |
|
---|
196 | if (opts.numbers) {
|
---|
197 | this.number(_jsesc(node.value, opts));
|
---|
198 | } else if (raw == null) {
|
---|
199 | this.number(value);
|
---|
200 | } else if (this.format.minified) {
|
---|
201 | this.number(raw.length < value.length ? raw : value);
|
---|
202 | } else {
|
---|
203 | this.number(raw);
|
---|
204 | }
|
---|
205 | }
|
---|
206 |
|
---|
207 | function StringLiteral(node) {
|
---|
208 | const raw = this.getPossibleRaw(node);
|
---|
209 |
|
---|
210 | if (!this.format.minified && raw != null) {
|
---|
211 | this.token(raw);
|
---|
212 | return;
|
---|
213 | }
|
---|
214 |
|
---|
215 | const val = _jsesc(node.value, Object.assign(this.format.jsescOption, this.format.jsonCompatibleStrings && {
|
---|
216 | json: true
|
---|
217 | }));
|
---|
218 |
|
---|
219 | return this.token(val);
|
---|
220 | }
|
---|
221 |
|
---|
222 | function BigIntLiteral(node) {
|
---|
223 | const raw = this.getPossibleRaw(node);
|
---|
224 |
|
---|
225 | if (!this.format.minified && raw != null) {
|
---|
226 | this.word(raw);
|
---|
227 | return;
|
---|
228 | }
|
---|
229 |
|
---|
230 | this.word(node.value + "n");
|
---|
231 | }
|
---|
232 |
|
---|
233 | function DecimalLiteral(node) {
|
---|
234 | const raw = this.getPossibleRaw(node);
|
---|
235 |
|
---|
236 | if (!this.format.minified && raw != null) {
|
---|
237 | this.word(raw);
|
---|
238 | return;
|
---|
239 | }
|
---|
240 |
|
---|
241 | this.word(node.value + "m");
|
---|
242 | }
|
---|
243 |
|
---|
244 | function PipelineTopicExpression(node) {
|
---|
245 | this.print(node.expression, node);
|
---|
246 | }
|
---|
247 |
|
---|
248 | function PipelineBareFunction(node) {
|
---|
249 | this.print(node.callee, node);
|
---|
250 | }
|
---|
251 |
|
---|
252 | function PipelinePrimaryTopicReference() {
|
---|
253 | this.token("#");
|
---|
254 | } |
---|