prettier 1.2.3 → 1.5.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (58) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +349 -358
  3. data/README.md +21 -93
  4. data/node_modules/prettier/index.js +54 -54
  5. data/package.json +1 -2
  6. data/rubocop.yml +26 -0
  7. data/src/haml/embed.js +87 -0
  8. data/src/haml/nodes/comment.js +27 -0
  9. data/src/haml/nodes/doctype.js +34 -0
  10. data/src/haml/nodes/filter.js +16 -0
  11. data/src/haml/nodes/hamlComment.js +21 -0
  12. data/src/haml/nodes/plain.js +6 -0
  13. data/src/haml/nodes/root.js +8 -0
  14. data/src/haml/nodes/script.js +33 -0
  15. data/src/haml/nodes/silentScript.js +59 -0
  16. data/src/haml/nodes/tag.js +193 -0
  17. data/src/haml/parser.js +22 -0
  18. data/src/haml/parser.rb +138 -0
  19. data/src/haml/printer.js +28 -0
  20. data/src/parser/getLang.js +32 -0
  21. data/src/parser/getNetcat.js +50 -0
  22. data/src/parser/netcat.js +15 -0
  23. data/src/parser/parseSync.js +33 -0
  24. data/src/parser/requestParse.js +74 -0
  25. data/src/parser/server.rb +61 -0
  26. data/src/plugin.js +26 -4
  27. data/src/prettier.js +1 -0
  28. data/src/rbs/parser.js +39 -0
  29. data/src/rbs/parser.rb +94 -0
  30. data/src/rbs/printer.js +605 -0
  31. data/src/ruby/embed.js +58 -8
  32. data/src/ruby/nodes/args.js +20 -6
  33. data/src/ruby/nodes/blocks.js +64 -59
  34. data/src/ruby/nodes/calls.js +12 -43
  35. data/src/ruby/nodes/class.js +17 -27
  36. data/src/ruby/nodes/commands.js +7 -2
  37. data/src/ruby/nodes/conditionals.js +1 -1
  38. data/src/ruby/nodes/hashes.js +28 -14
  39. data/src/ruby/nodes/hooks.js +9 -19
  40. data/src/ruby/nodes/loops.js +4 -10
  41. data/src/ruby/nodes/methods.js +8 -17
  42. data/src/ruby/nodes/params.js +22 -14
  43. data/src/ruby/nodes/patterns.js +9 -5
  44. data/src/ruby/nodes/rescue.js +32 -25
  45. data/src/ruby/nodes/return.js +0 -4
  46. data/src/ruby/nodes/statements.js +11 -13
  47. data/src/ruby/nodes/strings.js +27 -35
  48. data/src/ruby/parser.js +2 -49
  49. data/src/ruby/parser.rb +256 -232
  50. data/src/ruby/printer.js +0 -2
  51. data/src/ruby/toProc.js +4 -8
  52. data/src/utils.js +1 -0
  53. data/src/utils/isEmptyBodyStmt.js +7 -0
  54. data/src/utils/isEmptyStmts.js +9 -5
  55. data/src/utils/makeCall.js +3 -0
  56. data/src/utils/noIndent.js +1 -0
  57. data/src/utils/printEmptyCollection.js +9 -2
  58. metadata +26 -2
@@ -4,6 +4,7 @@ const {
4
4
  hardline,
5
5
  indent,
6
6
  literalline,
7
+ removeLines,
7
8
  softline,
8
9
  join
9
10
  } = require("../../prettier");
@@ -31,17 +32,10 @@ function isSingleQuotable(node) {
31
32
 
32
33
  const quotePattern = new RegExp("\\\\([\\s\\S])|(['\"])", "g");
33
34
 
34
- function normalizeQuotes(content, enclosingQuote, originalQuote) {
35
- const replaceOther = originalQuote === '"';
36
- const otherQuote = enclosingQuote === '"' ? "'" : '"';
37
-
35
+ function normalizeQuotes(content, enclosingQuote) {
38
36
  // Escape and unescape single and double quotes as needed to be able to
39
37
  // enclose `content` with `enclosingQuote`.
40
38
  return content.replace(quotePattern, (match, escaped, quote) => {
41
- if (replaceOther && escaped === otherQuote) {
42
- return escaped;
43
- }
44
-
45
39
  if (quote === enclosingQuote) {
46
40
  return `\\${quote}`;
47
41
  }
@@ -97,12 +91,34 @@ function printDynaSymbol(path, opts, print) {
97
91
  return concat([":", quote].concat(path.map(print, "body")).concat(quote));
98
92
  }
99
93
 
94
+ function printStringConcat(path, opts, print) {
95
+ const [leftDoc, rightDoc] = path.map(print, "body");
96
+
97
+ return group(concat([leftDoc, " \\", indent(concat([hardline, rightDoc]))]));
98
+ }
99
+
100
100
  // Prints out an interpolated variable in the string by converting it into an
101
101
  // embedded expression.
102
102
  function printStringDVar(path, opts, print) {
103
103
  return concat(["#{", path.call(print, "body", 0), "}"]);
104
104
  }
105
105
 
106
+ function printStringEmbExpr(path, opts, print) {
107
+ const node = path.getValue();
108
+ const parts = path.call(print, "body", 0);
109
+
110
+ // If the contents of this embedded expression were originally on the same
111
+ // line in the source, then we're going to leave them in place and assume
112
+ // that's the way the developer wanted this expression represented.
113
+ if (node.sl === node.el) {
114
+ return concat(["#{", removeLines(parts), "}"]);
115
+ }
116
+
117
+ return group(
118
+ concat(["#{", indent(concat([softline, parts])), concat([softline, "}"])])
119
+ );
120
+ }
121
+
106
122
  // Prints out a literal string. This function does its best to respect the
107
123
  // wishes of the user with regards to single versus double quotes, but if the
108
124
  // string contains any escape expressions then it will just keep the original
@@ -131,10 +147,7 @@ function printStringLiteral(path, { rubySingleQuote }, print) {
131
147
  }
132
148
 
133
149
  // In this case, the part of the string is just regular string content
134
- return join(
135
- literalline,
136
- normalizeQuotes(part.body, quote, node.quote).split("\n")
137
- );
150
+ return join(literalline, normalizeQuotes(part.body, quote).split("\n"));
138
151
  });
139
152
 
140
153
  return concat([quote].concat(parts).concat(getClosingQuote(quote)));
@@ -155,30 +168,9 @@ function printXStringLiteral(path, opts, print) {
155
168
  module.exports = {
156
169
  "@CHAR": printChar,
157
170
  dyna_symbol: printDynaSymbol,
158
- string_concat: (path, opts, print) =>
159
- group(
160
- concat([
161
- path.call(print, "body", 0),
162
- " \\",
163
- indent(concat([hardline, path.call(print, "body", 1)]))
164
- ])
165
- ),
171
+ string_concat: printStringConcat,
166
172
  string_dvar: printStringDVar,
167
- string_embexpr: (path, opts, print) => {
168
- const parts = path.call(print, "body", 0);
169
-
170
- // If the interpolated expression is inside of a heredoc or an xstring
171
- // literal (a string that gets sent to the command line) then we don't want
172
- // to automatically indent, as this can lead to some very odd looking
173
- // expressions
174
- if (["heredoc", "xstring_literal"].includes(path.getParentNode().type)) {
175
- return concat(["#{", parts, "}"]);
176
- }
177
-
178
- return group(
179
- concat(["#{", indent(concat([softline, parts])), concat([softline, "}"])])
180
- );
181
- },
173
+ string_embexpr: printStringEmbExpr,
182
174
  string_literal: printStringLiteral,
183
175
  symbol_literal: printSymbolLiteral,
184
176
  xstring_literal: printXStringLiteral
@@ -1,58 +1,11 @@
1
- const { spawnSync } = require("child_process");
2
- const path = require("path");
3
-
4
- // In order to properly parse ruby code, we need to tell the ruby process to
5
- // parse using UTF-8. Unfortunately, the way that you accomplish this looks
6
- // differently depending on your platform.
7
- const LANG = (() => {
8
- const { env, platform } = process;
9
- const envValue = env.LC_ALL || env.LC_CTYPE || env.LANG;
10
-
11
- // If an env var is set for the locale that already includes UTF-8 in the
12
- // name, then assume we can go with that.
13
- if (envValue && envValue.includes("UTF-8")) {
14
- return envValue;
15
- }
16
-
17
- // Otherwise, we're going to guess which encoding to use based on the system.
18
- // This is probably not the best approach in the world, as you could be on
19
- // linux and not have C.UTF-8, but in that case you're probably passing an env
20
- // var for it. This object below represents all of the possible values of
21
- // process.platform per:
22
- // https://nodejs.org/api/process.html#process_process_platform
23
- return {
24
- aix: "C.UTF-8",
25
- darwin: "en_US.UTF-8",
26
- freebsd: "C.UTF-8",
27
- linux: "C.UTF-8",
28
- openbsd: "C.UTF-8",
29
- sunos: "C.UTF-8",
30
- win32: ".UTF-8"
31
- }[platform];
32
- })();
1
+ const parseSync = require("../parser/parseSync");
33
2
 
34
3
  // This function is responsible for taking an input string of text and returning
35
4
  // to prettier a JavaScript object that is the equivalent AST that represents
36
5
  // the code stored in that string. We accomplish this by spawning a new Ruby
37
6
  // process of parser.rb and reading JSON off STDOUT.
38
7
  function parse(text, _parsers, _opts) {
39
- const child = spawnSync(
40
- "ruby",
41
- ["--disable-gems", path.join(__dirname, "./parser.rb")],
42
- {
43
- env: Object.assign({}, process.env, { LANG }),
44
- input: text,
45
- maxBuffer: 10 * 1024 * 1024 // 10MB
46
- }
47
- );
48
-
49
- const error = child.stderr.toString();
50
- if (error) {
51
- throw new Error(error);
52
- }
53
-
54
- const response = child.stdout.toString();
55
- return JSON.parse(response);
8
+ return parseSync("ruby", text);
56
9
  }
57
10
 
58
11
  const pragmaPattern = /#\s*@(prettier|format)/;
@@ -14,14 +14,19 @@ if (RUBY_MAJOR < 2) || ((RUBY_MAJOR == 2) && (RUBY_MINOR < 5))
14
14
  end
15
15
 
16
16
  require 'delegate'
17
- require 'json' unless defined?(JSON)
17
+ require 'json'
18
18
  require 'ripper'
19
19
 
20
- module Prettier; end
20
+ module Prettier
21
+ end
21
22
 
22
23
  class Prettier::Parser < Ripper
23
24
  attr_reader :source, :lines, :scanner_events, :line_counts
24
25
 
26
+ # This is an attr_accessor so Stmts objects can grab comments out of this
27
+ # array and attach them to themselves.
28
+ attr_accessor :comments
29
+
25
30
  def initialize(source, *args)
26
31
  super(source, *args)
27
32
 
@@ -40,6 +45,13 @@ class Prettier::Parser < Ripper
40
45
  @source.lines.each { |line| @line_counts << @line_counts.last + line.size }
41
46
  end
42
47
 
48
+ def self.parse(source)
49
+ builder = new(source)
50
+
51
+ response = builder.parse
52
+ response unless builder.error?
53
+ end
54
+
43
55
  private
44
56
 
45
57
  # This represents the current place in the source string that we've gotten to
@@ -93,8 +105,8 @@ class Prettier::Parser < Ripper
93
105
  node = {
94
106
  type: :"@#{event}",
95
107
  body: value,
96
- start: lineno,
97
- end: lineno,
108
+ sl: lineno,
109
+ el: lineno,
98
110
  sc: char_pos,
99
111
  ec: ec
100
112
  }
@@ -118,8 +130,9 @@ class Prettier::Parser < Ripper
118
130
  @comments << {
119
131
  type: :@comment,
120
132
  value: value[1..-1].chomp.force_encoding('UTF-8'),
121
- start: lineno,
122
- end: lineno,
133
+ inline: value.strip != lines[lineno - 1],
134
+ sl: lineno,
135
+ el: lineno,
123
136
  sc: char_pos,
124
137
  ec: char_pos + value.length - 1
125
138
  }
@@ -138,8 +151,8 @@ class Prettier::Parser < Ripper
138
151
  {
139
152
  type: :ignored_nl,
140
153
  body: nil,
141
- start: lineno,
142
- end: lineno,
154
+ sl: lineno,
155
+ el: lineno,
143
156
  sc: char_pos,
144
157
  ec: char_pos
145
158
  }
@@ -191,7 +204,7 @@ class Prettier::Parser < Ripper
191
204
  find_scanner_event(:@kw, 'BEGIN').merge!(
192
205
  type: :BEGIN,
193
206
  body: [beging, stmts],
194
- end: ending[:end],
207
+ el: ending[:el],
195
208
  ec: ending[:ec]
196
209
  )
197
210
  end
@@ -215,7 +228,7 @@ class Prettier::Parser < Ripper
215
228
  find_scanner_event(:@kw, 'END').merge!(
216
229
  type: :END,
217
230
  body: [beging, stmts],
218
- end: ending[:end],
231
+ el: ending[:el],
219
232
  ec: ending[:ec]
220
233
  )
221
234
  end
@@ -234,9 +247,9 @@ class Prettier::Parser < Ripper
234
247
  {
235
248
  type: :alias,
236
249
  body: [left, right],
237
- start: beging[:start],
250
+ sl: beging[:sl],
238
251
  sc: beging[:sc],
239
- end: ending[:end],
252
+ el: ending[:el],
240
253
  ec: ending[:ec]
241
254
  }
242
255
  end
@@ -262,9 +275,9 @@ class Prettier::Parser < Ripper
262
275
  {
263
276
  type: :aref,
264
277
  body: [collection, index],
265
- start: collection[:start],
278
+ sl: collection[:sl],
266
279
  sc: collection[:sc],
267
- end: ending[:end],
280
+ el: ending[:el],
268
281
  ec: ending[:ec]
269
282
  }
270
283
  end
@@ -278,9 +291,9 @@ class Prettier::Parser < Ripper
278
291
  {
279
292
  type: :aref_field,
280
293
  body: [collection, index],
281
- start: collection[:start],
294
+ sl: collection[:sl],
282
295
  sc: collection[:sc],
283
- end: ending[:end],
296
+ el: ending[:el],
284
297
  ec: ending[:ec]
285
298
  }
286
299
  end
@@ -292,9 +305,9 @@ class Prettier::Parser < Ripper
292
305
  {
293
306
  type: :args,
294
307
  body: [],
295
- start: lineno,
308
+ sl: lineno,
296
309
  sc: char_pos,
297
- end: lineno,
310
+ el: lineno,
298
311
  ec: char_pos
299
312
  }
300
313
  end
@@ -307,7 +320,7 @@ class Prettier::Parser < Ripper
307
320
  if args[:body].empty?
308
321
  arg.merge(type: :args, body: [arg])
309
322
  else
310
- args.merge!(body: args[:body] << arg, end: arg[:end], ec: arg[:ec])
323
+ args.merge!(body: args[:body] << arg, el: arg[:el], ec: arg[:ec])
311
324
  end
312
325
  end
313
326
 
@@ -320,7 +333,7 @@ class Prettier::Parser < Ripper
320
333
  args.merge(
321
334
  type: :args_add_block,
322
335
  body: [args, block],
323
- end: ending[:end],
336
+ el: ending[:el],
324
337
  ec: ending[:ec]
325
338
  )
326
339
  end
@@ -335,9 +348,9 @@ class Prettier::Parser < Ripper
335
348
  {
336
349
  type: :args_add_star,
337
350
  body: [args, part],
338
- start: beging[:start],
351
+ sl: beging[:sl],
339
352
  sc: beging[:sc],
340
- end: ending[:end],
353
+ el: ending[:el],
341
354
  ec: ending[:ec]
342
355
  }
343
356
  end
@@ -357,14 +370,14 @@ class Prettier::Parser < Ripper
357
370
  # If the arguments exceed the ending of the parentheses, then we know we
358
371
  # have a heredoc in the arguments, and we need to use the bounds of the
359
372
  # arguments to determine how large the arg_paren is.
360
- ending = (args && args[:end] > rparen[:end]) ? args : rparen
373
+ ending = (args && args[:el] > rparen[:el]) ? args : rparen
361
374
 
362
375
  {
363
376
  type: :arg_paren,
364
377
  body: [args],
365
- start: beging[:start],
378
+ sl: beging[:sl],
366
379
  sc: beging[:sc],
367
- end: ending[:end],
380
+ el: ending[:el],
368
381
  ec: ending[:ec]
369
382
  }
370
383
  end
@@ -381,9 +394,9 @@ class Prettier::Parser < Ripper
381
394
  {
382
395
  type: :array,
383
396
  body: [contents],
384
- start: beging[:start],
397
+ sl: beging[:sl],
385
398
  sc: beging[:sc],
386
- end: ending[:end],
399
+ el: ending[:el],
387
400
  ec: ending[:ec]
388
401
  }
389
402
  else
@@ -393,7 +406,7 @@ class Prettier::Parser < Ripper
393
406
  ending.merge!(
394
407
  type: :array,
395
408
  body: [contents],
396
- start: contents[:start],
409
+ sl: contents[:sl],
397
410
  sc: contents[:sc]
398
411
  )
399
412
  end
@@ -407,9 +420,9 @@ class Prettier::Parser < Ripper
407
420
  {
408
421
  type: :aryptn,
409
422
  body: [const, preargs, splatarg, postargs],
410
- start: pieces[0][:start],
423
+ sl: pieces[0][:sl],
411
424
  sc: pieces[0][:sc],
412
- end: pieces[-1][:end],
425
+ el: pieces[-1][:el],
413
426
  ec: pieces[-1][:ec]
414
427
  }
415
428
  end
@@ -421,7 +434,7 @@ class Prettier::Parser < Ripper
421
434
  left.merge(
422
435
  type: :assign,
423
436
  body: [left, right],
424
- end: right[:end],
437
+ el: right[:el],
425
438
  ec: right[:ec]
426
439
  )
427
440
  end
@@ -433,9 +446,9 @@ class Prettier::Parser < Ripper
433
446
  {
434
447
  type: :assoc_new,
435
448
  body: [key, value],
436
- start: key[:start],
449
+ sl: key[:sl],
437
450
  sc: key[:sc],
438
- end: value[:end],
451
+ el: value[:el],
439
452
  ec: value[:ec]
440
453
  }
441
454
  end
@@ -446,7 +459,7 @@ class Prettier::Parser < Ripper
446
459
  find_scanner_event(:@op, '**').merge!(
447
460
  type: :assoc_splat,
448
461
  body: [contents],
449
- end: contents[:end],
462
+ el: contents[:el],
450
463
  ec: contents[:ec]
451
464
  )
452
465
  end
@@ -459,9 +472,9 @@ class Prettier::Parser < Ripper
459
472
  {
460
473
  type: :assoclist_from_args,
461
474
  body: assocs,
462
- start: assocs[0][:start],
475
+ sl: assocs[0][:sl],
463
476
  sc: assocs[0][:sc],
464
- end: assocs[-1][:end],
477
+ el: assocs[-1][:el],
465
478
  ec: assocs[-1][:ec]
466
479
  }
467
480
  end
@@ -474,9 +487,9 @@ class Prettier::Parser < Ripper
474
487
  {
475
488
  type: :bare_assoc_hash,
476
489
  body: assoc_news,
477
- start: assoc_news[0][:start],
490
+ sl: assoc_news[0][:sl],
478
491
  sc: assoc_news[0][:sc],
479
- end: assoc_news[-1][:end],
492
+ el: assoc_news[-1][:el],
480
493
  ec: assoc_news[-1][:ec]
481
494
  }
482
495
  end
@@ -497,7 +510,7 @@ class Prettier::Parser < Ripper
497
510
  beging.merge!(
498
511
  type: :begin,
499
512
  body: [bodystmt],
500
- end: bodystmt[:end],
513
+ el: bodystmt[:el],
501
514
  ec: bodystmt[:ec]
502
515
  )
503
516
  end
@@ -508,9 +521,9 @@ class Prettier::Parser < Ripper
508
521
  {
509
522
  type: :binary,
510
523
  body: [left, oper, right],
511
- start: left[:start],
524
+ sl: left[:sl],
512
525
  sc: left[:sc],
513
- end: right[:end],
526
+ el: right[:el],
514
527
  ec: right[:ec]
515
528
  }
516
529
  end
@@ -530,9 +543,9 @@ class Prettier::Parser < Ripper
530
543
  {
531
544
  type: :block_var,
532
545
  body: [params, locals],
533
- start: beging[:start],
546
+ sl: beging[:sl],
534
547
  sc: beging[:sc],
535
- end: ending[:end],
548
+ el: ending[:el],
536
549
  ec: ending[:ec]
537
550
  }
538
551
  end
@@ -543,7 +556,7 @@ class Prettier::Parser < Ripper
543
556
  find_scanner_event(:@op, '&').merge!(
544
557
  type: :blockarg,
545
558
  body: [ident],
546
- end: ident[:end],
559
+ el: ident[:el],
547
560
  ec: ident[:ec]
548
561
  )
549
562
  end
@@ -574,9 +587,9 @@ class Prettier::Parser < Ripper
574
587
  BodyStmt.new(
575
588
  type: :bodystmt,
576
589
  body: [stmts, rescued, ensured, elsed],
577
- start: lineno,
590
+ sl: lineno,
578
591
  sc: char_pos,
579
- end: lineno,
592
+ el: lineno,
580
593
  ec: char_pos
581
594
  )
582
595
  end
@@ -594,9 +607,9 @@ class Prettier::Parser < Ripper
594
607
  {
595
608
  type: :brace_block,
596
609
  body: [block_var, stmts],
597
- start: beging[:start],
610
+ sl: beging[:sl],
598
611
  sc: beging[:sc],
599
- end: ending[:end],
612
+ el: ending[:el],
600
613
  ec: ending[:ec]
601
614
  }
602
615
  end
@@ -617,7 +630,7 @@ class Prettier::Parser < Ripper
617
630
  beging.merge!(
618
631
  type: :break,
619
632
  body: [args_add_block],
620
- end: args_add_block[:end],
633
+ el: args_add_block[:el],
621
634
  ec: args_add_block[:ec]
622
635
  )
623
636
  end
@@ -634,10 +647,6 @@ class Prettier::Parser < Ripper
634
647
  # foo.(1, 2, 3)
635
648
  #
636
649
  def on_call(receiver, oper, sending)
637
- # Make sure we take the operator out of the scanner events so that it
638
- # doesn't get confused for a unary operator later.
639
- scanner_events.delete(oper)
640
-
641
650
  ending = sending
642
651
 
643
652
  if sending == :call
@@ -651,9 +660,9 @@ class Prettier::Parser < Ripper
651
660
  {
652
661
  type: :call,
653
662
  body: [receiver, oper, sending],
654
- start: receiver[:start],
663
+ sl: receiver[:sl],
655
664
  sc: receiver[:sc],
656
- end: ending[:end],
665
+ el: ending[:el],
657
666
  ec: ending[:ec]
658
667
  }
659
668
  end
@@ -672,7 +681,7 @@ class Prettier::Parser < Ripper
672
681
 
673
682
  beging.merge!(
674
683
  body: [switch, consequent],
675
- end: consequent[:end],
684
+ el: consequent[:el],
676
685
  ec: consequent[:ec]
677
686
  )
678
687
  end
@@ -714,9 +723,9 @@ class Prettier::Parser < Ripper
714
723
  {
715
724
  type: :class,
716
725
  body: [const, superclass, bodystmt],
717
- start: beging[:start],
726
+ sl: beging[:sl],
718
727
  sc: beging[:sc],
719
- end: ending[:end],
728
+ el: ending[:el],
720
729
  ec: ending[:ec]
721
730
  }
722
731
  end
@@ -728,9 +737,9 @@ class Prettier::Parser < Ripper
728
737
  {
729
738
  type: :command,
730
739
  body: [ident, args],
731
- start: ident[:start],
740
+ sl: ident[:sl],
732
741
  sc: ident[:sc],
733
- end: args[:end],
742
+ el: args[:el],
734
743
  ec: args[:ec]
735
744
  }
736
745
  end
@@ -740,19 +749,14 @@ class Prettier::Parser < Ripper
740
749
  # of the method, the operator being used to send the method, the name of
741
750
  # the method, and the arguments being passed to the method.
742
751
  def on_command_call(receiver, oper, ident, args)
743
- # Make sure we take the operator out of the scanner events so that it
744
- # doesn't get confused for a unary operator later.
745
- scanner_events.delete(oper)
746
-
747
- # Grab the ending from either the arguments or the method being sent
748
752
  ending = args || ident
749
753
 
750
754
  {
751
755
  type: :command_call,
752
756
  body: [receiver, oper, ident, args],
753
- start: receiver[:start],
757
+ sl: receiver[:sl],
754
758
  sc: receiver[:sc],
755
- end: ending[:end],
759
+ el: ending[:el],
756
760
  ec: ending[:ec]
757
761
  }
758
762
  end
@@ -767,9 +771,9 @@ class Prettier::Parser < Ripper
767
771
  {
768
772
  type: :const_path_field,
769
773
  body: [left, const],
770
- start: left[:start],
774
+ sl: left[:sl],
771
775
  sc: left[:sc],
772
- end: const[:end],
776
+ el: const[:el],
773
777
  ec: const[:ec]
774
778
  }
775
779
  end
@@ -784,9 +788,9 @@ class Prettier::Parser < Ripper
784
788
  {
785
789
  type: :const_path_ref,
786
790
  body: [left, const],
787
- start: left[:start],
791
+ sl: left[:sl],
788
792
  sc: left[:sc],
789
- end: const[:end],
793
+ el: const[:el],
790
794
  ec: const[:ec]
791
795
  }
792
796
  end
@@ -837,9 +841,9 @@ class Prettier::Parser < Ripper
837
841
  {
838
842
  type: :defsl,
839
843
  body: [ident, params, bodystmt],
840
- start: beging[:start],
844
+ sl: beging[:sl],
841
845
  sc: beging[:sc],
842
- end: bodystmt[:end],
846
+ el: bodystmt[:el],
843
847
  ec: bodystmt[:ec]
844
848
  }
845
849
  )
@@ -857,9 +861,9 @@ class Prettier::Parser < Ripper
857
861
  {
858
862
  type: :def,
859
863
  body: [ident, params, bodystmt],
860
- start: beging[:start],
864
+ sl: beging[:sl],
861
865
  sc: beging[:sc],
862
- end: ending[:end],
866
+ el: ending[:el],
863
867
  ec: ending[:ec]
864
868
  }
865
869
  end
@@ -897,9 +901,9 @@ class Prettier::Parser < Ripper
897
901
  {
898
902
  type: :defs,
899
903
  body: [target, oper, ident, params, bodystmt],
900
- start: beging[:start],
904
+ sl: beging[:sl],
901
905
  sc: beging[:sc],
902
- end: ending[:end],
906
+ el: ending[:el],
903
907
  ec: ending[:ec]
904
908
  }
905
909
  end
@@ -917,7 +921,7 @@ class Prettier::Parser < Ripper
917
921
  beging.merge!(
918
922
  type: :defined,
919
923
  body: [value],
920
- end: ending[:end],
924
+ el: ending[:el],
921
925
  ec: ending[:ec]
922
926
  )
923
927
  end
@@ -935,9 +939,9 @@ class Prettier::Parser < Ripper
935
939
  {
936
940
  type: :do_block,
937
941
  body: [block_var, bodystmt],
938
- start: beging[:start],
942
+ sl: beging[:sl],
939
943
  sc: beging[:sc],
940
- end: ending[:end],
944
+ el: ending[:el],
941
945
  ec: ending[:ec]
942
946
  }
943
947
  end
@@ -954,9 +958,9 @@ class Prettier::Parser < Ripper
954
958
  {
955
959
  type: :dot2,
956
960
  body: [left, right],
957
- start: beging[:start],
961
+ sl: beging[:sl],
958
962
  sc: beging[:sc],
959
- end: ending[:end],
963
+ el: ending[:el],
960
964
  ec: ending[:ec]
961
965
  }
962
966
  end
@@ -973,9 +977,9 @@ class Prettier::Parser < Ripper
973
977
  {
974
978
  type: :dot3,
975
979
  body: [left, right],
976
- start: beging[:start],
980
+ sl: beging[:sl],
977
981
  sc: beging[:sc],
978
- end: ending[:end],
982
+ el: ending[:el],
979
983
  ec: ending[:ec]
980
984
  }
981
985
  end
@@ -1008,7 +1012,7 @@ class Prettier::Parser < Ripper
1008
1012
  type: :dyna_symbol,
1009
1013
  quote: beging[:body][1],
1010
1014
  body: string[:body],
1011
- end: ending[:end],
1015
+ el: ending[:el],
1012
1016
  ec: ending[:ec]
1013
1017
  )
1014
1018
  else
@@ -1019,9 +1023,9 @@ class Prettier::Parser < Ripper
1019
1023
  string.merge!(
1020
1024
  type: :dyna_symbol,
1021
1025
  quote: ending[:body][0],
1022
- start: beging[:start],
1026
+ sl: beging[:sl],
1023
1027
  sc: beging[:sc],
1024
- end: ending[:end],
1028
+ el: ending[:el],
1025
1029
  ec: ending[:ec]
1026
1030
  )
1027
1031
  end
@@ -1052,9 +1056,9 @@ class Prettier::Parser < Ripper
1052
1056
  {
1053
1057
  type: :else,
1054
1058
  body: [stmts],
1055
- start: beging[:start],
1059
+ sl: beging[:sl],
1056
1060
  sc: beging[:sc],
1057
- end: ending[:end],
1061
+ el: ending[:el],
1058
1062
  ec: ending[:ec]
1059
1063
  }
1060
1064
  end
@@ -1072,9 +1076,9 @@ class Prettier::Parser < Ripper
1072
1076
  {
1073
1077
  type: :elsif,
1074
1078
  body: [predicate, stmts, consequent],
1075
- start: beging[:start],
1079
+ sl: beging[:sl],
1076
1080
  sc: beging[:sc],
1077
- end: ending[:end],
1081
+ el: ending[:el],
1078
1082
  ec: ending[:ec]
1079
1083
  }
1080
1084
  end
@@ -1085,7 +1089,7 @@ class Prettier::Parser < Ripper
1085
1089
  # and add to it as we get content. It always starts with this scanner
1086
1090
  # event, so here we'll initialize the current embdoc.
1087
1091
  def on_embdoc_beg(value)
1088
- @embdoc = { type: :@embdoc, value: value, start: lineno, sc: char_pos }
1092
+ @embdoc = { type: :@embdoc, value: value, sl: lineno, sc: char_pos }
1089
1093
  end
1090
1094
 
1091
1095
  # This is a scanner event that gets hit when we're inside an embdoc and
@@ -1104,7 +1108,7 @@ class Prettier::Parser < Ripper
1104
1108
  @comments <<
1105
1109
  @embdoc.merge!(
1106
1110
  value: @embdoc[:value] << value.chomp,
1107
- end: lineno,
1111
+ el: lineno,
1108
1112
  ec: char_pos + value.length - 1
1109
1113
  )
1110
1114
 
@@ -1129,9 +1133,9 @@ class Prettier::Parser < Ripper
1129
1133
  {
1130
1134
  type: :ensure,
1131
1135
  body: [beging, stmts],
1132
- start: beging[:start],
1136
+ sl: beging[:sl],
1133
1137
  sc: beging[:sc],
1134
- end: ending[:end],
1138
+ el: ending[:el],
1135
1139
  ec: ending[:ec]
1136
1140
  }
1137
1141
  end
@@ -1160,9 +1164,9 @@ class Prettier::Parser < Ripper
1160
1164
  {
1161
1165
  type: :field,
1162
1166
  body: [left, oper, right],
1163
- start: left[:start],
1167
+ sl: left[:sl],
1164
1168
  sc: left[:sc],
1165
- end: right[:end],
1169
+ el: right[:el],
1166
1170
  ec: right[:ec]
1167
1171
  }
1168
1172
  end
@@ -1176,9 +1180,9 @@ class Prettier::Parser < Ripper
1176
1180
  {
1177
1181
  type: :fndptn,
1178
1182
  body: [const, presplat, args, postsplat],
1179
- start: beging[:start],
1183
+ sl: beging[:sl],
1180
1184
  sc: beging[:sc],
1181
- end: ending[:end],
1185
+ el: ending[:el],
1182
1186
  ec: ending[:ec]
1183
1187
  }
1184
1188
  end
@@ -1196,9 +1200,9 @@ class Prettier::Parser < Ripper
1196
1200
  {
1197
1201
  type: :for,
1198
1202
  body: [ident, enumerable, stmts],
1199
- start: beging[:start],
1203
+ sl: beging[:sl],
1200
1204
  sc: beging[:sc],
1201
- end: ending[:end],
1205
+ el: ending[:el],
1202
1206
  ec: ending[:ec]
1203
1207
  }
1204
1208
  end
@@ -1219,9 +1223,9 @@ class Prettier::Parser < Ripper
1219
1223
  {
1220
1224
  type: :hash,
1221
1225
  body: [assoclist_from_args],
1222
- start: beging[:start],
1226
+ sl: beging[:sl],
1223
1227
  sc: beging[:sc],
1224
- end: ending[:end],
1228
+ el: ending[:el],
1225
1229
  ec: ending[:ec]
1226
1230
  }
1227
1231
  end
@@ -1234,8 +1238,8 @@ class Prettier::Parser < Ripper
1234
1238
  # printer through our embed function.
1235
1239
  def on_heredoc_beg(beging)
1236
1240
  location = {
1237
- start: lineno,
1238
- end: lineno,
1241
+ sl: lineno,
1242
+ el: lineno,
1239
1243
  sc: char_pos,
1240
1244
  ec: char_pos + beging.length + 1
1241
1245
  }
@@ -1259,7 +1263,7 @@ class Prettier::Parser < Ripper
1259
1263
 
1260
1264
  # This is a scanner event that represents the end of the heredoc.
1261
1265
  def on_heredoc_end(ending)
1262
- @heredocs[-1].merge!(ending: ending.chomp, end: lineno, ec: char_pos)
1266
+ @heredocs[-1].merge!(ending: ending.chomp, el: lineno, ec: char_pos)
1263
1267
  end
1264
1268
 
1265
1269
  # hshptn is a parser event that represents matching against a hash pattern
@@ -1270,9 +1274,9 @@ class Prettier::Parser < Ripper
1270
1274
  {
1271
1275
  type: :hshptn,
1272
1276
  body: [const, kw, kwrest],
1273
- start: pieces[0][:start],
1277
+ sl: pieces[0][:sl],
1274
1278
  sc: pieces[0][:sc],
1275
- end: pieces[-1][:end],
1279
+ el: pieces[-1][:el],
1276
1280
  ec: pieces[-1][:ec]
1277
1281
  }
1278
1282
  end
@@ -1289,9 +1293,9 @@ class Prettier::Parser < Ripper
1289
1293
  {
1290
1294
  type: :if,
1291
1295
  body: [predicate, stmts, consequent],
1292
- start: beging[:start],
1296
+ sl: beging[:sl],
1293
1297
  sc: beging[:sc],
1294
- end: ending[:end],
1298
+ el: ending[:el],
1295
1299
  ec: ending[:ec]
1296
1300
  }
1297
1301
  end
@@ -1303,7 +1307,7 @@ class Prettier::Parser < Ripper
1303
1307
  predicate.merge(
1304
1308
  type: :ifop,
1305
1309
  body: [predicate, truthy, falsy],
1306
- end: falsy[:end],
1310
+ el: falsy[:el],
1307
1311
  ec: falsy[:ec]
1308
1312
  )
1309
1313
  end
@@ -1317,9 +1321,9 @@ class Prettier::Parser < Ripper
1317
1321
  {
1318
1322
  type: :if_mod,
1319
1323
  body: [predicate, statement],
1320
- start: statement[:start],
1324
+ sl: statement[:sl],
1321
1325
  sc: statement[:sc],
1322
- end: predicate[:end],
1326
+ el: predicate[:el],
1323
1327
  ec: predicate[:ec]
1324
1328
  }
1325
1329
  end
@@ -1339,7 +1343,7 @@ class Prettier::Parser < Ripper
1339
1343
  beging.merge!(
1340
1344
  type: :in,
1341
1345
  body: [pattern, stmts, consequent],
1342
- end: ending[:end],
1346
+ el: ending[:el],
1343
1347
  ec: ending[:ec]
1344
1348
  )
1345
1349
  end
@@ -1353,7 +1357,7 @@ class Prettier::Parser < Ripper
1353
1357
  oper.merge!(
1354
1358
  type: :kwrest_param,
1355
1359
  body: [ident],
1356
- end: ident[:end],
1360
+ el: ident[:el],
1357
1361
  ec: ident[:ec]
1358
1362
  )
1359
1363
  end
@@ -1381,9 +1385,9 @@ class Prettier::Parser < Ripper
1381
1385
  {
1382
1386
  type: :lambda,
1383
1387
  body: [params, stmts],
1384
- start: beging[:start],
1388
+ sl: beging[:sl],
1385
1389
  sc: beging[:sc],
1386
- end: closing[:end],
1390
+ el: closing[:el],
1387
1391
  ec: closing[:ec]
1388
1392
  }
1389
1393
  end
@@ -1411,9 +1415,9 @@ class Prettier::Parser < Ripper
1411
1415
  {
1412
1416
  type: :massign,
1413
1417
  body: [left, right],
1414
- start: left[:start],
1418
+ sl: left[:sl],
1415
1419
  sc: left[:sc],
1416
- end: right[:end],
1420
+ el: right[:el],
1417
1421
  ec: right[:ec]
1418
1422
  }
1419
1423
  end
@@ -1433,9 +1437,9 @@ class Prettier::Parser < Ripper
1433
1437
  {
1434
1438
  type: :method_add_arg,
1435
1439
  body: [fcall, arg_paren],
1436
- start: fcall[:start],
1440
+ sl: fcall[:sl],
1437
1441
  sc: fcall[:sc],
1438
- end: arg_paren[:end],
1442
+ el: arg_paren[:el],
1439
1443
  ec: arg_paren[:ec]
1440
1444
  }
1441
1445
  end
@@ -1447,9 +1451,9 @@ class Prettier::Parser < Ripper
1447
1451
  {
1448
1452
  type: :method_add_block,
1449
1453
  body: [method_add_arg, block],
1450
- start: method_add_arg[:start],
1454
+ sl: method_add_arg[:sl],
1451
1455
  sc: method_add_arg[:sc],
1452
- end: block[:end],
1456
+ el: block[:el],
1453
1457
  ec: block[:ec]
1454
1458
  }
1455
1459
  end
@@ -1461,9 +1465,9 @@ class Prettier::Parser < Ripper
1461
1465
  {
1462
1466
  type: :mlhs,
1463
1467
  body: [],
1464
- start: lineno,
1468
+ sl: lineno,
1465
1469
  sc: char_pos,
1466
- end: lineno,
1470
+ el: lineno,
1467
1471
  ec: char_pos
1468
1472
  }
1469
1473
  end
@@ -1475,7 +1479,7 @@ class Prettier::Parser < Ripper
1475
1479
  if mlhs[:body].empty?
1476
1480
  part.merge(type: :mlhs, body: [part])
1477
1481
  else
1478
- mlhs.merge!(body: mlhs[:body] << part, end: part[:end], ec: part[:ec])
1482
+ mlhs.merge!(body: mlhs[:body] << part, el: part[:el], ec: part[:ec])
1479
1483
  end
1480
1484
  end
1481
1485
 
@@ -1488,7 +1492,7 @@ class Prettier::Parser < Ripper
1488
1492
  mlhs_add_star.merge(
1489
1493
  type: :mlhs_add_post,
1490
1494
  body: [mlhs_add_star, mlhs],
1491
- end: mlhs[:end],
1495
+ el: mlhs[:el],
1492
1496
  ec: mlhs[:ec]
1493
1497
  )
1494
1498
  end
@@ -1504,9 +1508,9 @@ class Prettier::Parser < Ripper
1504
1508
  {
1505
1509
  type: :mlhs_add_star,
1506
1510
  body: [mlhs, part],
1507
- start: beging[:start],
1511
+ sl: beging[:sl],
1508
1512
  sc: beging[:sc],
1509
- end: ending[:end],
1513
+ el: ending[:el],
1510
1514
  ec: ending[:ec]
1511
1515
  }
1512
1516
  end
@@ -1526,9 +1530,9 @@ class Prettier::Parser < Ripper
1526
1530
  {
1527
1531
  type: :mlhs_paren,
1528
1532
  body: [contents],
1529
- start: beging[:start],
1533
+ sl: beging[:sl],
1530
1534
  sc: beging[:sc],
1531
- end: ending[:end],
1535
+ el: ending[:el],
1532
1536
  ec: ending[:ec]
1533
1537
  }
1534
1538
  end
@@ -1545,9 +1549,9 @@ class Prettier::Parser < Ripper
1545
1549
  {
1546
1550
  type: :module,
1547
1551
  body: [const, bodystmt],
1548
- start: beging[:start],
1552
+ sl: beging[:sl],
1549
1553
  sc: beging[:sc],
1550
- end: ending[:end],
1554
+ el: ending[:el],
1551
1555
  ec: ending[:ec]
1552
1556
  }
1553
1557
  end
@@ -1560,9 +1564,9 @@ class Prettier::Parser < Ripper
1560
1564
  {
1561
1565
  type: :mrhs,
1562
1566
  body: [],
1563
- start: lineno,
1567
+ sl: lineno,
1564
1568
  sc: char_pos,
1565
- end: lineno,
1569
+ el: lineno,
1566
1570
  ec: char_pos
1567
1571
  }
1568
1572
  end
@@ -1573,7 +1577,7 @@ class Prettier::Parser < Ripper
1573
1577
  if mrhs[:body].empty?
1574
1578
  part.merge(type: :mrhs, body: [part])
1575
1579
  else
1576
- mrhs.merge!(body: mrhs[:body] << part, end: part[:end], ec: part[:ec])
1580
+ mrhs.merge!(body: mrhs[:body] << part, el: part[:el], ec: part[:ec])
1577
1581
  end
1578
1582
  end
1579
1583
 
@@ -1587,9 +1591,9 @@ class Prettier::Parser < Ripper
1587
1591
  {
1588
1592
  type: :mrhs_add_star,
1589
1593
  body: [mrhs, part],
1590
- start: beging[:start],
1594
+ sl: beging[:sl],
1591
1595
  sc: beging[:sc],
1592
- end: ending[:end],
1596
+ el: ending[:el],
1593
1597
  ec: ending[:ec]
1594
1598
  }
1595
1599
  end
@@ -1613,7 +1617,7 @@ class Prettier::Parser < Ripper
1613
1617
  find_scanner_event(:@kw, 'next').merge!(
1614
1618
  type: :next,
1615
1619
  body: [args_add_block],
1616
- end: args_add_block[:end],
1620
+ el: args_add_block[:el],
1617
1621
  ec: args_add_block[:ec]
1618
1622
  )
1619
1623
  end
@@ -1626,7 +1630,7 @@ class Prettier::Parser < Ripper
1626
1630
  left.merge(
1627
1631
  type: :opassign,
1628
1632
  body: [left, oper, right],
1629
- end: right[:end],
1633
+ el: right[:el],
1630
1634
  ec: right[:ec]
1631
1635
  )
1632
1636
  end
@@ -1641,13 +1645,13 @@ class Prettier::Parser < Ripper
1641
1645
  location =
1642
1646
  if flattened.any?
1643
1647
  {
1644
- start: flattened[0][:start],
1648
+ sl: flattened[0][:sl],
1645
1649
  sc: flattened[0][:sc],
1646
- end: flattened[-1][:end],
1650
+ el: flattened[-1][:el],
1647
1651
  ec: flattened[-1][:ec]
1648
1652
  }
1649
1653
  else
1650
- { start: lineno, sc: char_pos, end: lineno, ec: char_pos }
1654
+ { sl: lineno, sc: char_pos, el: lineno, ec: char_pos }
1651
1655
  end
1652
1656
 
1653
1657
  location.merge!(type: :params, body: types)
@@ -1657,12 +1661,17 @@ class Prettier::Parser < Ripper
1657
1661
  # anywhere in a Ruby program. It accepts as arguments the contents, which
1658
1662
  # can be either params or statements.
1659
1663
  def on_paren(contents)
1664
+ beging = find_scanner_event(:@lparen)
1660
1665
  ending = find_scanner_event(:@rparen)
1661
1666
 
1662
- find_scanner_event(:@lparen).merge!(
1667
+ if contents && contents[:type] == :params
1668
+ contents.merge!(sc: beging[:ec], ec: ending[:sc])
1669
+ end
1670
+
1671
+ beging.merge!(
1663
1672
  type: :paren,
1664
1673
  body: [contents],
1665
- end: ending[:end],
1674
+ el: ending[:el],
1666
1675
  ec: ending[:ec]
1667
1676
  )
1668
1677
  end
@@ -1672,7 +1681,7 @@ class Prettier::Parser < Ripper
1672
1681
  # source string. We'll also attach on the __END__ content if there was
1673
1682
  # some found at the end of the source string.
1674
1683
  def on_program(stmts)
1675
- range = { start: 1, end: lines.length, sc: 0, ec: source.length }
1684
+ range = { sl: 1, el: lines.length, sc: 0, ec: source.length }
1676
1685
 
1677
1686
  stmts[:body] << @__end__ if @__end__
1678
1687
  stmts.bind(0, source.length)
@@ -1694,7 +1703,7 @@ class Prettier::Parser < Ripper
1694
1703
  def on_qsymbols_add(qsymbols, tstring_content)
1695
1704
  qsymbols.merge!(
1696
1705
  body: qsymbols[:body] << tstring_content,
1697
- end: tstring_content[:end],
1706
+ el: tstring_content[:el],
1698
1707
  ec: tstring_content[:ec]
1699
1708
  )
1700
1709
  end
@@ -1713,7 +1722,7 @@ class Prettier::Parser < Ripper
1713
1722
  def on_qwords_add(qwords, tstring_content)
1714
1723
  qwords.merge!(
1715
1724
  body: qwords[:body] << tstring_content,
1716
- end: tstring_content[:end],
1725
+ el: tstring_content[:el],
1717
1726
  ec: tstring_content[:ec]
1718
1727
  )
1719
1728
  end
@@ -1739,7 +1748,7 @@ class Prettier::Parser < Ripper
1739
1748
  def on_regexp_add(regexp, piece)
1740
1749
  regexp.merge!(
1741
1750
  body: regexp[:body] << piece,
1742
- end: regexp[:end],
1751
+ el: regexp[:el],
1743
1752
  ec: regexp[:ec]
1744
1753
  )
1745
1754
  end
@@ -1752,7 +1761,7 @@ class Prettier::Parser < Ripper
1752
1761
  regexp.merge!(
1753
1762
  type: :regexp_literal,
1754
1763
  ending: ending[:body],
1755
- end: ending[:end],
1764
+ el: ending[:el],
1756
1765
  ec: ending[:ec]
1757
1766
  )
1758
1767
  end
@@ -1765,8 +1774,8 @@ class Prettier::Parser < Ripper
1765
1774
  def bind_end(ec)
1766
1775
  merge!(ec: ec)
1767
1776
 
1768
- stmts = self[:body][2]
1769
- consequent = self[:body][3]
1777
+ stmts = self[:body][1]
1778
+ consequent = self[:body][2]
1770
1779
 
1771
1780
  if consequent
1772
1781
  consequent.bind_end(ec)
@@ -1781,17 +1790,31 @@ class Prettier::Parser < Ripper
1781
1790
  # inside of a bodystmt.
1782
1791
  def on_rescue(exceptions, variable, stmts, consequent)
1783
1792
  beging = find_scanner_event(:@kw, 'rescue')
1793
+ exceptions = exceptions[0] if exceptions.is_a?(Array)
1784
1794
 
1785
- last_exception = exceptions.is_a?(Array) ? exceptions[-1] : exceptions
1786
- last_node = variable || last_exception || beging
1787
-
1795
+ last_node = variable || exceptions || beging
1788
1796
  stmts.bind(find_next_statement_start(last_node[:ec]), char_pos)
1789
1797
 
1798
+ # We add an additional inner node here that ripper doesn't provide so that
1799
+ # we have a nice place to attach inline comment. But we only need it if we
1800
+ # have an exception or a variable that we're rescuing.
1801
+ rescue_ex =
1802
+ if exceptions || variable
1803
+ {
1804
+ type: :rescue_ex,
1805
+ body: [exceptions, variable],
1806
+ sl: beging[:sl],
1807
+ sc: beging[:ec] + 1,
1808
+ el: last_node[:el],
1809
+ ec: last_node[:ec]
1810
+ }
1811
+ end
1812
+
1790
1813
  Rescue.new(
1791
1814
  beging.merge!(
1792
1815
  type: :rescue,
1793
- body: [exceptions, variable, stmts, consequent],
1794
- end: lineno,
1816
+ body: [rescue_ex, stmts, consequent],
1817
+ el: lineno,
1795
1818
  ec: char_pos
1796
1819
  )
1797
1820
  )
@@ -1806,9 +1829,9 @@ class Prettier::Parser < Ripper
1806
1829
  {
1807
1830
  type: :rescue_mod,
1808
1831
  body: [statement, rescued],
1809
- start: statement[:start],
1832
+ sl: statement[:sl],
1810
1833
  sc: statement[:sc],
1811
- end: rescued[:end],
1834
+ el: rescued[:el],
1812
1835
  ec: rescued[:ec]
1813
1836
  }
1814
1837
  end
@@ -1824,7 +1847,7 @@ class Prettier::Parser < Ripper
1824
1847
  oper.merge!(
1825
1848
  type: :rest_param,
1826
1849
  body: [ident],
1827
- end: ident[:end],
1850
+ el: ident[:el],
1828
1851
  ec: ident[:ec]
1829
1852
  )
1830
1853
  end
@@ -1842,7 +1865,7 @@ class Prettier::Parser < Ripper
1842
1865
  find_scanner_event(:@kw, 'return').merge!(
1843
1866
  type: :return,
1844
1867
  body: [args_add_block],
1845
- end: args_add_block[:end],
1868
+ el: args_add_block[:el],
1846
1869
  ec: args_add_block[:ec]
1847
1870
  )
1848
1871
  end
@@ -1874,9 +1897,9 @@ class Prettier::Parser < Ripper
1874
1897
  {
1875
1898
  type: :sclass,
1876
1899
  body: [target, bodystmt],
1877
- start: beging[:start],
1900
+ sl: beging[:sl],
1878
1901
  sc: beging[:sc],
1879
- end: ending[:end],
1902
+ el: ending[:el],
1880
1903
  ec: ending[:ec]
1881
1904
  }
1882
1905
  end
@@ -1889,12 +1912,21 @@ class Prettier::Parser < Ripper
1889
1912
  # propagate that onto void_stmt nodes inside the stmts in order to make sure
1890
1913
  # all comments get printed appropriately.
1891
1914
  class Stmts < SimpleDelegator
1915
+ attr_reader :parser
1916
+
1917
+ def initialize(parser, values)
1918
+ @parser = parser
1919
+ __setobj__(values)
1920
+ end
1921
+
1892
1922
  def bind(sc, ec)
1893
1923
  merge!(sc: sc, ec: ec)
1894
1924
 
1895
1925
  if self[:body][0][:type] == :void_stmt
1896
1926
  self[:body][0].merge!(sc: sc, ec: sc)
1897
1927
  end
1928
+
1929
+ attach_comments(sc, ec)
1898
1930
  end
1899
1931
 
1900
1932
  def bind_end(ec)
@@ -1903,14 +1935,30 @@ class Prettier::Parser < Ripper
1903
1935
 
1904
1936
  def <<(statement)
1905
1937
  if self[:body].any?
1906
- merge!(statement.slice(:end, :ec))
1938
+ merge!(statement.slice(:el, :ec))
1907
1939
  else
1908
- merge!(statement.slice(:start, :end, :sc, :ec))
1940
+ merge!(statement.slice(:sl, :el, :sc, :ec))
1909
1941
  end
1910
1942
 
1911
1943
  self[:body] << statement
1912
1944
  self
1913
1945
  end
1946
+
1947
+ private
1948
+
1949
+ def attach_comments(sc, ec)
1950
+ attachable =
1951
+ parser.comments.select do |comment|
1952
+ comment[:type] == :@comment && !comment[:inline] &&
1953
+ sc <= comment[:sc] && ec >= comment[:ec] &&
1954
+ !comment[:value].include?('prettier-ignore')
1955
+ end
1956
+
1957
+ return if attachable.empty?
1958
+
1959
+ parser.comments -= attachable
1960
+ self[:body] = (self[:body] + attachable).sort_by! { |node| node[:sc] }
1961
+ end
1914
1962
  end
1915
1963
 
1916
1964
  # stmts_new is a parser event that represents the beginning of a list of
@@ -1918,10 +1966,11 @@ class Prettier::Parser < Ripper
1918
1966
  # stmts_add events, which we'll append onto an array body.
1919
1967
  def on_stmts_new
1920
1968
  Stmts.new(
1969
+ self,
1921
1970
  type: :stmts,
1922
1971
  body: [],
1923
- start: lineno,
1924
- end: lineno,
1972
+ sl: lineno,
1973
+ el: lineno,
1925
1974
  sc: char_pos,
1926
1975
  ec: char_pos
1927
1976
  )
@@ -1945,9 +1994,9 @@ class Prettier::Parser < Ripper
1945
1994
  {
1946
1995
  type: :string_concat,
1947
1996
  body: [left, right],
1948
- start: left[:start],
1997
+ sl: left[:sl],
1949
1998
  sc: left[:sc],
1950
- end: right[:end],
1999
+ el: right[:el],
1951
2000
  ec: right[:ec]
1952
2001
  }
1953
2002
  end
@@ -1961,8 +2010,8 @@ class Prettier::Parser < Ripper
1961
2010
  {
1962
2011
  type: :string,
1963
2012
  body: [],
1964
- start: lineno,
1965
- end: lineno,
2013
+ sl: lineno,
2014
+ el: lineno,
1966
2015
  sc: char_pos,
1967
2016
  ec: char_pos
1968
2017
  }
@@ -1973,11 +2022,7 @@ class Prettier::Parser < Ripper
1973
2022
  # It accepts as arguments the parent string node as well as the additional
1974
2023
  # piece of the string.
1975
2024
  def on_string_add(string, piece)
1976
- string.merge!(
1977
- body: string[:body] << piece,
1978
- end: piece[:end],
1979
- ec: piece[:ec]
1980
- )
2025
+ string.merge!(body: string[:body] << piece, el: piece[:el], ec: piece[:ec])
1981
2026
  end
1982
2027
 
1983
2028
  # string_dvar is a parser event that represents a very special kind of
@@ -1989,7 +2034,7 @@ class Prettier::Parser < Ripper
1989
2034
  find_scanner_event(:@embvar).merge!(
1990
2035
  type: :string_dvar,
1991
2036
  body: [var_ref],
1992
- end: var_ref[:end],
2037
+ el: var_ref[:el],
1993
2038
  ec: var_ref[:ec]
1994
2039
  )
1995
2040
  end
@@ -2007,9 +2052,9 @@ class Prettier::Parser < Ripper
2007
2052
  {
2008
2053
  type: :string_embexpr,
2009
2054
  body: [stmts],
2010
- start: beging[:start],
2055
+ sl: beging[:sl],
2011
2056
  sc: beging[:sc],
2012
- end: ending[:end],
2057
+ el: ending[:el],
2013
2058
  ec: ending[:ec]
2014
2059
  }
2015
2060
  end
@@ -2029,9 +2074,9 @@ class Prettier::Parser < Ripper
2029
2074
  type: :string_literal,
2030
2075
  body: string[:body],
2031
2076
  quote: beging[:body],
2032
- start: beging[:start],
2077
+ sl: beging[:sl],
2033
2078
  sc: beging[:sc],
2034
- end: ending[:end],
2079
+ el: ending[:el],
2035
2080
  ec: ending[:ec]
2036
2081
  }
2037
2082
  end
@@ -2045,7 +2090,7 @@ class Prettier::Parser < Ripper
2045
2090
  find_scanner_event(:@kw, 'super').merge!(
2046
2091
  type: :super,
2047
2092
  body: [contents],
2048
- end: contents[:end],
2093
+ el: contents[:el],
2049
2094
  ec: contents[:ec]
2050
2095
  )
2051
2096
  end
@@ -2096,7 +2141,7 @@ class Prettier::Parser < Ripper
2096
2141
  def on_symbols_add(symbols, word_add)
2097
2142
  symbols.merge!(
2098
2143
  body: symbols[:body] << word_add,
2099
- end: word_add[:end],
2144
+ el: word_add[:el],
2100
2145
  ec: word_add[:ec]
2101
2146
  )
2102
2147
  end
@@ -2125,7 +2170,7 @@ class Prettier::Parser < Ripper
2125
2170
  const.merge(
2126
2171
  type: :top_const_field,
2127
2172
  body: [const],
2128
- start: beging[:start],
2173
+ sl: beging[:sl],
2129
2174
  sc: beging[:sc]
2130
2175
  )
2131
2176
  end
@@ -2141,7 +2186,7 @@ class Prettier::Parser < Ripper
2141
2186
  const.merge(
2142
2187
  type: :top_const_ref,
2143
2188
  body: [const],
2144
- start: beging[:start],
2189
+ sl: beging[:sl],
2145
2190
  sc: beging[:sc]
2146
2191
  )
2147
2192
  end
@@ -2161,7 +2206,7 @@ class Prettier::Parser < Ripper
2161
2206
  type: :unary,
2162
2207
  oper: oper,
2163
2208
  body: [value],
2164
- end: ending[:end],
2209
+ el: ending[:el],
2165
2210
  ec: ending[:ec],
2166
2211
  paren: paren
2167
2212
  )
@@ -2172,7 +2217,7 @@ class Prettier::Parser < Ripper
2172
2217
  # stack. So we need to explicitly disallow those operators.
2173
2218
  index =
2174
2219
  scanner_events.rindex do |scanner_event|
2175
- scanner_event[:type] == :@op &&
2220
+ scanner_event[:type] == :@op && scanner_event[:sc] < value[:sc] &&
2176
2221
  !%w[.. ...].include?(scanner_event[:body])
2177
2222
  end
2178
2223
 
@@ -2181,7 +2226,7 @@ class Prettier::Parser < Ripper
2181
2226
  type: :unary,
2182
2227
  oper: oper[0],
2183
2228
  body: [value],
2184
- end: value[:end],
2229
+ el: value[:el],
2185
2230
  ec: value[:ec]
2186
2231
  )
2187
2232
  end
@@ -2197,7 +2242,7 @@ class Prettier::Parser < Ripper
2197
2242
  find_scanner_event(:@kw, 'undef').merge!(
2198
2243
  type: :undef,
2199
2244
  body: symbol_literals,
2200
- end: last[:end],
2245
+ el: last[:el],
2201
2246
  ec: last[:ec]
2202
2247
  )
2203
2248
  end
@@ -2215,9 +2260,9 @@ class Prettier::Parser < Ripper
2215
2260
  {
2216
2261
  type: :unless,
2217
2262
  body: [predicate, stmts, consequent],
2218
- start: beging[:start],
2263
+ sl: beging[:sl],
2219
2264
  sc: beging[:sc],
2220
- end: ending[:end],
2265
+ el: ending[:el],
2221
2266
  ec: ending[:ec]
2222
2267
  }
2223
2268
  end
@@ -2231,9 +2276,9 @@ class Prettier::Parser < Ripper
2231
2276
  {
2232
2277
  type: :unless_mod,
2233
2278
  body: [predicate, statement],
2234
- start: statement[:start],
2279
+ sl: statement[:sl],
2235
2280
  sc: statement[:sc],
2236
- end: predicate[:end],
2281
+ el: predicate[:el],
2237
2282
  ec: predicate[:ec]
2238
2283
  }
2239
2284
  end
@@ -2257,9 +2302,9 @@ class Prettier::Parser < Ripper
2257
2302
  {
2258
2303
  type: :until,
2259
2304
  body: [predicate, stmts],
2260
- start: beging[:start],
2305
+ sl: beging[:sl],
2261
2306
  sc: beging[:sc],
2262
- end: ending[:end],
2307
+ el: ending[:el],
2263
2308
  ec: ending[:ec]
2264
2309
  }
2265
2310
  end
@@ -2273,9 +2318,9 @@ class Prettier::Parser < Ripper
2273
2318
  {
2274
2319
  type: :until_mod,
2275
2320
  body: [predicate, statement],
2276
- start: statement[:start],
2321
+ sl: statement[:sl],
2277
2322
  sc: statement[:sc],
2278
- end: predicate[:end],
2323
+ el: predicate[:el],
2279
2324
  ec: predicate[:ec]
2280
2325
  }
2281
2326
  end
@@ -2293,9 +2338,9 @@ class Prettier::Parser < Ripper
2293
2338
  {
2294
2339
  type: :var_alias,
2295
2340
  body: [left, right],
2296
- start: beging[:start],
2341
+ sl: beging[:sl],
2297
2342
  sc: beging[:sc],
2298
- end: ending[:end],
2343
+ el: ending[:el],
2299
2344
  ec: ending[:ec]
2300
2345
  }
2301
2346
  end
@@ -2348,7 +2393,7 @@ class Prettier::Parser < Ripper
2348
2393
  # block of code. It often will have comments attached to it, so it requires
2349
2394
  # some special handling.
2350
2395
  def on_void_stmt
2351
- { type: :void_stmt, start: lineno, end: lineno, sc: char_pos, ec: char_pos }
2396
+ { type: :void_stmt, sl: lineno, el: lineno, sc: char_pos, ec: char_pos }
2352
2397
  end
2353
2398
 
2354
2399
  # when is a parser event that represents another clause in a case chain.
@@ -2364,9 +2409,9 @@ class Prettier::Parser < Ripper
2364
2409
  {
2365
2410
  type: :when,
2366
2411
  body: [predicate, stmts, consequent],
2367
- start: beging[:start],
2412
+ sl: beging[:sl],
2368
2413
  sc: beging[:sc],
2369
- end: ending[:end],
2414
+ el: ending[:el],
2370
2415
  ec: ending[:ec]
2371
2416
  }
2372
2417
  end
@@ -2390,9 +2435,9 @@ class Prettier::Parser < Ripper
2390
2435
  {
2391
2436
  type: :while,
2392
2437
  body: [predicate, stmts],
2393
- start: beging[:start],
2438
+ sl: beging[:sl],
2394
2439
  sc: beging[:sc],
2395
- end: ending[:end],
2440
+ el: ending[:el],
2396
2441
  ec: ending[:ec]
2397
2442
  }
2398
2443
  end
@@ -2406,9 +2451,9 @@ class Prettier::Parser < Ripper
2406
2451
  {
2407
2452
  type: :while_mod,
2408
2453
  body: [predicate, statement],
2409
- start: statement[:start],
2454
+ sl: statement[:sl],
2410
2455
  sc: statement[:sc],
2411
- end: predicate[:end],
2456
+ el: predicate[:el],
2412
2457
  ec: predicate[:ec]
2413
2458
  }
2414
2459
  end
@@ -2439,7 +2484,7 @@ class Prettier::Parser < Ripper
2439
2484
  # location information from the first piece.
2440
2485
  piece.merge(type: :word, body: [piece])
2441
2486
  else
2442
- word.merge!(body: word[:body] << piece, end: piece[:end], ec: piece[:ec])
2487
+ word.merge!(body: word[:body] << piece, el: piece[:el], ec: piece[:ec])
2443
2488
  end
2444
2489
  end
2445
2490
 
@@ -2458,7 +2503,7 @@ class Prettier::Parser < Ripper
2458
2503
  def on_words_add(words, word_add)
2459
2504
  words.merge!(
2460
2505
  body: words[:body] << word_add,
2461
- end: word_add[:end],
2506
+ el: word_add[:el],
2462
2507
  ec: word_add[:ec]
2463
2508
  )
2464
2509
  end
@@ -2492,7 +2537,7 @@ class Prettier::Parser < Ripper
2492
2537
  def on_xstring_add(xstring, piece)
2493
2538
  xstring.merge!(
2494
2539
  body: xstring[:body] << piece,
2495
- end: piece[:end],
2540
+ el: piece[:el],
2496
2541
  ec: piece[:ec]
2497
2542
  )
2498
2543
  end
@@ -2518,7 +2563,7 @@ class Prettier::Parser < Ripper
2518
2563
  heredoc.merge!(body: xstring[:body])
2519
2564
  else
2520
2565
  ending = find_scanner_event(:@tstring_end)
2521
- xstring.merge!(type: :xstring_literal, end: ending[:end], ec: ending[:ec])
2566
+ xstring.merge!(type: :xstring_literal, el: ending[:el], ec: ending[:ec])
2522
2567
  end
2523
2568
  end
2524
2569
 
@@ -2529,7 +2574,7 @@ class Prettier::Parser < Ripper
2529
2574
  find_scanner_event(:@kw, 'yield').merge!(
2530
2575
  type: :yield,
2531
2576
  body: [args_add_block],
2532
- end: args_add_block[:end],
2577
+ el: args_add_block[:el],
2533
2578
  ec: args_add_block[:ec]
2534
2579
  )
2535
2580
  end
@@ -2550,24 +2595,3 @@ class Prettier::Parser < Ripper
2550
2595
  find_scanner_event(:@kw, 'super').merge!(type: :zsuper)
2551
2596
  end
2552
2597
  end
2553
-
2554
- # If this is the main file we're executing, then most likely this is being
2555
- # executed from the parser.js spawn. In that case, read the ruby source from
2556
- # stdin and report back the AST over stdout.
2557
-
2558
- if $0 == __FILE__
2559
- builder = Prettier::Parser.new($stdin.read)
2560
- response = builder.parse
2561
-
2562
- if !response || builder.error?
2563
- warn(
2564
- '@prettier/plugin-ruby encountered an error when attempting to parse ' \
2565
- 'the ruby source. This usually means there was a syntax error in the ' \
2566
- 'file in question. You can verify by running `ruby -i [path/to/file]`.'
2567
- )
2568
-
2569
- exit 1
2570
- end
2571
-
2572
- puts JSON.fast_generate(response)
2573
- end