UNPKG

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