prettier 1.5.4 → 2.0.0.pre.rc1

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