prettier 1.6.1 → 2.0.0.pre.rc1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +316 -293
- data/CONTRIBUTING.md +6 -9
- data/LICENSE +1 -1
- data/README.md +11 -12
- data/dist/haml/embed.js +53 -0
- data/dist/haml/parser.js +31 -0
- data/{src → dist}/haml/parser.rb +0 -0
- data/dist/haml/printer.js +336 -0
- data/dist/parser/getInfo.js +17 -0
- data/{src → dist}/parser/netcat.js +1 -0
- data/dist/parser/parseSync.js +128 -0
- data/dist/parser/server.rb +140 -0
- data/dist/plugin.js +143 -0
- data/dist/prettier.js +15 -0
- data/dist/rbs/parser.js +34 -0
- data/{src → dist}/rbs/parser.rb +0 -0
- data/dist/rbs/printer.js +517 -0
- data/dist/ruby/embed.js +110 -0
- data/dist/ruby/nodes/alias.js +59 -0
- data/{src → dist}/ruby/nodes/aref.js +26 -35
- data/dist/ruby/nodes/args.js +165 -0
- data/dist/ruby/nodes/arrays.js +126 -0
- data/dist/ruby/nodes/assign.js +41 -0
- data/dist/ruby/nodes/blocks.js +68 -0
- data/dist/ruby/nodes/calls.js +220 -0
- data/dist/ruby/nodes/case.js +50 -0
- data/dist/ruby/nodes/class.js +54 -0
- data/dist/ruby/nodes/commands.js +124 -0
- data/dist/ruby/nodes/conditionals.js +242 -0
- data/dist/ruby/nodes/constants.js +38 -0
- data/dist/ruby/nodes/flow.js +66 -0
- data/dist/ruby/nodes/hashes.js +130 -0
- data/dist/ruby/nodes/heredocs.js +30 -0
- data/dist/ruby/nodes/hooks.js +35 -0
- data/dist/ruby/nodes/ints.js +27 -0
- data/dist/ruby/nodes/lambdas.js +69 -0
- data/dist/ruby/nodes/loops.js +73 -0
- data/dist/ruby/nodes/massign.js +73 -0
- data/dist/ruby/nodes/methods.js +70 -0
- data/dist/ruby/nodes/operators.js +70 -0
- data/dist/ruby/nodes/params.js +89 -0
- data/dist/ruby/nodes/patterns.js +109 -0
- data/dist/ruby/nodes/regexp.js +45 -0
- data/dist/ruby/nodes/rescue.js +82 -0
- data/dist/ruby/nodes/return.js +75 -0
- data/dist/ruby/nodes/statements.js +111 -0
- data/dist/ruby/nodes/strings.js +218 -0
- data/dist/ruby/nodes/super.js +30 -0
- data/dist/ruby/nodes/undef.js +26 -0
- data/dist/ruby/nodes.js +151 -0
- data/dist/ruby/parser.js +34 -0
- data/{src → dist}/ruby/parser.rb +1215 -252
- data/dist/ruby/printer.js +125 -0
- data/dist/ruby/toProc.js +93 -0
- data/dist/types/haml.js +4 -0
- data/dist/types/plugin.js +3 -0
- data/dist/types/rbs.js +4 -0
- data/dist/types/ruby.js +4 -0
- data/dist/types/utils.js +2 -0
- data/dist/types.js +30 -0
- data/dist/utils/containsAssignment.js +15 -0
- data/dist/utils/getTrailingComma.js +6 -0
- data/dist/utils/hasAncestor.js +15 -0
- data/{src → dist}/utils/inlineEnsureParens.js +16 -17
- data/dist/utils/isEmptyBodyStmt.js +10 -0
- data/dist/utils/isEmptyStmts.js +10 -0
- data/dist/utils/literal.js +8 -0
- data/dist/utils/literallineWithoutBreakParent.js +8 -0
- data/dist/utils/makeCall.js +13 -0
- data/dist/utils/noIndent.js +11 -0
- data/dist/utils/printEmptyCollection.js +44 -0
- data/dist/utils/skipAssignIndent.js +15 -0
- data/dist/utils.js +30 -0
- data/node_modules/prettier/bin-prettier.js +313 -190
- data/node_modules/prettier/doc.js +191 -323
- data/node_modules/prettier/index.js +2753 -3677
- data/node_modules/prettier/package.json +1 -1
- data/node_modules/prettier/parser-angular.js +13 -14
- data/node_modules/prettier/parser-babel.js +7 -7
- data/node_modules/prettier/parser-espree.js +7 -7
- data/node_modules/prettier/parser-flow.js +7 -7
- data/node_modules/prettier/parser-glimmer.js +1 -1
- data/node_modules/prettier/parser-graphql.js +1 -1
- data/node_modules/prettier/parser-html.js +17 -17
- data/node_modules/prettier/parser-markdown.js +9 -9
- data/node_modules/prettier/parser-meriyah.js +7 -7
- data/node_modules/prettier/parser-postcss.js +2 -2
- data/node_modules/prettier/parser-typescript.js +7 -7
- data/node_modules/prettier/parser-yaml.js +2 -2
- data/node_modules/prettier/third-party.js +143 -78
- data/package.json +26 -18
- metadata +74 -67
- data/src/haml/embed.js +0 -87
- data/src/haml/parser.js +0 -23
- data/src/haml/printer.js +0 -438
- data/src/parser/parseSync.js +0 -172
- data/src/parser/server.rb +0 -66
- data/src/plugin.js +0 -148
- data/src/prettier.js +0 -16
- data/src/rbs/parser.js +0 -37
- data/src/rbs/printer.js +0 -643
- data/src/ruby/embed.js +0 -142
- data/src/ruby/nodes/alias.js +0 -73
- data/src/ruby/nodes/args.js +0 -222
- data/src/ruby/nodes/arrays.js +0 -162
- data/src/ruby/nodes/assign.js +0 -47
- data/src/ruby/nodes/blocks.js +0 -90
- data/src/ruby/nodes/calls.js +0 -246
- data/src/ruby/nodes/case.js +0 -65
- data/src/ruby/nodes/class.js +0 -64
- data/src/ruby/nodes/commands.js +0 -131
- data/src/ruby/nodes/conditionals.js +0 -282
- data/src/ruby/nodes/constants.js +0 -43
- data/src/ruby/nodes/flow.js +0 -74
- data/src/ruby/nodes/hashes.js +0 -155
- data/src/ruby/nodes/heredocs.js +0 -36
- data/src/ruby/nodes/hooks.js +0 -34
- data/src/ruby/nodes/ints.js +0 -31
- data/src/ruby/nodes/lambdas.js +0 -76
- data/src/ruby/nodes/loops.js +0 -98
- data/src/ruby/nodes/massign.js +0 -98
- data/src/ruby/nodes/methods.js +0 -74
- data/src/ruby/nodes/operators.js +0 -83
- data/src/ruby/nodes/params.js +0 -106
- data/src/ruby/nodes/patterns.js +0 -157
- data/src/ruby/nodes/regexp.js +0 -56
- data/src/ruby/nodes/rescue.js +0 -101
- data/src/ruby/nodes/return.js +0 -94
- data/src/ruby/nodes/statements.js +0 -142
- data/src/ruby/nodes/strings.js +0 -272
- data/src/ruby/nodes/super.js +0 -35
- data/src/ruby/nodes/undef.js +0 -42
- data/src/ruby/nodes.js +0 -34
- data/src/ruby/parser.js +0 -37
- data/src/ruby/printer.js +0 -147
- data/src/ruby/toProc.js +0 -105
- data/src/utils/containsAssignment.js +0 -11
- data/src/utils/getTrailingComma.js +0 -5
- data/src/utils/hasAncestor.js +0 -17
- data/src/utils/isEmptyBodyStmt.js +0 -7
- data/src/utils/isEmptyStmts.js +0 -11
- data/src/utils/literal.js +0 -7
- data/src/utils/literallineWithoutBreakParent.js +0 -7
- data/src/utils/makeCall.js +0 -14
- data/src/utils/noIndent.js +0 -10
- data/src/utils/printEmptyCollection.js +0 -49
- data/src/utils/skipAssignIndent.js +0 -17
- data/src/utils.js +0 -13
data/src/rbs/printer.js
DELETED
@@ -1,643 +0,0 @@
|
|
1
|
-
const {
|
2
|
-
concat,
|
3
|
-
group,
|
4
|
-
hardline,
|
5
|
-
indent,
|
6
|
-
makeString,
|
7
|
-
join,
|
8
|
-
line,
|
9
|
-
softline
|
10
|
-
} = require("../prettier");
|
11
|
-
|
12
|
-
// For some lists of entities in the AST, the parser returns them as an unsorted
|
13
|
-
// object (presumably because Ruby hashes have implicit ordering). We do not
|
14
|
-
// have that in JavaScript, so here we sort each object by its position in the
|
15
|
-
// source string.
|
16
|
-
function getSortedKeys(object) {
|
17
|
-
return Object.keys(object).sort(
|
18
|
-
(left, right) =>
|
19
|
-
object[left].type.location.start_pos -
|
20
|
-
object[right].type.location.start_pos
|
21
|
-
);
|
22
|
-
}
|
23
|
-
|
24
|
-
// In some cases, we want to just defer to whatever was in the source.
|
25
|
-
function getSource(node, opts) {
|
26
|
-
return opts.originalText.slice(
|
27
|
-
node.location.start_pos,
|
28
|
-
node.location.end_pos
|
29
|
-
);
|
30
|
-
}
|
31
|
-
|
32
|
-
// This is the generic node print function, used to convert any node in the AST
|
33
|
-
// into its equivalent Doc representation.
|
34
|
-
function printNode(path, opts, print) {
|
35
|
-
const node = path.getValue();
|
36
|
-
let doc = null;
|
37
|
-
|
38
|
-
if (node.declarations) {
|
39
|
-
return printRoot();
|
40
|
-
}
|
41
|
-
|
42
|
-
/* istanbul ignore else */
|
43
|
-
if (node.declaration) {
|
44
|
-
switch (node.declaration) {
|
45
|
-
case "alias":
|
46
|
-
doc = printTypeAlias();
|
47
|
-
break;
|
48
|
-
case "class":
|
49
|
-
doc = printClass();
|
50
|
-
break;
|
51
|
-
case "constant":
|
52
|
-
case "global":
|
53
|
-
doc = printConstant();
|
54
|
-
break;
|
55
|
-
case "interface":
|
56
|
-
doc = printInterface();
|
57
|
-
break;
|
58
|
-
case "module":
|
59
|
-
doc = printModule();
|
60
|
-
break;
|
61
|
-
/* istanbul ignore next */
|
62
|
-
default:
|
63
|
-
throw new Error(`unknown declaration: ${node.declaration}`);
|
64
|
-
}
|
65
|
-
} else if (node.member) {
|
66
|
-
switch (node.member) {
|
67
|
-
case "alias":
|
68
|
-
doc = printAlias();
|
69
|
-
break;
|
70
|
-
case "attr_accessor":
|
71
|
-
case "attr_reader":
|
72
|
-
case "attr_writer":
|
73
|
-
doc = printAttr();
|
74
|
-
break;
|
75
|
-
case "class_variable":
|
76
|
-
case "instance_variable":
|
77
|
-
doc = printVariable();
|
78
|
-
break;
|
79
|
-
case "class_instance_variable":
|
80
|
-
doc = concat(["self.", printVariable()]);
|
81
|
-
break;
|
82
|
-
case "include":
|
83
|
-
case "extend":
|
84
|
-
case "prepend":
|
85
|
-
doc = printMixin();
|
86
|
-
break;
|
87
|
-
case "public":
|
88
|
-
case "private":
|
89
|
-
doc = node.member;
|
90
|
-
break;
|
91
|
-
case "method_definition":
|
92
|
-
doc = printMethodDefinition();
|
93
|
-
break;
|
94
|
-
/* istanbul ignore next */
|
95
|
-
default:
|
96
|
-
throw new Error(`unknown member: ${node.member}`);
|
97
|
-
}
|
98
|
-
} else {
|
99
|
-
const ast = JSON.stringify(node, null, 2);
|
100
|
-
throw new Error(`Unsupported node encountered:\n${ast}`);
|
101
|
-
}
|
102
|
-
|
103
|
-
// Certain nodes can't have annotations at all
|
104
|
-
if (node.annotations && node.annotations.length > 0) {
|
105
|
-
doc = concat([printAnnotations(), hardline, doc]);
|
106
|
-
}
|
107
|
-
|
108
|
-
if (node.comment) {
|
109
|
-
doc = concat([printComment(), hardline, doc]);
|
110
|
-
}
|
111
|
-
|
112
|
-
return doc;
|
113
|
-
|
114
|
-
// Prints out a string in the source, which looks like:
|
115
|
-
// 'foo'
|
116
|
-
function printString(node) {
|
117
|
-
// We're going to go straight to the source here, as if we don't then we're
|
118
|
-
// going to end up with the result of String#inspect, which does weird
|
119
|
-
// things to escape sequences.
|
120
|
-
const value = getSource(node, opts);
|
121
|
-
|
122
|
-
// Get the quote that was used in the source and the quote that we want to
|
123
|
-
// be using.
|
124
|
-
const originalQuote = value[0];
|
125
|
-
const preferredQuote = opts.rubySingleQuote ? "'" : '"';
|
126
|
-
|
127
|
-
// Determine if we're allowed to change the quote based on whether or not
|
128
|
-
// there is an escape sequence in the source string.
|
129
|
-
const quote = node.literal.includes("\\") ? originalQuote : preferredQuote;
|
130
|
-
|
131
|
-
return makeString(value.slice(1, -1), quote, false);
|
132
|
-
}
|
133
|
-
|
134
|
-
// Certain nodes are names with optional arguments attached, as in Array[A].
|
135
|
-
// We handle all of that printing centralized here.
|
136
|
-
function printNameAndArgs(path) {
|
137
|
-
const node = path.getValue();
|
138
|
-
|
139
|
-
if (node.args.length === 0) {
|
140
|
-
return node.name;
|
141
|
-
}
|
142
|
-
|
143
|
-
return group(
|
144
|
-
concat([node.name, "[", join(", ", path.map(printType, "args")), "]"])
|
145
|
-
);
|
146
|
-
}
|
147
|
-
|
148
|
-
// This is the big function that prints out any individual type, which can
|
149
|
-
// look like all kinds of things, listed in the case statement below.
|
150
|
-
function printType(path, { forceParens = false } = {}) {
|
151
|
-
const node = path.getValue();
|
152
|
-
|
153
|
-
switch (node.class) {
|
154
|
-
case "literal":
|
155
|
-
if (node.literal[0] === '"') {
|
156
|
-
return printString(node);
|
157
|
-
}
|
158
|
-
return node.literal;
|
159
|
-
case "optional":
|
160
|
-
return concat([
|
161
|
-
path.call(
|
162
|
-
(typePath) => printType(typePath, { forceParens: true }),
|
163
|
-
"type"
|
164
|
-
),
|
165
|
-
"?"
|
166
|
-
]);
|
167
|
-
case "tuple":
|
168
|
-
// If we don't have any sub types, we explicitly need the space in between
|
169
|
-
// the brackets to not confuse the parser.
|
170
|
-
if (node.types.length === 0) {
|
171
|
-
return "[ ]";
|
172
|
-
}
|
173
|
-
|
174
|
-
return group(
|
175
|
-
concat(["[", join(", ", path.map(printType, "types")), "]"])
|
176
|
-
);
|
177
|
-
case "union": {
|
178
|
-
const doc = group(
|
179
|
-
join(concat([line, "| "]), path.map(printType, "types"))
|
180
|
-
);
|
181
|
-
|
182
|
-
if (forceParens) {
|
183
|
-
return concat(["(", doc, ")"]);
|
184
|
-
}
|
185
|
-
|
186
|
-
return doc;
|
187
|
-
}
|
188
|
-
case "intersection": {
|
189
|
-
const doc = group(
|
190
|
-
join(
|
191
|
-
concat([line, "& "]),
|
192
|
-
path.map(
|
193
|
-
(typePath) => printType(typePath, { forceParens: true }),
|
194
|
-
"types"
|
195
|
-
)
|
196
|
-
)
|
197
|
-
);
|
198
|
-
|
199
|
-
if (forceParens) {
|
200
|
-
return concat(["(", doc, ")"]);
|
201
|
-
}
|
202
|
-
|
203
|
-
return doc;
|
204
|
-
}
|
205
|
-
case "class_singleton":
|
206
|
-
return concat(["singleton(", node.name, ")"]);
|
207
|
-
case "proc":
|
208
|
-
return concat(["^", printMethodSignature(path)]);
|
209
|
-
case "record": {
|
210
|
-
const parts = [];
|
211
|
-
|
212
|
-
getSortedKeys(node.fields).forEach((field) => {
|
213
|
-
const fieldParts = [];
|
214
|
-
|
215
|
-
if (node.fields[field].joiner === "rocket") {
|
216
|
-
fieldParts.push(`${field} => `);
|
217
|
-
} else {
|
218
|
-
fieldParts.push(`${field}: `);
|
219
|
-
}
|
220
|
-
|
221
|
-
fieldParts.push(path.call(printType, "fields", field, "type"));
|
222
|
-
parts.push(concat(fieldParts));
|
223
|
-
});
|
224
|
-
|
225
|
-
return group(
|
226
|
-
concat([
|
227
|
-
"{",
|
228
|
-
indent(concat([line, join(concat([",", line]), parts)])),
|
229
|
-
line,
|
230
|
-
"}"
|
231
|
-
])
|
232
|
-
);
|
233
|
-
}
|
234
|
-
case "class_instance":
|
235
|
-
case "interface":
|
236
|
-
return printNameAndArgs(path);
|
237
|
-
case "alias":
|
238
|
-
case "variable":
|
239
|
-
return node.name;
|
240
|
-
case "bool":
|
241
|
-
case "bot":
|
242
|
-
case "class":
|
243
|
-
case "instance":
|
244
|
-
case "nil":
|
245
|
-
case "self":
|
246
|
-
case "top":
|
247
|
-
case "untyped":
|
248
|
-
case "void":
|
249
|
-
return node.class;
|
250
|
-
/* istanbul ignore next */
|
251
|
-
default:
|
252
|
-
throw new Error(`unknown type: ${node.class}`);
|
253
|
-
}
|
254
|
-
}
|
255
|
-
|
256
|
-
// Prints out the root of the tree, which includes zero or more declarations.
|
257
|
-
function printRoot() {
|
258
|
-
return concat([
|
259
|
-
join(concat([hardline, hardline]), path.map(print, "declarations")),
|
260
|
-
hardline
|
261
|
-
]);
|
262
|
-
}
|
263
|
-
|
264
|
-
// Prints out the members of a class, module, or interface.
|
265
|
-
function printMembers() {
|
266
|
-
let lastLine = null;
|
267
|
-
const docs = [];
|
268
|
-
|
269
|
-
path.each((memberPath) => {
|
270
|
-
const memberNode = memberPath.getValue();
|
271
|
-
|
272
|
-
if (lastLine !== null && memberNode.location.start.line - lastLine >= 2) {
|
273
|
-
docs.push(concat([hardline, hardline]));
|
274
|
-
} else {
|
275
|
-
docs.push(hardline);
|
276
|
-
}
|
277
|
-
|
278
|
-
docs.push(print(memberPath));
|
279
|
-
lastLine = memberNode.location.end.line;
|
280
|
-
}, "members");
|
281
|
-
|
282
|
-
return concat(docs);
|
283
|
-
}
|
284
|
-
|
285
|
-
// Prints out a type alias, which is a declaration that looks like:
|
286
|
-
// type foo = String
|
287
|
-
function printTypeAlias() {
|
288
|
-
return group(
|
289
|
-
concat([
|
290
|
-
"type ",
|
291
|
-
node.name,
|
292
|
-
" =",
|
293
|
-
indent(group(concat([line, path.call(printType, "type")])))
|
294
|
-
])
|
295
|
-
);
|
296
|
-
}
|
297
|
-
|
298
|
-
// Prints out the name of a class, interface, or module declaration.
|
299
|
-
// Additionally loops through each type parameter if there are any and print
|
300
|
-
// them out joined by commas. Checks for validation and variance.
|
301
|
-
function printNameAndTypeParams() {
|
302
|
-
if (node.type_params.params.length === 0) {
|
303
|
-
return node.name;
|
304
|
-
}
|
305
|
-
|
306
|
-
const docs = path.map(
|
307
|
-
(paramPath) => {
|
308
|
-
const node = paramPath.getValue();
|
309
|
-
const parts = [];
|
310
|
-
|
311
|
-
if (node.skip_validation) {
|
312
|
-
parts.push("unchecked");
|
313
|
-
}
|
314
|
-
|
315
|
-
if (node.variance === "covariant") {
|
316
|
-
parts.push("out");
|
317
|
-
} else if (node.variance === "contravariant") {
|
318
|
-
parts.push("in");
|
319
|
-
}
|
320
|
-
|
321
|
-
return join(" ", parts.concat(node.name));
|
322
|
-
},
|
323
|
-
"type_params",
|
324
|
-
"params"
|
325
|
-
);
|
326
|
-
|
327
|
-
return concat([node.name, "[", join(", ", docs), "]"]);
|
328
|
-
}
|
329
|
-
|
330
|
-
// Prints out a class declarations, which looks like:
|
331
|
-
// class Foo end
|
332
|
-
function printClass() {
|
333
|
-
const parts = ["class ", printNameAndTypeParams()];
|
334
|
-
|
335
|
-
if (node.super_class) {
|
336
|
-
parts.push(" < ", path.call(printNameAndArgs, "super_class"));
|
337
|
-
}
|
338
|
-
|
339
|
-
parts.push(indent(printMembers()), hardline, "end");
|
340
|
-
|
341
|
-
return group(concat(parts));
|
342
|
-
}
|
343
|
-
|
344
|
-
// Prints out a constant or a global declaration, which looks like:
|
345
|
-
// Foo: String
|
346
|
-
// $foo: String
|
347
|
-
function printConstant() {
|
348
|
-
return group(concat([node.name, ": ", path.call(printType, "type")]));
|
349
|
-
}
|
350
|
-
|
351
|
-
// Prints out an interface declaration, which looks like:
|
352
|
-
// interface _Foo end
|
353
|
-
function printInterface() {
|
354
|
-
return group(
|
355
|
-
concat([
|
356
|
-
"interface ",
|
357
|
-
printNameAndTypeParams(),
|
358
|
-
indent(printMembers()),
|
359
|
-
hardline,
|
360
|
-
"end"
|
361
|
-
])
|
362
|
-
);
|
363
|
-
}
|
364
|
-
|
365
|
-
// Prints out a module declaration, which looks like:
|
366
|
-
// module Foo end
|
367
|
-
function printModule() {
|
368
|
-
const parts = ["module ", printNameAndTypeParams()];
|
369
|
-
|
370
|
-
if (node.self_types.length > 0) {
|
371
|
-
parts.push(" : ", join(", ", path.map(printNameAndArgs, "self_types")));
|
372
|
-
}
|
373
|
-
|
374
|
-
parts.push(indent(printMembers()), hardline, "end");
|
375
|
-
|
376
|
-
return group(concat(parts));
|
377
|
-
}
|
378
|
-
|
379
|
-
// Prints out an alias within a declaration, which looks like:
|
380
|
-
// alias foo bar
|
381
|
-
// alias self.foo self.bar
|
382
|
-
function printAlias() {
|
383
|
-
if (node.kind === "singleton") {
|
384
|
-
return concat(["alias self.", node.new_name, " self.", node.old_name]);
|
385
|
-
}
|
386
|
-
|
387
|
-
return concat(["alias ", node.new_name, " ", node.old_name]);
|
388
|
-
}
|
389
|
-
|
390
|
-
// Prints out an attr_* meta method, which looks like:
|
391
|
-
// attr_accessor foo
|
392
|
-
// attr_reader self.foo()
|
393
|
-
// attr_writer self.foo(@bar): String
|
394
|
-
function printAttr() {
|
395
|
-
const parts = [node.member, " "];
|
396
|
-
|
397
|
-
if (node.kind === "singleton") {
|
398
|
-
parts.push("self.");
|
399
|
-
}
|
400
|
-
|
401
|
-
parts.push(node.name);
|
402
|
-
|
403
|
-
if (node.ivar_name === false) {
|
404
|
-
parts.push("()");
|
405
|
-
} else if (node.ivar_name) {
|
406
|
-
parts.push("(", node.ivar_name, ")");
|
407
|
-
}
|
408
|
-
|
409
|
-
parts.push(": ", path.call(printType, "type"));
|
410
|
-
|
411
|
-
return group(concat(parts));
|
412
|
-
}
|
413
|
-
|
414
|
-
// Prints out a variable member, which looks like:
|
415
|
-
// @foo: String
|
416
|
-
// self.@foo: String
|
417
|
-
// @@foo: String
|
418
|
-
function printVariable() {
|
419
|
-
return group(concat([node.name, ": ", path.call(printType, "type")]));
|
420
|
-
}
|
421
|
-
|
422
|
-
// Prints out a mixin, which looks like:
|
423
|
-
// include Foo
|
424
|
-
// prepend Foo
|
425
|
-
// extend Foo
|
426
|
-
function printMixin() {
|
427
|
-
return group(concat([node.member, " ", printNameAndArgs(path)]));
|
428
|
-
}
|
429
|
-
|
430
|
-
// Returns an array of printed parameters so that the calling function can
|
431
|
-
// join them together in whatever way.
|
432
|
-
function printMethodParams(path) {
|
433
|
-
const node = path.getValue();
|
434
|
-
let parts = [];
|
435
|
-
|
436
|
-
// required positionals, as in (A)
|
437
|
-
parts = parts.concat(path.map(printMethodParam, "required_positionals"));
|
438
|
-
|
439
|
-
// optional positionals, as in (?A)
|
440
|
-
parts = parts.concat(
|
441
|
-
path.map(
|
442
|
-
(paramPath) => concat(["?", printMethodParam(paramPath)]),
|
443
|
-
"optional_positionals"
|
444
|
-
)
|
445
|
-
);
|
446
|
-
|
447
|
-
// rest positional, as in (*A)
|
448
|
-
if (node.rest_positionals) {
|
449
|
-
parts.push(
|
450
|
-
concat(["*", path.call(printMethodParam, "rest_positionals")])
|
451
|
-
);
|
452
|
-
}
|
453
|
-
|
454
|
-
// trailing positionals are required positionals after a rest
|
455
|
-
parts = parts.concat(path.map(printMethodParam, "trailing_positionals"));
|
456
|
-
|
457
|
-
// required keywords, as in (a: A)
|
458
|
-
getSortedKeys(node.required_keywords).forEach((name) => {
|
459
|
-
parts.push(
|
460
|
-
concat([
|
461
|
-
name,
|
462
|
-
": ",
|
463
|
-
path.call(printMethodParam, "required_keywords", name)
|
464
|
-
])
|
465
|
-
);
|
466
|
-
});
|
467
|
-
|
468
|
-
// optional keywords, as in (?a: A)
|
469
|
-
getSortedKeys(node.optional_keywords).forEach((name) => {
|
470
|
-
parts.push(
|
471
|
-
concat([
|
472
|
-
"?",
|
473
|
-
name,
|
474
|
-
": ",
|
475
|
-
path.call(printMethodParam, "optional_keywords", name)
|
476
|
-
])
|
477
|
-
);
|
478
|
-
});
|
479
|
-
|
480
|
-
// rest keyword, as in (**A)
|
481
|
-
if (node.rest_keywords) {
|
482
|
-
parts.push(concat(["**", path.call(printMethodParam, "rest_keywords")]));
|
483
|
-
}
|
484
|
-
|
485
|
-
return parts;
|
486
|
-
|
487
|
-
// Prints out a method parameter at a given path. Handles printing out the
|
488
|
-
// name if there is one (and whether or not it's escaped).
|
489
|
-
function printMethodParam(path) {
|
490
|
-
const node = path.getValue();
|
491
|
-
const parts = [path.call(printType, "type")];
|
492
|
-
|
493
|
-
if (node.name) {
|
494
|
-
parts.push(" ");
|
495
|
-
|
496
|
-
if (node.escaped) {
|
497
|
-
parts.push("`", node.name, "`");
|
498
|
-
} else {
|
499
|
-
parts.push(node.name);
|
500
|
-
}
|
501
|
-
}
|
502
|
-
|
503
|
-
return concat(parts);
|
504
|
-
}
|
505
|
-
}
|
506
|
-
|
507
|
-
// Prints out a specific method signature, which looks like:
|
508
|
-
// (T t) -> void
|
509
|
-
function printMethodSignature(path) {
|
510
|
-
const node = path.getValue();
|
511
|
-
const parts = [];
|
512
|
-
|
513
|
-
// We won't have a type_params key if we're printing a block
|
514
|
-
if (node.type_params && node.type_params.length > 0) {
|
515
|
-
parts.push("[", join(", ", node.type_params), "] ");
|
516
|
-
}
|
517
|
-
|
518
|
-
let params = path.call(printMethodParams, "type");
|
519
|
-
|
520
|
-
if (params.length > 0) {
|
521
|
-
parts.push(
|
522
|
-
"(",
|
523
|
-
indent(concat([softline, join(concat([",", line]), params)])),
|
524
|
-
softline,
|
525
|
-
") "
|
526
|
-
);
|
527
|
-
}
|
528
|
-
|
529
|
-
if (node.block) {
|
530
|
-
if (!node.block.required) {
|
531
|
-
parts.push("?");
|
532
|
-
}
|
533
|
-
|
534
|
-
parts.push(
|
535
|
-
"{",
|
536
|
-
indent(concat([line, path.call(printMethodSignature, "block")])),
|
537
|
-
line,
|
538
|
-
"} "
|
539
|
-
);
|
540
|
-
}
|
541
|
-
|
542
|
-
parts.push(
|
543
|
-
"-> ",
|
544
|
-
path.call(
|
545
|
-
(typePath) => printType(typePath, { forceParens: true }),
|
546
|
-
"type",
|
547
|
-
"return_type"
|
548
|
-
)
|
549
|
-
);
|
550
|
-
|
551
|
-
return group(concat(parts));
|
552
|
-
}
|
553
|
-
|
554
|
-
// Prints out a method definition, which looks like:
|
555
|
-
// def t: (T t) -> void
|
556
|
-
function printMethodDefinition() {
|
557
|
-
let typeDocs = path.map(printMethodSignature, "types");
|
558
|
-
|
559
|
-
if (node.overload) {
|
560
|
-
typeDocs.push("...");
|
561
|
-
}
|
562
|
-
|
563
|
-
if (typeDocs.length === 1) {
|
564
|
-
typeDocs = concat([" ", typeDocs[0]]);
|
565
|
-
} else {
|
566
|
-
typeDocs = indent(
|
567
|
-
group(concat([line, join(concat([line, "| "]), typeDocs)]))
|
568
|
-
);
|
569
|
-
}
|
570
|
-
|
571
|
-
const parts = ["def "];
|
572
|
-
|
573
|
-
if (node.kind === "singleton") {
|
574
|
-
parts.push("self.");
|
575
|
-
} else if (node.kind === "singleton_instance") {
|
576
|
-
parts.push("self?.");
|
577
|
-
}
|
578
|
-
|
579
|
-
const escaped = isMethodNameEscaped();
|
580
|
-
parts.push(escaped ? `\`${node.name}\`` : node.name, ":", typeDocs);
|
581
|
-
|
582
|
-
return group(concat(parts));
|
583
|
-
|
584
|
-
// Determine if a method name is escaped in the original source.
|
585
|
-
function isMethodNameEscaped() {
|
586
|
-
const pos = node.location.start_pos + 4;
|
587
|
-
const name = opts.originalText.slice(pos, pos + 2).trimStart();
|
588
|
-
|
589
|
-
return name[0] === "`" && name[1] !== ":";
|
590
|
-
}
|
591
|
-
}
|
592
|
-
|
593
|
-
// An annotation can be attached to most kinds of nodes, and should be printed
|
594
|
-
// using %a{}.
|
595
|
-
function printAnnotations() {
|
596
|
-
return join(hardline, path.map(printAnnotation, "annotations"));
|
597
|
-
|
598
|
-
function printAnnotation(path) {
|
599
|
-
const node = path.getValue();
|
600
|
-
|
601
|
-
// If there are already braces inside the annotation, then we're just
|
602
|
-
// going to print out the original string to avoid having to escape
|
603
|
-
// anything.
|
604
|
-
if (/[{}]/.test(node.string)) {
|
605
|
-
return getSource(node, opts);
|
606
|
-
}
|
607
|
-
|
608
|
-
return concat(["%a{", node.string, "}"]);
|
609
|
-
}
|
610
|
-
}
|
611
|
-
|
612
|
-
// Comments come in as one whole string, so here we split it up into multiple
|
613
|
-
// lines and then prefix it with the pound sign.
|
614
|
-
function printComment() {
|
615
|
-
const lines = node.comment.string.slice(0, -1).split("\n");
|
616
|
-
|
617
|
-
return join(
|
618
|
-
hardline,
|
619
|
-
lines.map((segment) => `# ${segment}`)
|
620
|
-
);
|
621
|
-
}
|
622
|
-
}
|
623
|
-
|
624
|
-
// This is an escape-hatch to ignore nodes in the tree. If you have a comment
|
625
|
-
// that includes this pattern, then the entire node will be ignored and just the
|
626
|
-
// original source will be printed out.
|
627
|
-
function hasPrettierIgnore(path) {
|
628
|
-
const node = path.getValue();
|
629
|
-
|
630
|
-
return node.comment && node.comment.string.includes("prettier-ignore");
|
631
|
-
}
|
632
|
-
|
633
|
-
// This function handles adding the format pragma to a source string. This is an
|
634
|
-
// optional workflow for incremental adoption.
|
635
|
-
function insertPragma(text) {
|
636
|
-
return `# @format\n${text}`;
|
637
|
-
}
|
638
|
-
|
639
|
-
module.exports = {
|
640
|
-
print: printNode,
|
641
|
-
hasPrettierIgnore,
|
642
|
-
insertPragma
|
643
|
-
};
|