prettier 1.2.2 → 1.4.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (81) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +64 -1
  3. data/CONTRIBUTING.md +2 -2
  4. data/README.md +22 -94
  5. data/node_modules/prettier/index.js +54 -54
  6. data/package.json +2 -3
  7. data/rubocop.yml +26 -0
  8. data/src/haml/embed.js +87 -0
  9. data/src/haml/nodes/comment.js +27 -0
  10. data/src/haml/nodes/doctype.js +34 -0
  11. data/src/haml/nodes/filter.js +16 -0
  12. data/src/haml/nodes/hamlComment.js +21 -0
  13. data/src/haml/nodes/plain.js +6 -0
  14. data/src/haml/nodes/root.js +8 -0
  15. data/src/haml/nodes/script.js +33 -0
  16. data/src/haml/nodes/silentScript.js +59 -0
  17. data/src/haml/nodes/tag.js +193 -0
  18. data/src/haml/parser.js +22 -0
  19. data/src/haml/parser.rb +138 -0
  20. data/src/haml/printer.js +28 -0
  21. data/src/parser/getLang.js +32 -0
  22. data/src/parser/getNetcat.js +50 -0
  23. data/src/parser/netcat.js +15 -0
  24. data/src/parser/parseSync.js +33 -0
  25. data/src/parser/requestParse.js +74 -0
  26. data/src/parser/server.rb +61 -0
  27. data/src/{ruby.js → plugin.js} +25 -4
  28. data/src/prettier.js +1 -0
  29. data/src/rbs/parser.js +39 -0
  30. data/src/rbs/parser.rb +88 -0
  31. data/src/rbs/printer.js +605 -0
  32. data/src/{embed.js → ruby/embed.js} +6 -2
  33. data/src/{nodes.js → ruby/nodes.js} +0 -0
  34. data/src/{nodes → ruby/nodes}/alias.js +1 -1
  35. data/src/{nodes → ruby/nodes}/aref.js +8 -1
  36. data/src/{nodes → ruby/nodes}/args.js +2 -2
  37. data/src/{nodes → ruby/nodes}/arrays.js +2 -3
  38. data/src/{nodes → ruby/nodes}/assign.js +7 -3
  39. data/src/ruby/nodes/blocks.js +90 -0
  40. data/src/{nodes → ruby/nodes}/calls.js +20 -47
  41. data/src/{nodes → ruby/nodes}/case.js +1 -1
  42. data/src/{nodes → ruby/nodes}/class.js +1 -1
  43. data/src/ruby/nodes/commands.js +131 -0
  44. data/src/{nodes → ruby/nodes}/conditionals.js +3 -3
  45. data/src/{nodes → ruby/nodes}/constants.js +2 -2
  46. data/src/{nodes → ruby/nodes}/flow.js +2 -2
  47. data/src/{nodes → ruby/nodes}/hashes.js +32 -10
  48. data/src/{nodes → ruby/nodes}/heredocs.js +2 -2
  49. data/src/ruby/nodes/hooks.js +34 -0
  50. data/src/{nodes → ruby/nodes}/ints.js +0 -0
  51. data/src/{nodes → ruby/nodes}/lambdas.js +2 -2
  52. data/src/{nodes → ruby/nodes}/loops.js +10 -7
  53. data/src/{nodes → ruby/nodes}/massign.js +8 -1
  54. data/src/{nodes → ruby/nodes}/methods.js +10 -9
  55. data/src/{nodes → ruby/nodes}/operators.js +2 -2
  56. data/src/{nodes → ruby/nodes}/params.js +31 -16
  57. data/src/{nodes → ruby/nodes}/patterns.js +17 -6
  58. data/src/{nodes → ruby/nodes}/regexp.js +2 -2
  59. data/src/{nodes → ruby/nodes}/rescue.js +34 -27
  60. data/src/{nodes → ruby/nodes}/return.js +21 -10
  61. data/src/{nodes → ruby/nodes}/statements.js +9 -9
  62. data/src/{nodes → ruby/nodes}/strings.js +28 -36
  63. data/src/{nodes → ruby/nodes}/super.js +2 -2
  64. data/src/{nodes → ruby/nodes}/undef.js +1 -1
  65. data/src/ruby/parser.js +39 -0
  66. data/src/{parser.rb → ruby/parser.rb} +498 -529
  67. data/src/{printer.js → ruby/printer.js} +1 -3
  68. data/src/{toProc.js → ruby/toProc.js} +4 -8
  69. data/src/utils.js +10 -93
  70. data/src/utils/containsAssignment.js +11 -0
  71. data/src/utils/getTrailingComma.js +5 -0
  72. data/src/utils/hasAncestor.js +17 -0
  73. data/src/utils/literal.js +7 -0
  74. data/src/utils/makeCall.js +14 -0
  75. data/src/utils/noIndent.js +11 -0
  76. data/src/utils/skipAssignIndent.js +10 -0
  77. metadata +71 -41
  78. data/src/nodes/blocks.js +0 -85
  79. data/src/nodes/commands.js +0 -91
  80. data/src/nodes/hooks.js +0 -44
  81. data/src/parser.js +0 -86
@@ -1,5 +1,11 @@
1
- const printer = require("./printer");
2
- const parser = require("./parser");
1
+ const rubyPrinter = require("./ruby/printer");
2
+ const rubyParser = require("./ruby/parser");
3
+
4
+ const rbsPrinter = require("./rbs/printer");
5
+ const rbsParser = require("./rbs/parser");
6
+
7
+ const hamlPrinter = require("./haml/printer");
8
+ const hamlParser = require("./haml/parser");
3
9
 
4
10
  /*
5
11
  * metadata mostly pulled from linguist and rubocop:
@@ -67,13 +73,28 @@ module.exports = {
67
73
  interpreters: ["jruby", "macruby", "rake", "rbx", "ruby"],
68
74
  linguistLanguageId: 326,
69
75
  vscodeLanguageIds: ["ruby"]
76
+ },
77
+ {
78
+ name: "RBS",
79
+ parsers: ["rbs"],
80
+ extensions: [".rbs"]
81
+ },
82
+ {
83
+ name: "HAML",
84
+ parsers: ["haml"],
85
+ extensions: [".haml"],
86
+ vscodeLanguageIds: ["haml"]
70
87
  }
71
88
  ],
72
89
  parsers: {
73
- ruby: parser
90
+ ruby: rubyParser,
91
+ rbs: rbsParser,
92
+ haml: hamlParser
74
93
  },
75
94
  printers: {
76
- ruby: printer
95
+ ruby: rubyPrinter,
96
+ rbs: rbsPrinter,
97
+ haml: hamlPrinter
77
98
  },
78
99
  options: {
79
100
  rubyArrayLiteral: {
@@ -1,6 +1,7 @@
1
1
  // If `RBPRETTIER` is set, then this is being run from the `Prettier::run` ruby
2
2
  // method. In that case, we need to pull `prettier` from the node_modules
3
3
  // directly, as it's been shipped with the gem.
4
+ /* istanbul ignore next */
4
5
  const source = process.env.RBPRETTIER ? "../node_modules/prettier" : "prettier";
5
6
 
6
7
  const prettier = require(source);
@@ -0,0 +1,39 @@
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);
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
+ };
@@ -0,0 +1,88 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ begin
4
+ require 'rbs'
5
+ rescue LoadError
6
+ # If we can't load the rbs gem, then we're going to provide a shim parser that
7
+ # will warn and bail out.
8
+ class Prettier::RBSParser
9
+ def self.parse(text)
10
+ warn(
11
+ 'The `rbs` gem could not be loaded. Please ensure you have it ' \
12
+ 'installed and that it is available in the gem path.'
13
+ )
14
+
15
+ false
16
+ end
17
+ end
18
+
19
+ return
20
+ end
21
+
22
+ # Monkey-patch this so that we can get the character positions.
23
+ class RBS::Location
24
+ def to_json(*args)
25
+ {
26
+ start: { line: start_line, column: start_column },
27
+ end: { line: end_line, column: end_column },
28
+ start_pos: start_pos,
29
+ end_pos: end_pos
30
+ }.to_json(*args)
31
+ end
32
+ end
33
+
34
+ # Monkey-patch this so that we get whether or not it needs to be escaped.
35
+ class RBS::Types::Function::Param
36
+ def to_json(*a)
37
+ escaped = name && /\A#{RBS::Parser::KEYWORDS_RE}\z/.match?(name)
38
+ { type: type, name: name, escaped: escaped }.to_json(*a)
39
+ end
40
+ end
41
+
42
+ # Monkey-patch this so that we get the name field in the serialized JSON, as
43
+ # well as information about whether or not we need to escape it.
44
+ class RBS::AST::Members::MethodDefinition
45
+ def to_json(*a)
46
+ {
47
+ member: :method_definition,
48
+ name: name,
49
+ kind: kind,
50
+ types: types,
51
+ annotations: annotations,
52
+ location: location,
53
+ comment: comment,
54
+ overload: overload
55
+ }.to_json(*a)
56
+ end
57
+ end
58
+
59
+ # Monkey-patch this so that we get the information we need about how to join the
60
+ # key-value pairs of the record.
61
+ class RBS::Types::Record
62
+ def to_json(*a)
63
+ fields_extra = {}
64
+
65
+ # Explicitly not using Enumerable#to_h here to support Ruby 2.5
66
+ fields.each do |key, type|
67
+ if key.is_a?(Symbol) && key.match?(/\A[A-Za-z_][A-Za-z_]*\z/) &&
68
+ !key.match?(RBS::Parser::KEYWORDS_RE)
69
+ fields_extra[key] = { type: type, joiner: :label }
70
+ else
71
+ fields_extra[key.inspect] = { type: type, joiner: :rocket }
72
+ end
73
+ end
74
+
75
+ { class: :record, fields: fields_extra, location: location }.to_json(*a)
76
+ end
77
+ end
78
+
79
+ # The main parser interface.
80
+ module Prettier
81
+ class RBSParser
82
+ def self.parse(text)
83
+ { declarations: RBS::Parser.parse_signature(text) }
84
+ rescue StandardError
85
+ false
86
+ end
87
+ end
88
+ end
@@ -0,0 +1,605 @@
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
+ };