prettier 1.2.2 → 1.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
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
@@ -6,30 +6,41 @@ const {
6
6
  line,
7
7
  join,
8
8
  softline
9
- } = require("../prettier");
10
- const { literal } = require("../utils");
9
+ } = require("../../prettier");
10
+ const { literal } = require("../../utils");
11
11
 
12
- // You can't skip the parentheses if you have the `and` or `or` operator,
13
- // because they have low enough operator precedence that you need to explicitly
14
- // keep them in there.
12
+ // You can't skip the parentheses if you have comments or certain operators with
13
+ // lower precedence than the return keyword.
15
14
  const canSkipParens = (args) => {
16
15
  const stmts = args.body[0].body[0];
16
+
17
+ // return(
18
+ // # a
19
+ // b
20
+ // )
17
21
  if (stmts.comments) {
18
22
  return false;
19
23
  }
20
24
 
21
25
  const stmt = stmts.body[0];
22
- return stmt.type !== "binary" || !["and", "or"].includes(stmt.body[1]);
26
+
27
+ // return (a or b)
28
+ if (stmt.type === "binary" && ["and", "or"].includes(stmt.body[1])) {
29
+ return false;
30
+ }
31
+
32
+ // return (not a)
33
+ if (stmt.type === "unary" && stmt.oper === "not") {
34
+ return false;
35
+ }
36
+
37
+ return true;
23
38
  };
24
39
 
25
40
  const printReturn = (path, opts, print) => {
26
41
  let args = path.getValue().body[0].body[0];
27
42
  let steps = ["body", 0, "body", 0];
28
43
 
29
- if (!args) {
30
- return "return";
31
- }
32
-
33
44
  if (args.body.length === 1) {
34
45
  // If the body of the return contains parens, then just skip directly to the
35
46
  // content of the parens so that we can skip printing parens if we don't
@@ -10,7 +10,7 @@ const {
10
10
  literalline,
11
11
  softline,
12
12
  trim
13
- } = require("../prettier");
13
+ } = require("../../prettier");
14
14
 
15
15
  function printBodyStmt(path, opts, print) {
16
16
  const [stmts, rescue, elseClause, ensure] = path.getValue().body;
@@ -30,6 +30,7 @@ function printBodyStmt(path, opts, print) {
30
30
 
31
31
  if (elseClause) {
32
32
  // Before Ruby 2.6, this piece of bodystmt was an explicit "else" node
33
+ /* istanbul ignore next */
33
34
  const stmts =
34
35
  elseClause.type === "else"
35
36
  ? path.call(print, "body", 2, "body", 0)
@@ -69,11 +70,7 @@ function printParen(path, opts, print) {
69
70
  }
70
71
 
71
72
  return group(
72
- concat([
73
- "(",
74
- indent(concat([softline, contentDoc])),
75
- concat([softline, ")"])
76
- ])
73
+ concat(["(", indent(concat([softline, contentDoc])), softline, ")"])
77
74
  );
78
75
  }
79
76
 
@@ -82,6 +79,9 @@ module.exports = {
82
79
  const { body } = path.getValue();
83
80
  return concat([trim, "__END__", literalline, body]);
84
81
  },
82
+ "@comment"(path, opts, _print) {
83
+ return opts.printer.printComment(path);
84
+ },
85
85
  bodystmt: printBodyStmt,
86
86
  paren: printParen,
87
87
  program: (path, opts, print) =>
@@ -123,12 +123,12 @@ module.exports = {
123
123
  if (lineNo === null) {
124
124
  parts.push(printed);
125
125
  } else if (
126
- stmt.start - lineNo > 1 ||
126
+ stmt.sl - lineNo > 1 ||
127
127
  [stmt.type, stmts[index - 1].type].includes("access_ctrl")
128
128
  ) {
129
129
  parts.push(hardline, hardline, printed);
130
130
  } else if (
131
- stmt.start !== lineNo ||
131
+ stmt.sl !== lineNo ||
132
132
  path.getParentNode().type !== "string_embexpr"
133
133
  ) {
134
134
  parts.push(hardline, printed);
@@ -136,7 +136,7 @@ module.exports = {
136
136
  parts.push("; ", printed);
137
137
  }
138
138
 
139
- lineNo = stmt.end;
139
+ lineNo = stmt.el;
140
140
  });
141
141
 
142
142
  return concat(parts);
@@ -4,9 +4,10 @@ const {
4
4
  hardline,
5
5
  indent,
6
6
  literalline,
7
+ removeLines,
7
8
  softline,
8
9
  join
9
- } = require("../prettier");
10
+ } = require("../../prettier");
10
11
 
11
12
  // If there is some part of this string that matches an escape sequence or that
12
13
  // contains the interpolation pattern ("#{"), then we are locked into whichever
@@ -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,5 +1,5 @@
1
- const { align, concat, group, join, line } = require("../prettier");
2
- const { literal } = require("../utils");
1
+ const { align, concat, group, join, line } = require("../../prettier");
2
+ const { literal } = require("../../utils");
3
3
 
4
4
  function printSuper(path, opts, print) {
5
5
  const args = path.getValue().body[0];
@@ -5,7 +5,7 @@ const {
5
5
  group,
6
6
  join,
7
7
  line
8
- } = require("../prettier");
8
+ } = require("../../prettier");
9
9
 
10
10
  function printUndefSymbol(path, opts, print) {
11
11
  const node = path.getValue();
@@ -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("ruby", 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.sc;
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.ec;
31
+ }
32
+
33
+ module.exports = {
34
+ parse,
35
+ astFormat: "ruby",
36
+ hasPragma,
37
+ locStart,
38
+ locEnd
39
+ };
@@ -14,7 +14,7 @@ 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
20
  module Prettier; end
@@ -22,6 +22,10 @@ module Prettier; end
22
22
  class Prettier::Parser < Ripper
23
23
  attr_reader :source, :lines, :scanner_events, :line_counts
24
24
 
25
+ # This is an attr_accessor so Stmts objects can grab comments out of this
26
+ # array and attach them to themselves.
27
+ attr_accessor :comments
28
+
25
29
  def initialize(source, *args)
26
30
  super(source, *args)
27
31
 
@@ -40,6 +44,13 @@ class Prettier::Parser < Ripper
40
44
  @source.lines.each { |line| @line_counts << @line_counts.last + line.size }
41
45
  end
42
46
 
47
+ def self.parse(source)
48
+ builder = new(source)
49
+
50
+ response = builder.parse
51
+ response unless builder.error?
52
+ end
53
+
43
54
  private
44
55
 
45
56
  # This represents the current place in the source string that we've gotten to
@@ -89,14 +100,14 @@ class Prettier::Parser < Ripper
89
100
 
90
101
  (SCANNER_EVENTS - defined).each do |event|
91
102
  define_method(:"on_#{event}") do |value|
92
- char_end = char_pos + value.size
103
+ ec = char_pos + value.size
93
104
  node = {
94
105
  type: :"@#{event}",
95
106
  body: value,
96
- start: lineno,
97
- end: lineno,
98
- char_start: char_pos,
99
- char_end: char_end
107
+ sl: lineno,
108
+ el: lineno,
109
+ sc: char_pos,
110
+ ec: ec
100
111
  }
101
112
 
102
113
  scanner_events << node
@@ -118,10 +129,11 @@ class Prettier::Parser < Ripper
118
129
  @comments << {
119
130
  type: :@comment,
120
131
  value: value[1..-1].chomp.force_encoding('UTF-8'),
121
- start: lineno,
122
- end: lineno,
123
- char_start: char_pos,
124
- char_end: char_pos + value.length - 1
132
+ inline: value.strip != lines[lineno - 1],
133
+ sl: lineno,
134
+ el: lineno,
135
+ sc: char_pos,
136
+ ec: char_pos + value.length - 1
125
137
  }
126
138
  end
127
139
 
@@ -138,10 +150,10 @@ class Prettier::Parser < Ripper
138
150
  {
139
151
  type: :ignored_nl,
140
152
  body: nil,
141
- start: lineno,
142
- end: lineno,
143
- char_start: char_pos,
144
- char_end: char_pos
153
+ sl: lineno,
154
+ el: lineno,
155
+ sc: char_pos,
156
+ ec: char_pos
145
157
  }
146
158
  end
147
159
 
@@ -186,16 +198,13 @@ class Prettier::Parser < Ripper
186
198
  beging = find_scanner_event(:@lbrace)
187
199
  ending = find_scanner_event(:@rbrace)
188
200
 
189
- stmts.bind(
190
- find_next_statement_start(beging[:char_end]),
191
- ending[:char_start]
192
- )
201
+ stmts.bind(find_next_statement_start(beging[:ec]), ending[:sc])
193
202
 
194
203
  find_scanner_event(:@kw, 'BEGIN').merge!(
195
204
  type: :BEGIN,
196
205
  body: [beging, stmts],
197
- end: ending[:end],
198
- char_end: ending[:char_end]
206
+ el: ending[:el],
207
+ ec: ending[:ec]
199
208
  )
200
209
  end
201
210
 
@@ -213,16 +222,13 @@ class Prettier::Parser < Ripper
213
222
  beging = find_scanner_event(:@lbrace)
214
223
  ending = find_scanner_event(:@rbrace)
215
224
 
216
- stmts.bind(
217
- find_next_statement_start(beging[:char_end]),
218
- ending[:char_start]
219
- )
225
+ stmts.bind(find_next_statement_start(beging[:ec]), ending[:sc])
220
226
 
221
227
  find_scanner_event(:@kw, 'END').merge!(
222
228
  type: :END,
223
229
  body: [beging, stmts],
224
- end: ending[:end],
225
- char_end: ending[:char_end]
230
+ el: ending[:el],
231
+ ec: ending[:ec]
226
232
  )
227
233
  end
228
234
 
@@ -234,16 +240,16 @@ class Prettier::Parser < Ripper
234
240
  def on_alias(left, right)
235
241
  beging = find_scanner_event(:@kw, 'alias')
236
242
 
237
- paren = source[beging[:char_end]...left[:char_start]].include?('(')
243
+ paren = source[beging[:ec]...left[:sc]].include?('(')
238
244
  ending = paren ? find_scanner_event(:@rparen) : right
239
245
 
240
246
  {
241
247
  type: :alias,
242
248
  body: [left, right],
243
- start: beging[:start],
244
- char_start: beging[:char_start],
245
- end: ending[:end],
246
- char_end: ending[:char_end]
249
+ sl: beging[:sl],
250
+ sc: beging[:sc],
251
+ el: ending[:el],
252
+ ec: ending[:ec]
247
253
  }
248
254
  end
249
255
 
@@ -268,10 +274,10 @@ class Prettier::Parser < Ripper
268
274
  {
269
275
  type: :aref,
270
276
  body: [collection, index],
271
- start: collection[:start],
272
- char_start: collection[:char_start],
273
- end: ending[:end],
274
- char_end: ending[:char_end]
277
+ sl: collection[:sl],
278
+ sc: collection[:sc],
279
+ el: ending[:el],
280
+ ec: ending[:ec]
275
281
  }
276
282
  end
277
283
 
@@ -284,10 +290,10 @@ class Prettier::Parser < Ripper
284
290
  {
285
291
  type: :aref_field,
286
292
  body: [collection, index],
287
- start: collection[:start],
288
- char_start: collection[:char_start],
289
- end: ending[:end],
290
- char_end: ending[:char_end]
293
+ sl: collection[:sl],
294
+ sc: collection[:sc],
295
+ el: ending[:el],
296
+ ec: ending[:ec]
291
297
  }
292
298
  end
293
299
 
@@ -298,10 +304,10 @@ class Prettier::Parser < Ripper
298
304
  {
299
305
  type: :args,
300
306
  body: [],
301
- start: lineno,
302
- char_start: char_pos,
303
- end: lineno,
304
- char_end: char_pos
307
+ sl: lineno,
308
+ sc: char_pos,
309
+ el: lineno,
310
+ ec: char_pos
305
311
  }
306
312
  end
307
313
 
@@ -313,11 +319,7 @@ class Prettier::Parser < Ripper
313
319
  if args[:body].empty?
314
320
  arg.merge(type: :args, body: [arg])
315
321
  else
316
- args.merge!(
317
- body: args[:body] << arg,
318
- end: arg[:end],
319
- char_end: arg[:char_end]
320
- )
322
+ args.merge!(body: args[:body] << arg, el: arg[:el], ec: arg[:ec])
321
323
  end
322
324
  end
323
325
 
@@ -330,8 +332,8 @@ class Prettier::Parser < Ripper
330
332
  args.merge(
331
333
  type: :args_add_block,
332
334
  body: [args, block],
333
- end: ending[:end],
334
- char_end: ending[:char_end]
335
+ el: ending[:el],
336
+ ec: ending[:ec]
335
337
  )
336
338
  end
337
339
 
@@ -345,10 +347,10 @@ class Prettier::Parser < Ripper
345
347
  {
346
348
  type: :args_add_star,
347
349
  body: [args, part],
348
- start: beging[:start],
349
- char_start: beging[:char_start],
350
- end: ending[:end],
351
- char_end: ending[:char_end]
350
+ sl: beging[:sl],
351
+ sc: beging[:sc],
352
+ el: ending[:el],
353
+ ec: ending[:ec]
352
354
  }
353
355
  end
354
356
 
@@ -367,15 +369,15 @@ class Prettier::Parser < Ripper
367
369
  # If the arguments exceed the ending of the parentheses, then we know we
368
370
  # have a heredoc in the arguments, and we need to use the bounds of the
369
371
  # arguments to determine how large the arg_paren is.
370
- ending = (args && args[:end] > rparen[:end]) ? args : rparen
372
+ ending = (args && args[:el] > rparen[:el]) ? args : rparen
371
373
 
372
374
  {
373
375
  type: :arg_paren,
374
376
  body: [args],
375
- start: beging[:start],
376
- char_start: beging[:char_start],
377
- end: ending[:end],
378
- char_end: ending[:char_end]
377
+ sl: beging[:sl],
378
+ sc: beging[:sc],
379
+ el: ending[:el],
380
+ ec: ending[:ec]
379
381
  }
380
382
  end
381
383
 
@@ -391,20 +393,20 @@ class Prettier::Parser < Ripper
391
393
  {
392
394
  type: :array,
393
395
  body: [contents],
394
- start: beging[:start],
395
- char_start: beging[:char_start],
396
- end: ending[:end],
397
- char_end: ending[:char_end]
396
+ sl: beging[:sl],
397
+ sc: beging[:sc],
398
+ el: ending[:el],
399
+ ec: ending[:ec]
398
400
  }
399
401
  else
400
402
  ending = find_scanner_event(:@tstring_end)
401
- contents[:char_end] = ending[:char_end]
403
+ contents[:ec] = ending[:ec]
402
404
 
403
405
  ending.merge!(
404
406
  type: :array,
405
407
  body: [contents],
406
- start: contents[:start],
407
- char_start: contents[:char_start]
408
+ sl: contents[:sl],
409
+ sc: contents[:sc]
408
410
  )
409
411
  end
410
412
  end
@@ -417,10 +419,10 @@ class Prettier::Parser < Ripper
417
419
  {
418
420
  type: :aryptn,
419
421
  body: [const, preargs, splatarg, postargs],
420
- start: pieces[0][:start],
421
- char_start: pieces[0][:char_start],
422
- end: pieces[-1][:end],
423
- char_end: pieces[-1][:char_end]
422
+ sl: pieces[0][:sl],
423
+ sc: pieces[0][:sc],
424
+ el: pieces[-1][:el],
425
+ ec: pieces[-1][:ec]
424
426
  }
425
427
  end
426
428
 
@@ -431,8 +433,8 @@ class Prettier::Parser < Ripper
431
433
  left.merge(
432
434
  type: :assign,
433
435
  body: [left, right],
434
- end: right[:end],
435
- char_end: right[:char_end]
436
+ el: right[:el],
437
+ ec: right[:ec]
436
438
  )
437
439
  end
438
440
 
@@ -443,10 +445,10 @@ class Prettier::Parser < Ripper
443
445
  {
444
446
  type: :assoc_new,
445
447
  body: [key, value],
446
- start: key[:start],
447
- char_start: key[:char_start],
448
- end: value[:end],
449
- char_end: value[:char_end]
448
+ sl: key[:sl],
449
+ sc: key[:sc],
450
+ el: value[:el],
451
+ ec: value[:ec]
450
452
  }
451
453
  end
452
454
 
@@ -456,8 +458,8 @@ class Prettier::Parser < Ripper
456
458
  find_scanner_event(:@op, '**').merge!(
457
459
  type: :assoc_splat,
458
460
  body: [contents],
459
- end: contents[:end],
460
- char_end: contents[:char_end]
461
+ el: contents[:el],
462
+ ec: contents[:ec]
461
463
  )
462
464
  end
463
465
 
@@ -469,10 +471,10 @@ class Prettier::Parser < Ripper
469
471
  {
470
472
  type: :assoclist_from_args,
471
473
  body: assocs,
472
- start: assocs[0][:start],
473
- char_start: assocs[0][:char_start],
474
- end: assocs[-1][:end],
475
- char_end: assocs[-1][:char_end]
474
+ sl: assocs[0][:sl],
475
+ sc: assocs[0][:sc],
476
+ el: assocs[-1][:el],
477
+ ec: assocs[-1][:ec]
476
478
  }
477
479
  end
478
480
 
@@ -484,10 +486,10 @@ class Prettier::Parser < Ripper
484
486
  {
485
487
  type: :bare_assoc_hash,
486
488
  body: assoc_news,
487
- start: assoc_news[0][:start],
488
- char_start: assoc_news[0][:char_start],
489
- end: assoc_news[-1][:end],
490
- char_end: assoc_news[-1][:char_end]
489
+ sl: assoc_news[0][:sl],
490
+ sc: assoc_news[0][:sc],
491
+ el: assoc_news[-1][:el],
492
+ ec: assoc_news[-1][:ec]
491
493
  }
492
494
  end
493
495
 
@@ -495,20 +497,20 @@ class Prettier::Parser < Ripper
495
497
  # It includes a bodystmt event that has all of the consequent clauses.
496
498
  def on_begin(bodystmt)
497
499
  beging = find_scanner_event(:@kw, 'begin')
498
- char_end =
500
+ ec =
499
501
  if bodystmt[:body][1..-1].any?
500
- bodystmt[:char_end]
502
+ bodystmt[:ec]
501
503
  else
502
- find_scanner_event(:@kw, 'end')[:char_end]
504
+ find_scanner_event(:@kw, 'end')[:ec]
503
505
  end
504
506
 
505
- bodystmt.bind(beging[:char_end], char_end)
507
+ bodystmt.bind(beging[:ec], ec)
506
508
 
507
509
  beging.merge!(
508
510
  type: :begin,
509
511
  body: [bodystmt],
510
- end: bodystmt[:end],
511
- char_end: bodystmt[:char_end]
512
+ el: bodystmt[:el],
513
+ ec: bodystmt[:ec]
512
514
  )
513
515
  end
514
516
 
@@ -518,10 +520,10 @@ class Prettier::Parser < Ripper
518
520
  {
519
521
  type: :binary,
520
522
  body: [left, oper, right],
521
- start: left[:start],
522
- char_start: left[:char_start],
523
- end: right[:end],
524
- char_end: right[:char_end]
523
+ sl: left[:sl],
524
+ sc: left[:sc],
525
+ el: right[:el],
526
+ ec: right[:ec]
525
527
  }
526
528
  end
527
529
 
@@ -531,7 +533,7 @@ class Prettier::Parser < Ripper
531
533
  index =
532
534
  scanner_events.rindex do |event|
533
535
  event[:type] == :@op && %w[| ||].include?(event[:body]) &&
534
- event[:char_start] < params[:char_start]
536
+ event[:sc] < params[:sc]
535
537
  end
536
538
 
537
539
  beging = scanner_events[index]
@@ -540,10 +542,10 @@ class Prettier::Parser < Ripper
540
542
  {
541
543
  type: :block_var,
542
544
  body: [params, locals],
543
- start: beging[:start],
544
- char_start: beging[:char_start],
545
- end: ending[:end],
546
- char_end: ending[:char_end]
545
+ sl: beging[:sl],
546
+ sc: beging[:sc],
547
+ el: ending[:el],
548
+ ec: ending[:ec]
547
549
  }
548
550
  end
549
551
 
@@ -553,8 +555,8 @@ class Prettier::Parser < Ripper
553
555
  find_scanner_event(:@op, '&').merge!(
554
556
  type: :blockarg,
555
557
  body: [ident],
556
- end: ident[:end],
557
- char_end: ident[:char_end]
558
+ el: ident[:el],
559
+ ec: ident[:ec]
558
560
  )
559
561
  end
560
562
 
@@ -562,21 +564,18 @@ class Prettier::Parser < Ripper
562
564
  # doesn't necessarily know where it started. So the parent node needs to
563
565
  # report back down into this one where it goes.
564
566
  class BodyStmt < SimpleDelegator
565
- def bind(char_start, char_end)
566
- merge!(char_start: char_start, char_end: char_end)
567
+ def bind(sc, ec)
568
+ merge!(sc: sc, ec: ec)
567
569
  parts = self[:body]
568
570
 
569
571
  # Here we're going to determine the bounds for the stmts
570
572
  consequent = parts[1..-1].compact.first
571
- self[:body][0].bind(
572
- char_start,
573
- consequent ? consequent[:char_start] : char_end
574
- )
573
+ self[:body][0].bind(sc, consequent ? consequent[:sc] : ec)
575
574
 
576
575
  # Next we're going to determine the rescue clause if there is one
577
576
  if parts[1]
578
577
  consequent = parts[2..-1].compact.first
579
- self[:body][1].bind_end(consequent ? consequent[:char_start] : char_end)
578
+ self[:body][1].bind_end(consequent ? consequent[:sc] : ec)
580
579
  end
581
580
  end
582
581
  end
@@ -587,10 +586,10 @@ class Prettier::Parser < Ripper
587
586
  BodyStmt.new(
588
587
  type: :bodystmt,
589
588
  body: [stmts, rescued, ensured, elsed],
590
- start: lineno,
591
- char_start: char_pos,
592
- end: lineno,
593
- char_end: char_pos
589
+ sl: lineno,
590
+ sc: char_pos,
591
+ el: lineno,
592
+ ec: char_pos
594
593
  )
595
594
  end
596
595
 
@@ -602,15 +601,15 @@ class Prettier::Parser < Ripper
602
601
  beging = find_scanner_event(:@lbrace)
603
602
  ending = find_scanner_event(:@rbrace)
604
603
 
605
- stmts.bind((block_var || beging)[:char_end], ending[:char_start])
604
+ stmts.bind((block_var || beging)[:ec], ending[:sc])
606
605
 
607
606
  {
608
607
  type: :brace_block,
609
608
  body: [block_var, stmts],
610
- start: beging[:start],
611
- char_start: beging[:char_start],
612
- end: ending[:end],
613
- char_end: ending[:char_end]
609
+ sl: beging[:sl],
610
+ sc: beging[:sc],
611
+ el: ending[:el],
612
+ ec: ending[:ec]
614
613
  }
615
614
  end
616
615
 
@@ -630,8 +629,8 @@ class Prettier::Parser < Ripper
630
629
  beging.merge!(
631
630
  type: :break,
632
631
  body: [args_add_block],
633
- end: args_add_block[:end],
634
- char_end: args_add_block[:char_end]
632
+ el: args_add_block[:el],
633
+ ec: args_add_block[:ec]
635
634
  )
636
635
  end
637
636
 
@@ -647,10 +646,6 @@ class Prettier::Parser < Ripper
647
646
  # foo.(1, 2, 3)
648
647
  #
649
648
  def on_call(receiver, oper, sending)
650
- # Make sure we take the operator out of the scanner events so that it
651
- # doesn't get confused for a unary operator later.
652
- scanner_events.delete(oper)
653
-
654
649
  ending = sending
655
650
 
656
651
  if sending == :call
@@ -664,10 +659,10 @@ class Prettier::Parser < Ripper
664
659
  {
665
660
  type: :call,
666
661
  body: [receiver, oper, sending],
667
- start: receiver[:start],
668
- char_start: receiver[:char_start],
669
- end: ending[:end],
670
- char_end: ending[:char_end]
662
+ sl: receiver[:sl],
663
+ sc: receiver[:sc],
664
+ el: ending[:el],
665
+ ec: ending[:ec]
671
666
  }
672
667
  end
673
668
 
@@ -685,8 +680,8 @@ class Prettier::Parser < Ripper
685
680
 
686
681
  beging.merge!(
687
682
  body: [switch, consequent],
688
- end: consequent[:end],
689
- char_end: consequent[:char_end]
683
+ el: consequent[:el],
684
+ ec: consequent[:ec]
690
685
  )
691
686
  end
692
687
 
@@ -720,17 +715,17 @@ class Prettier::Parser < Ripper
720
715
  ending = find_scanner_event(:@kw, 'end')
721
716
 
722
717
  bodystmt.bind(
723
- find_next_statement_start((superclass || const)[:char_end]),
724
- ending[:char_start]
718
+ find_next_statement_start((superclass || const)[:ec]),
719
+ ending[:sc]
725
720
  )
726
721
 
727
722
  {
728
723
  type: :class,
729
724
  body: [const, superclass, bodystmt],
730
- start: beging[:start],
731
- char_start: beging[:char_start],
732
- end: ending[:end],
733
- char_end: ending[:char_end]
725
+ sl: beging[:sl],
726
+ sc: beging[:sc],
727
+ el: ending[:el],
728
+ ec: ending[:ec]
734
729
  }
735
730
  end
736
731
 
@@ -741,10 +736,10 @@ class Prettier::Parser < Ripper
741
736
  {
742
737
  type: :command,
743
738
  body: [ident, args],
744
- start: ident[:start],
745
- char_start: ident[:char_start],
746
- end: args[:end],
747
- char_end: args[:char_end]
739
+ sl: ident[:sl],
740
+ sc: ident[:sc],
741
+ el: args[:el],
742
+ ec: args[:ec]
748
743
  }
749
744
  end
750
745
 
@@ -753,20 +748,15 @@ class Prettier::Parser < Ripper
753
748
  # of the method, the operator being used to send the method, the name of
754
749
  # the method, and the arguments being passed to the method.
755
750
  def on_command_call(receiver, oper, ident, args)
756
- # Make sure we take the operator out of the scanner events so that it
757
- # doesn't get confused for a unary operator later.
758
- scanner_events.delete(oper)
759
-
760
- # Grab the ending from either the arguments or the method being sent
761
751
  ending = args || ident
762
752
 
763
753
  {
764
754
  type: :command_call,
765
755
  body: [receiver, oper, ident, args],
766
- start: receiver[:start],
767
- char_start: receiver[:char_start],
768
- end: ending[:end],
769
- char_end: ending[:char_end]
756
+ sl: receiver[:sl],
757
+ sc: receiver[:sc],
758
+ el: ending[:el],
759
+ ec: ending[:ec]
770
760
  }
771
761
  end
772
762
 
@@ -780,10 +770,10 @@ class Prettier::Parser < Ripper
780
770
  {
781
771
  type: :const_path_field,
782
772
  body: [left, const],
783
- start: left[:start],
784
- char_start: left[:char_start],
785
- end: const[:end],
786
- char_end: const[:char_end]
773
+ sl: left[:sl],
774
+ sc: left[:sc],
775
+ el: const[:el],
776
+ ec: const[:ec]
787
777
  }
788
778
  end
789
779
 
@@ -797,10 +787,10 @@ class Prettier::Parser < Ripper
797
787
  {
798
788
  type: :const_path_ref,
799
789
  body: [left, const],
800
- start: left[:start],
801
- char_start: left[:char_start],
802
- end: const[:end],
803
- char_end: const[:char_end]
790
+ sl: left[:sl],
791
+ sc: left[:sc],
792
+ el: const[:el],
793
+ ec: const[:ec]
804
794
  }
805
795
  end
806
796
 
@@ -850,33 +840,30 @@ class Prettier::Parser < Ripper
850
840
  {
851
841
  type: :defsl,
852
842
  body: [ident, params, bodystmt],
853
- start: beging[:start],
854
- char_start: beging[:char_start],
855
- end: bodystmt[:end],
856
- char_end: bodystmt[:char_end]
843
+ sl: beging[:sl],
844
+ sc: beging[:sc],
845
+ el: bodystmt[:el],
846
+ ec: bodystmt[:ec]
857
847
  }
858
848
  )
859
849
  end
860
850
 
861
851
  if params[:type] == :params && !params[:body].any?
862
- location = ident[:char_end]
863
- params.merge!(char_start: location, char_end: location)
852
+ location = ident[:ec]
853
+ params.merge!(sc: location, ec: location)
864
854
  end
865
855
 
866
856
  ending = find_scanner_event(:@kw, 'end')
867
857
 
868
- bodystmt.bind(
869
- find_next_statement_start(params[:char_end]),
870
- ending[:char_start]
871
- )
858
+ bodystmt.bind(find_next_statement_start(params[:ec]), ending[:sc])
872
859
 
873
860
  {
874
861
  type: :def,
875
862
  body: [ident, params, bodystmt],
876
- start: beging[:start],
877
- char_start: beging[:char_start],
878
- end: ending[:end],
879
- char_end: ending[:char_end]
863
+ sl: beging[:sl],
864
+ sc: beging[:sc],
865
+ el: ending[:el],
866
+ ec: ending[:ec]
880
867
  }
881
868
  end
882
869
 
@@ -901,25 +888,22 @@ class Prettier::Parser < Ripper
901
888
  scanner_events.delete(ident)
902
889
 
903
890
  if params[:type] == :params && !params[:body].any?
904
- location = ident[:char_end]
905
- params.merge!(char_start: location, char_end: location)
891
+ location = ident[:ec]
892
+ params.merge!(sc: location, ec: location)
906
893
  end
907
894
 
908
895
  beging = find_scanner_event(:@kw, 'def')
909
896
  ending = find_scanner_event(:@kw, 'end')
910
897
 
911
- bodystmt.bind(
912
- find_next_statement_start(params[:char_end]),
913
- ending[:char_start]
914
- )
898
+ bodystmt.bind(find_next_statement_start(params[:ec]), ending[:sc])
915
899
 
916
900
  {
917
901
  type: :defs,
918
902
  body: [target, oper, ident, params, bodystmt],
919
- start: beging[:start],
920
- char_start: beging[:char_start],
921
- end: ending[:end],
922
- char_end: ending[:char_end]
903
+ sl: beging[:sl],
904
+ sc: beging[:sc],
905
+ el: ending[:el],
906
+ ec: ending[:ec]
923
907
  }
924
908
  end
925
909
 
@@ -930,14 +914,14 @@ class Prettier::Parser < Ripper
930
914
  def on_defined(value)
931
915
  beging = find_scanner_event(:@kw, 'defined?')
932
916
 
933
- paren = source[beging[:char_end]...value[:char_start]].include?('(')
917
+ paren = source[beging[:ec]...value[:sc]].include?('(')
934
918
  ending = paren ? find_scanner_event(:@rparen) : value
935
919
 
936
920
  beging.merge!(
937
921
  type: :defined,
938
922
  body: [value],
939
- end: ending[:end],
940
- char_end: ending[:char_end]
923
+ el: ending[:el],
924
+ ec: ending[:ec]
941
925
  )
942
926
  end
943
927
 
@@ -949,15 +933,15 @@ class Prettier::Parser < Ripper
949
933
  beging = find_scanner_event(:@kw, 'do')
950
934
  ending = find_scanner_event(:@kw, 'end')
951
935
 
952
- bodystmt.bind((block_var || beging)[:char_end], ending[:char_start])
936
+ bodystmt.bind((block_var || beging)[:ec], ending[:sc])
953
937
 
954
938
  {
955
939
  type: :do_block,
956
940
  body: [block_var, bodystmt],
957
- start: beging[:start],
958
- char_start: beging[:char_start],
959
- end: ending[:end],
960
- char_end: ending[:char_end]
941
+ sl: beging[:sl],
942
+ sc: beging[:sc],
943
+ el: ending[:el],
944
+ ec: ending[:ec]
961
945
  }
962
946
  end
963
947
 
@@ -973,10 +957,10 @@ class Prettier::Parser < Ripper
973
957
  {
974
958
  type: :dot2,
975
959
  body: [left, right],
976
- start: beging[:start],
977
- char_start: beging[:char_start],
978
- end: ending[:end],
979
- char_end: ending[:char_end]
960
+ sl: beging[:sl],
961
+ sc: beging[:sc],
962
+ el: ending[:el],
963
+ ec: ending[:ec]
980
964
  }
981
965
  end
982
966
 
@@ -992,10 +976,10 @@ class Prettier::Parser < Ripper
992
976
  {
993
977
  type: :dot3,
994
978
  body: [left, right],
995
- start: beging[:start],
996
- char_start: beging[:char_start],
997
- end: ending[:end],
998
- char_end: ending[:char_end]
979
+ sl: beging[:sl],
980
+ sc: beging[:sc],
981
+ el: ending[:el],
982
+ ec: ending[:ec]
999
983
  }
1000
984
  end
1001
985
 
@@ -1027,8 +1011,8 @@ class Prettier::Parser < Ripper
1027
1011
  type: :dyna_symbol,
1028
1012
  quote: beging[:body][1],
1029
1013
  body: string[:body],
1030
- end: ending[:end],
1031
- char_end: ending[:char_end]
1014
+ el: ending[:el],
1015
+ ec: ending[:ec]
1032
1016
  )
1033
1017
  else
1034
1018
  # A dynamic symbol as a hash key
@@ -1038,10 +1022,10 @@ class Prettier::Parser < Ripper
1038
1022
  string.merge!(
1039
1023
  type: :dyna_symbol,
1040
1024
  quote: ending[:body][0],
1041
- start: beging[:start],
1042
- char_start: beging[:char_start],
1043
- end: ending[:end],
1044
- char_end: ending[:char_end]
1025
+ sl: beging[:sl],
1026
+ sc: beging[:sc],
1027
+ el: ending[:el],
1028
+ ec: ending[:ec]
1045
1029
  )
1046
1030
  end
1047
1031
  end
@@ -1066,15 +1050,15 @@ class Prettier::Parser < Ripper
1066
1050
  beging = find_scanner_event(:@kw, 'else')
1067
1051
  ending = find_else_ending
1068
1052
 
1069
- stmts.bind(beging[:char_end], ending[:char_start])
1053
+ stmts.bind(beging[:ec], ending[:sc])
1070
1054
 
1071
1055
  {
1072
1056
  type: :else,
1073
1057
  body: [stmts],
1074
- start: beging[:start],
1075
- char_start: beging[:char_start],
1076
- end: ending[:end],
1077
- char_end: ending[:char_end]
1058
+ sl: beging[:sl],
1059
+ sc: beging[:sc],
1060
+ el: ending[:el],
1061
+ ec: ending[:ec]
1078
1062
  }
1079
1063
  end
1080
1064
 
@@ -1086,15 +1070,15 @@ class Prettier::Parser < Ripper
1086
1070
  beging = find_scanner_event(:@kw, 'elsif')
1087
1071
  ending = consequent || find_scanner_event(:@kw, 'end')
1088
1072
 
1089
- stmts.bind(predicate[:char_end], ending[:char_start])
1073
+ stmts.bind(predicate[:ec], ending[:sc])
1090
1074
 
1091
1075
  {
1092
1076
  type: :elsif,
1093
1077
  body: [predicate, stmts, consequent],
1094
- start: beging[:start],
1095
- char_start: beging[:char_start],
1096
- end: ending[:end],
1097
- char_end: ending[:char_end]
1078
+ sl: beging[:sl],
1079
+ sc: beging[:sc],
1080
+ el: ending[:el],
1081
+ ec: ending[:ec]
1098
1082
  }
1099
1083
  end
1100
1084
 
@@ -1104,12 +1088,7 @@ class Prettier::Parser < Ripper
1104
1088
  # and add to it as we get content. It always starts with this scanner
1105
1089
  # event, so here we'll initialize the current embdoc.
1106
1090
  def on_embdoc_beg(value)
1107
- @embdoc = {
1108
- type: :@embdoc,
1109
- value: value,
1110
- start: lineno,
1111
- char_start: char_pos
1112
- }
1091
+ @embdoc = { type: :@embdoc, value: value, sl: lineno, sc: char_pos }
1113
1092
  end
1114
1093
 
1115
1094
  # This is a scanner event that gets hit when we're inside an embdoc and
@@ -1128,8 +1107,8 @@ class Prettier::Parser < Ripper
1128
1107
  @comments <<
1129
1108
  @embdoc.merge!(
1130
1109
  value: @embdoc[:value] << value.chomp,
1131
- end: lineno,
1132
- char_end: char_pos + value.length - 1
1110
+ el: lineno,
1111
+ ec: char_pos + value.length - 1
1133
1112
  )
1134
1113
 
1135
1114
  @embdoc = nil
@@ -1148,18 +1127,15 @@ class Prettier::Parser < Ripper
1148
1127
  end
1149
1128
 
1150
1129
  ending = scanner_events[index]
1151
- stmts.bind(
1152
- find_next_statement_start(beging[:char_end]),
1153
- ending[:char_start]
1154
- )
1130
+ stmts.bind(find_next_statement_start(beging[:ec]), ending[:sc])
1155
1131
 
1156
1132
  {
1157
1133
  type: :ensure,
1158
1134
  body: [beging, stmts],
1159
- start: beging[:start],
1160
- char_start: beging[:char_start],
1161
- end: ending[:end],
1162
- char_end: ending[:char_end]
1135
+ sl: beging[:sl],
1136
+ sc: beging[:sc],
1137
+ el: ending[:el],
1138
+ ec: ending[:ec]
1163
1139
  }
1164
1140
  end
1165
1141
 
@@ -1187,10 +1163,10 @@ class Prettier::Parser < Ripper
1187
1163
  {
1188
1164
  type: :field,
1189
1165
  body: [left, oper, right],
1190
- start: left[:start],
1191
- char_start: left[:char_start],
1192
- end: right[:end],
1193
- char_end: right[:char_end]
1166
+ sl: left[:sl],
1167
+ sc: left[:sc],
1168
+ el: right[:el],
1169
+ ec: right[:ec]
1194
1170
  }
1195
1171
  end
1196
1172
 
@@ -1200,15 +1176,13 @@ class Prettier::Parser < Ripper
1200
1176
  beging = const || find_scanner_event(:@lbracket)
1201
1177
  ending = find_scanner_event(:@rbracket)
1202
1178
 
1203
- pieces = [const, presplat, *args, postsplat].compact
1204
-
1205
1179
  {
1206
1180
  type: :fndptn,
1207
1181
  body: [const, presplat, args, postsplat],
1208
- start: beging[:start],
1209
- char_start: beging[:char_start],
1210
- end: ending[:end],
1211
- char_end: ending[:char_end]
1182
+ sl: beging[:sl],
1183
+ sc: beging[:sc],
1184
+ el: ending[:el],
1185
+ ec: ending[:ec]
1212
1186
  }
1213
1187
  end
1214
1188
 
@@ -1220,15 +1194,15 @@ class Prettier::Parser < Ripper
1220
1194
  beging = find_scanner_event(:@kw, 'for')
1221
1195
  ending = find_scanner_event(:@kw, 'end')
1222
1196
 
1223
- stmts.bind(enumerable[:char_end], ending[:char_start])
1197
+ stmts.bind(enumerable[:ec], ending[:sc])
1224
1198
 
1225
1199
  {
1226
1200
  type: :for,
1227
1201
  body: [ident, enumerable, stmts],
1228
- start: beging[:start],
1229
- char_start: beging[:char_start],
1230
- end: ending[:end],
1231
- char_end: ending[:char_end]
1202
+ sl: beging[:sl],
1203
+ sc: beging[:sc],
1204
+ el: ending[:el],
1205
+ ec: ending[:ec]
1232
1206
  }
1233
1207
  end
1234
1208
 
@@ -1242,19 +1216,16 @@ class Prettier::Parser < Ripper
1242
1216
  if assoclist_from_args
1243
1217
  # Here we're going to expand out the location information for the assocs
1244
1218
  # node so that it can grab up any remaining comments inside the hash.
1245
- assoclist_from_args.merge!(
1246
- char_start: beging[:char_end],
1247
- char_end: ending[:char_start]
1248
- )
1219
+ assoclist_from_args.merge!(sc: beging[:ec], ec: ending[:sc])
1249
1220
  end
1250
1221
 
1251
1222
  {
1252
1223
  type: :hash,
1253
1224
  body: [assoclist_from_args],
1254
- start: beging[:start],
1255
- char_start: beging[:char_start],
1256
- end: ending[:end],
1257
- char_end: ending[:char_end]
1225
+ sl: beging[:sl],
1226
+ sc: beging[:sc],
1227
+ el: ending[:el],
1228
+ ec: ending[:ec]
1258
1229
  }
1259
1230
  end
1260
1231
 
@@ -1266,10 +1237,10 @@ class Prettier::Parser < Ripper
1266
1237
  # printer through our embed function.
1267
1238
  def on_heredoc_beg(beging)
1268
1239
  location = {
1269
- start: lineno,
1270
- end: lineno,
1271
- char_start: char_pos,
1272
- char_end: char_pos + beging.length + 1
1240
+ sl: lineno,
1241
+ el: lineno,
1242
+ sc: char_pos,
1243
+ ec: char_pos + beging.length + 1
1273
1244
  }
1274
1245
 
1275
1246
  # Here we're going to artificially create an extra node type so that if
@@ -1291,7 +1262,7 @@ class Prettier::Parser < Ripper
1291
1262
 
1292
1263
  # This is a scanner event that represents the end of the heredoc.
1293
1264
  def on_heredoc_end(ending)
1294
- @heredocs[-1].merge!(ending: ending.chomp, end: lineno, char_end: char_pos)
1265
+ @heredocs[-1].merge!(ending: ending.chomp, el: lineno, ec: char_pos)
1295
1266
  end
1296
1267
 
1297
1268
  # hshptn is a parser event that represents matching against a hash pattern
@@ -1302,10 +1273,10 @@ class Prettier::Parser < Ripper
1302
1273
  {
1303
1274
  type: :hshptn,
1304
1275
  body: [const, kw, kwrest],
1305
- start: pieces[0][:start],
1306
- char_start: pieces[0][:char_start],
1307
- end: pieces[-1][:end],
1308
- char_end: pieces[-1][:char_end]
1276
+ sl: pieces[0][:sl],
1277
+ sc: pieces[0][:sc],
1278
+ el: pieces[-1][:el],
1279
+ ec: pieces[-1][:ec]
1309
1280
  }
1310
1281
  end
1311
1282
 
@@ -1316,15 +1287,15 @@ class Prettier::Parser < Ripper
1316
1287
  beging = find_scanner_event(:@kw, 'if')
1317
1288
  ending = consequent || find_scanner_event(:@kw, 'end')
1318
1289
 
1319
- stmts.bind(predicate[:char_end], ending[:char_start])
1290
+ stmts.bind(predicate[:ec], ending[:sc])
1320
1291
 
1321
1292
  {
1322
1293
  type: :if,
1323
1294
  body: [predicate, stmts, consequent],
1324
- start: beging[:start],
1325
- char_start: beging[:char_start],
1326
- end: ending[:end],
1327
- char_end: ending[:char_end]
1295
+ sl: beging[:sl],
1296
+ sc: beging[:sc],
1297
+ el: ending[:el],
1298
+ ec: ending[:ec]
1328
1299
  }
1329
1300
  end
1330
1301
 
@@ -1335,8 +1306,8 @@ class Prettier::Parser < Ripper
1335
1306
  predicate.merge(
1336
1307
  type: :ifop,
1337
1308
  body: [predicate, truthy, falsy],
1338
- end: falsy[:end],
1339
- char_end: falsy[:char_end]
1309
+ el: falsy[:el],
1310
+ ec: falsy[:ec]
1340
1311
  )
1341
1312
  end
1342
1313
 
@@ -1349,10 +1320,10 @@ class Prettier::Parser < Ripper
1349
1320
  {
1350
1321
  type: :if_mod,
1351
1322
  body: [predicate, statement],
1352
- start: statement[:start],
1353
- char_start: statement[:char_start],
1354
- end: predicate[:end],
1355
- char_end: predicate[:char_end]
1323
+ sl: statement[:sl],
1324
+ sc: statement[:sc],
1325
+ el: predicate[:el],
1326
+ ec: predicate[:ec]
1356
1327
  }
1357
1328
  end
1358
1329
 
@@ -1366,13 +1337,13 @@ class Prettier::Parser < Ripper
1366
1337
  beging = find_scanner_event(:@kw, 'in')
1367
1338
  ending = consequent || find_scanner_event(:@kw, 'end')
1368
1339
 
1369
- stmts.bind(beging[:char_end], ending[:char_start])
1340
+ stmts.bind(beging[:ec], ending[:sc])
1370
1341
 
1371
1342
  beging.merge!(
1372
1343
  type: :in,
1373
1344
  body: [pattern, stmts, consequent],
1374
- end: ending[:end],
1375
- char_end: ending[:char_end]
1345
+ el: ending[:el],
1346
+ ec: ending[:ec]
1376
1347
  )
1377
1348
  end
1378
1349
 
@@ -1385,8 +1356,8 @@ class Prettier::Parser < Ripper
1385
1356
  oper.merge!(
1386
1357
  type: :kwrest_param,
1387
1358
  body: [ident],
1388
- end: ident[:end],
1389
- char_end: ident[:char_end]
1359
+ el: ident[:el],
1360
+ ec: ident[:ec]
1390
1361
  )
1391
1362
  end
1392
1363
 
@@ -1408,15 +1379,15 @@ class Prettier::Parser < Ripper
1408
1379
  closing = find_scanner_event(:@kw, 'end')
1409
1380
  end
1410
1381
 
1411
- stmts.bind(opening[:char_end], closing[:char_start])
1382
+ stmts.bind(opening[:ec], closing[:sc])
1412
1383
 
1413
1384
  {
1414
1385
  type: :lambda,
1415
1386
  body: [params, stmts],
1416
- start: beging[:start],
1417
- char_start: beging[:char_start],
1418
- end: closing[:end],
1419
- char_end: closing[:char_end]
1387
+ sl: beging[:sl],
1388
+ sc: beging[:sc],
1389
+ el: closing[:el],
1390
+ ec: closing[:ec]
1420
1391
  }
1421
1392
  end
1422
1393
 
@@ -1438,17 +1409,15 @@ class Prettier::Parser < Ripper
1438
1409
  # in which case we need to explicitly track the comma and add it onto the
1439
1410
  # child node.
1440
1411
  def on_massign(left, right)
1441
- if source[left[:char_end]...right[:char_start]].strip.start_with?(',')
1442
- left[:comma] = true
1443
- end
1412
+ left[:comma] = true if source[left[:ec]...right[:sc]].strip.start_with?(',')
1444
1413
 
1445
1414
  {
1446
1415
  type: :massign,
1447
1416
  body: [left, right],
1448
- start: left[:start],
1449
- char_start: left[:char_start],
1450
- end: right[:end],
1451
- char_end: right[:char_end]
1417
+ sl: left[:sl],
1418
+ sc: left[:sc],
1419
+ el: right[:el],
1420
+ ec: right[:ec]
1452
1421
  }
1453
1422
  end
1454
1423
 
@@ -1467,10 +1436,10 @@ class Prettier::Parser < Ripper
1467
1436
  {
1468
1437
  type: :method_add_arg,
1469
1438
  body: [fcall, arg_paren],
1470
- start: fcall[:start],
1471
- char_start: fcall[:char_start],
1472
- end: arg_paren[:end],
1473
- char_end: arg_paren[:char_end]
1439
+ sl: fcall[:sl],
1440
+ sc: fcall[:sc],
1441
+ el: arg_paren[:el],
1442
+ ec: arg_paren[:ec]
1474
1443
  }
1475
1444
  end
1476
1445
 
@@ -1481,10 +1450,10 @@ class Prettier::Parser < Ripper
1481
1450
  {
1482
1451
  type: :method_add_block,
1483
1452
  body: [method_add_arg, block],
1484
- start: method_add_arg[:start],
1485
- char_start: method_add_arg[:char_start],
1486
- end: block[:end],
1487
- char_end: block[:char_end]
1453
+ sl: method_add_arg[:sl],
1454
+ sc: method_add_arg[:sc],
1455
+ el: block[:el],
1456
+ ec: block[:ec]
1488
1457
  }
1489
1458
  end
1490
1459
 
@@ -1495,10 +1464,10 @@ class Prettier::Parser < Ripper
1495
1464
  {
1496
1465
  type: :mlhs,
1497
1466
  body: [],
1498
- start: lineno,
1499
- char_start: char_pos,
1500
- end: lineno,
1501
- char_end: char_pos
1467
+ sl: lineno,
1468
+ sc: char_pos,
1469
+ el: lineno,
1470
+ ec: char_pos
1502
1471
  }
1503
1472
  end
1504
1473
 
@@ -1509,11 +1478,7 @@ class Prettier::Parser < Ripper
1509
1478
  if mlhs[:body].empty?
1510
1479
  part.merge(type: :mlhs, body: [part])
1511
1480
  else
1512
- mlhs.merge!(
1513
- body: mlhs[:body] << part,
1514
- end: part[:end],
1515
- char_end: part[:char_end]
1516
- )
1481
+ mlhs.merge!(body: mlhs[:body] << part, el: part[:el], ec: part[:ec])
1517
1482
  end
1518
1483
  end
1519
1484
 
@@ -1526,8 +1491,8 @@ class Prettier::Parser < Ripper
1526
1491
  mlhs_add_star.merge(
1527
1492
  type: :mlhs_add_post,
1528
1493
  body: [mlhs_add_star, mlhs],
1529
- end: mlhs[:end],
1530
- char_end: mlhs[:char_end]
1494
+ el: mlhs[:el],
1495
+ ec: mlhs[:ec]
1531
1496
  )
1532
1497
  end
1533
1498
 
@@ -1542,10 +1507,10 @@ class Prettier::Parser < Ripper
1542
1507
  {
1543
1508
  type: :mlhs_add_star,
1544
1509
  body: [mlhs, part],
1545
- start: beging[:start],
1546
- char_start: beging[:char_start],
1547
- end: ending[:end],
1548
- char_end: ending[:char_end]
1510
+ sl: beging[:sl],
1511
+ sc: beging[:sc],
1512
+ el: ending[:el],
1513
+ ec: ending[:ec]
1549
1514
  }
1550
1515
  end
1551
1516
 
@@ -1557,17 +1522,17 @@ class Prettier::Parser < Ripper
1557
1522
  beging = find_scanner_event(:@lparen)
1558
1523
  ending = find_scanner_event(:@rparen)
1559
1524
 
1560
- if source[beging[:char_end]...ending[:char_start]].strip.end_with?(',')
1525
+ if source[beging[:ec]...ending[:sc]].strip.end_with?(',')
1561
1526
  contents[:comma] = true
1562
1527
  end
1563
1528
 
1564
1529
  {
1565
1530
  type: :mlhs_paren,
1566
1531
  body: [contents],
1567
- start: beging[:start],
1568
- char_start: beging[:char_start],
1569
- end: ending[:end],
1570
- char_end: ending[:char_end]
1532
+ sl: beging[:sl],
1533
+ sc: beging[:sc],
1534
+ el: ending[:el],
1535
+ ec: ending[:ec]
1571
1536
  }
1572
1537
  end
1573
1538
 
@@ -1578,18 +1543,15 @@ class Prettier::Parser < Ripper
1578
1543
  beging = find_scanner_event(:@kw, 'module')
1579
1544
  ending = find_scanner_event(:@kw, 'end')
1580
1545
 
1581
- bodystmt.bind(
1582
- find_next_statement_start(const[:char_end]),
1583
- ending[:char_start]
1584
- )
1546
+ bodystmt.bind(find_next_statement_start(const[:ec]), ending[:sc])
1585
1547
 
1586
1548
  {
1587
1549
  type: :module,
1588
1550
  body: [const, bodystmt],
1589
- start: beging[:start],
1590
- char_start: beging[:char_start],
1591
- end: ending[:end],
1592
- char_end: ending[:char_end]
1551
+ sl: beging[:sl],
1552
+ sc: beging[:sc],
1553
+ el: ending[:el],
1554
+ ec: ending[:ec]
1593
1555
  }
1594
1556
  end
1595
1557
 
@@ -1601,10 +1563,10 @@ class Prettier::Parser < Ripper
1601
1563
  {
1602
1564
  type: :mrhs,
1603
1565
  body: [],
1604
- start: lineno,
1605
- char_start: char_pos,
1606
- end: lineno,
1607
- char_end: char_pos
1566
+ sl: lineno,
1567
+ sc: char_pos,
1568
+ el: lineno,
1569
+ ec: char_pos
1608
1570
  }
1609
1571
  end
1610
1572
 
@@ -1614,11 +1576,7 @@ class Prettier::Parser < Ripper
1614
1576
  if mrhs[:body].empty?
1615
1577
  part.merge(type: :mrhs, body: [part])
1616
1578
  else
1617
- mrhs.merge!(
1618
- body: mrhs[:body] << part,
1619
- end: part[:end],
1620
- char_end: part[:char_end]
1621
- )
1579
+ mrhs.merge!(body: mrhs[:body] << part, el: part[:el], ec: part[:ec])
1622
1580
  end
1623
1581
  end
1624
1582
 
@@ -1632,10 +1590,10 @@ class Prettier::Parser < Ripper
1632
1590
  {
1633
1591
  type: :mrhs_add_star,
1634
1592
  body: [mrhs, part],
1635
- start: beging[:start],
1636
- char_start: beging[:char_start],
1637
- end: ending[:end],
1638
- char_end: ending[:char_end]
1593
+ sl: beging[:sl],
1594
+ sc: beging[:sc],
1595
+ el: ending[:el],
1596
+ ec: ending[:ec]
1639
1597
  }
1640
1598
  end
1641
1599
 
@@ -1658,8 +1616,8 @@ class Prettier::Parser < Ripper
1658
1616
  find_scanner_event(:@kw, 'next').merge!(
1659
1617
  type: :next,
1660
1618
  body: [args_add_block],
1661
- end: args_add_block[:end],
1662
- char_end: args_add_block[:char_end]
1619
+ el: args_add_block[:el],
1620
+ ec: args_add_block[:ec]
1663
1621
  )
1664
1622
  end
1665
1623
 
@@ -1671,8 +1629,8 @@ class Prettier::Parser < Ripper
1671
1629
  left.merge(
1672
1630
  type: :opassign,
1673
1631
  body: [left, oper, right],
1674
- end: right[:end],
1675
- char_end: right[:char_end]
1632
+ el: right[:el],
1633
+ ec: right[:ec]
1676
1634
  )
1677
1635
  end
1678
1636
 
@@ -1686,13 +1644,13 @@ class Prettier::Parser < Ripper
1686
1644
  location =
1687
1645
  if flattened.any?
1688
1646
  {
1689
- start: flattened[0][:start],
1690
- char_start: flattened[0][:char_start],
1691
- end: flattened[-1][:end],
1692
- char_end: flattened[-1][:char_end]
1647
+ sl: flattened[0][:sl],
1648
+ sc: flattened[0][:sc],
1649
+ el: flattened[-1][:el],
1650
+ ec: flattened[-1][:ec]
1693
1651
  }
1694
1652
  else
1695
- { start: lineno, char_start: char_pos, end: lineno, char_end: char_pos }
1653
+ { sl: lineno, sc: char_pos, el: lineno, ec: char_pos }
1696
1654
  end
1697
1655
 
1698
1656
  location.merge!(type: :params, body: types)
@@ -1702,13 +1660,18 @@ class Prettier::Parser < Ripper
1702
1660
  # anywhere in a Ruby program. It accepts as arguments the contents, which
1703
1661
  # can be either params or statements.
1704
1662
  def on_paren(contents)
1663
+ beging = find_scanner_event(:@lparen)
1705
1664
  ending = find_scanner_event(:@rparen)
1706
1665
 
1707
- find_scanner_event(:@lparen).merge!(
1666
+ if contents && contents[:type] == :params
1667
+ contents.merge!(sc: beging[:ec], ec: ending[:sc])
1668
+ end
1669
+
1670
+ beging.merge!(
1708
1671
  type: :paren,
1709
1672
  body: [contents],
1710
- end: ending[:end],
1711
- char_end: ending[:char_end]
1673
+ el: ending[:el],
1674
+ ec: ending[:ec]
1712
1675
  )
1713
1676
  end
1714
1677
 
@@ -1717,12 +1680,7 @@ class Prettier::Parser < Ripper
1717
1680
  # source string. We'll also attach on the __END__ content if there was
1718
1681
  # some found at the end of the source string.
1719
1682
  def on_program(stmts)
1720
- range = {
1721
- start: 1,
1722
- end: lines.length,
1723
- char_start: 0,
1724
- char_end: source.length
1725
- }
1683
+ range = { sl: 1, el: lines.length, sc: 0, ec: source.length }
1726
1684
 
1727
1685
  stmts[:body] << @__end__ if @__end__
1728
1686
  stmts.bind(0, source.length)
@@ -1744,8 +1702,8 @@ class Prettier::Parser < Ripper
1744
1702
  def on_qsymbols_add(qsymbols, tstring_content)
1745
1703
  qsymbols.merge!(
1746
1704
  body: qsymbols[:body] << tstring_content,
1747
- end: tstring_content[:end],
1748
- char_end: tstring_content[:char_end]
1705
+ el: tstring_content[:el],
1706
+ ec: tstring_content[:ec]
1749
1707
  )
1750
1708
  end
1751
1709
 
@@ -1763,8 +1721,8 @@ class Prettier::Parser < Ripper
1763
1721
  def on_qwords_add(qwords, tstring_content)
1764
1722
  qwords.merge!(
1765
1723
  body: qwords[:body] << tstring_content,
1766
- end: tstring_content[:end],
1767
- char_end: tstring_content[:char_end]
1724
+ el: tstring_content[:el],
1725
+ ec: tstring_content[:ec]
1768
1726
  )
1769
1727
  end
1770
1728
 
@@ -1789,8 +1747,8 @@ class Prettier::Parser < Ripper
1789
1747
  def on_regexp_add(regexp, piece)
1790
1748
  regexp.merge!(
1791
1749
  body: regexp[:body] << piece,
1792
- end: regexp[:end],
1793
- char_end: regexp[:char_end]
1750
+ el: regexp[:el],
1751
+ ec: regexp[:ec]
1794
1752
  )
1795
1753
  end
1796
1754
 
@@ -1802,8 +1760,8 @@ class Prettier::Parser < Ripper
1802
1760
  regexp.merge!(
1803
1761
  type: :regexp_literal,
1804
1762
  ending: ending[:body],
1805
- end: ending[:end],
1806
- char_end: ending[:char_end]
1763
+ el: ending[:el],
1764
+ ec: ending[:ec]
1807
1765
  )
1808
1766
  end
1809
1767
 
@@ -1812,17 +1770,17 @@ class Prettier::Parser < Ripper
1812
1770
  # determine its ending. Therefore it relies on its parent bodystmt node to
1813
1771
  # report its ending to it.
1814
1772
  class Rescue < SimpleDelegator
1815
- def bind_end(char_end)
1816
- merge!(char_end: char_end)
1773
+ def bind_end(ec)
1774
+ merge!(ec: ec)
1817
1775
 
1818
- stmts = self[:body][2]
1819
- consequent = self[:body][3]
1776
+ stmts = self[:body][1]
1777
+ consequent = self[:body][2]
1820
1778
 
1821
1779
  if consequent
1822
- consequent.bind_end(char_end)
1823
- stmts.bind_end(consequent[:char_start])
1780
+ consequent.bind_end(ec)
1781
+ stmts.bind_end(consequent[:sc])
1824
1782
  else
1825
- stmts.bind_end(char_end)
1783
+ stmts.bind_end(ec)
1826
1784
  end
1827
1785
  end
1828
1786
  end
@@ -1831,18 +1789,32 @@ class Prettier::Parser < Ripper
1831
1789
  # inside of a bodystmt.
1832
1790
  def on_rescue(exceptions, variable, stmts, consequent)
1833
1791
  beging = find_scanner_event(:@kw, 'rescue')
1792
+ exceptions = exceptions[0] if exceptions.is_a?(Array)
1834
1793
 
1835
- last_exception = exceptions.is_a?(Array) ? exceptions[-1] : exceptions
1836
- last_node = variable || last_exception || beging
1794
+ last_node = variable || exceptions || beging
1795
+ stmts.bind(find_next_statement_start(last_node[:ec]), char_pos)
1837
1796
 
1838
- stmts.bind(find_next_statement_start(last_node[:char_end]), char_pos)
1797
+ # We add an additional inner node here that ripper doesn't provide so that
1798
+ # we have a nice place to attach inline comment. But we only need it if we
1799
+ # have an exception or a variable that we're rescuing.
1800
+ rescue_ex =
1801
+ if exceptions || variable
1802
+ {
1803
+ type: :rescue_ex,
1804
+ body: [exceptions, variable],
1805
+ sl: beging[:sl],
1806
+ sc: beging[:ec] + 1,
1807
+ el: last_node[:el],
1808
+ ec: last_node[:ec]
1809
+ }
1810
+ end
1839
1811
 
1840
1812
  Rescue.new(
1841
1813
  beging.merge!(
1842
1814
  type: :rescue,
1843
- body: [exceptions, variable, stmts, consequent],
1844
- end: lineno,
1845
- char_end: char_pos
1815
+ body: [rescue_ex, stmts, consequent],
1816
+ el: lineno,
1817
+ ec: char_pos
1846
1818
  )
1847
1819
  )
1848
1820
  end
@@ -1856,10 +1828,10 @@ class Prettier::Parser < Ripper
1856
1828
  {
1857
1829
  type: :rescue_mod,
1858
1830
  body: [statement, rescued],
1859
- start: statement[:start],
1860
- char_start: statement[:char_start],
1861
- end: rescued[:end],
1862
- char_end: rescued[:char_end]
1831
+ sl: statement[:sl],
1832
+ sc: statement[:sc],
1833
+ el: rescued[:el],
1834
+ ec: rescued[:ec]
1863
1835
  }
1864
1836
  end
1865
1837
 
@@ -1874,8 +1846,8 @@ class Prettier::Parser < Ripper
1874
1846
  oper.merge!(
1875
1847
  type: :rest_param,
1876
1848
  body: [ident],
1877
- end: ident[:end],
1878
- char_end: ident[:char_end]
1849
+ el: ident[:el],
1850
+ ec: ident[:ec]
1879
1851
  )
1880
1852
  end
1881
1853
 
@@ -1892,8 +1864,8 @@ class Prettier::Parser < Ripper
1892
1864
  find_scanner_event(:@kw, 'return').merge!(
1893
1865
  type: :return,
1894
1866
  body: [args_add_block],
1895
- end: args_add_block[:end],
1896
- char_end: args_add_block[:char_end]
1867
+ el: args_add_block[:el],
1868
+ ec: args_add_block[:ec]
1897
1869
  )
1898
1870
  end
1899
1871
 
@@ -1919,18 +1891,15 @@ class Prettier::Parser < Ripper
1919
1891
  beging = find_scanner_event(:@kw, 'class')
1920
1892
  ending = find_scanner_event(:@kw, 'end')
1921
1893
 
1922
- bodystmt.bind(
1923
- find_next_statement_start(target[:char_end]),
1924
- ending[:char_start]
1925
- )
1894
+ bodystmt.bind(find_next_statement_start(target[:ec]), ending[:sc])
1926
1895
 
1927
1896
  {
1928
1897
  type: :sclass,
1929
1898
  body: [target, bodystmt],
1930
- start: beging[:start],
1931
- char_start: beging[:char_start],
1932
- end: ending[:end],
1933
- char_end: ending[:char_end]
1899
+ sl: beging[:sl],
1900
+ sc: beging[:sc],
1901
+ el: ending[:el],
1902
+ ec: ending[:ec]
1934
1903
  }
1935
1904
  end
1936
1905
 
@@ -1942,28 +1911,53 @@ class Prettier::Parser < Ripper
1942
1911
  # propagate that onto void_stmt nodes inside the stmts in order to make sure
1943
1912
  # all comments get printed appropriately.
1944
1913
  class Stmts < SimpleDelegator
1945
- def bind(char_start, char_end)
1946
- merge!(char_start: char_start, char_end: char_end)
1914
+ attr_reader :parser
1915
+
1916
+ def initialize(parser, values)
1917
+ @parser = parser
1918
+ __setobj__(values)
1919
+ end
1920
+
1921
+ def bind(sc, ec)
1922
+ merge!(sc: sc, ec: ec)
1947
1923
 
1948
1924
  if self[:body][0][:type] == :void_stmt
1949
- self[:body][0].merge!(char_start: char_start, char_end: char_start)
1925
+ self[:body][0].merge!(sc: sc, ec: sc)
1950
1926
  end
1927
+
1928
+ attach_comments(sc, ec)
1951
1929
  end
1952
1930
 
1953
- def bind_end(char_end)
1954
- merge!(char_end: char_end)
1931
+ def bind_end(ec)
1932
+ merge!(ec: ec)
1955
1933
  end
1956
1934
 
1957
1935
  def <<(statement)
1958
1936
  if self[:body].any?
1959
- merge!(statement.slice(:end, :char_end))
1937
+ merge!(statement.slice(:el, :ec))
1960
1938
  else
1961
- merge!(statement.slice(:start, :end, :char_start, :char_end))
1939
+ merge!(statement.slice(:sl, :el, :sc, :ec))
1962
1940
  end
1963
1941
 
1964
1942
  self[:body] << statement
1965
1943
  self
1966
1944
  end
1945
+
1946
+ private
1947
+
1948
+ def attach_comments(sc, ec)
1949
+ attachable =
1950
+ parser.comments.select do |comment|
1951
+ comment[:type] == :@comment && !comment[:inline] &&
1952
+ sc <= comment[:sc] && ec >= comment[:ec] &&
1953
+ !comment[:value].include?('prettier-ignore')
1954
+ end
1955
+
1956
+ return if attachable.empty?
1957
+
1958
+ parser.comments -= attachable
1959
+ self[:body] = (self[:body] + attachable).sort_by! { |node| node[:sc] }
1960
+ end
1967
1961
  end
1968
1962
 
1969
1963
  # stmts_new is a parser event that represents the beginning of a list of
@@ -1971,12 +1965,13 @@ class Prettier::Parser < Ripper
1971
1965
  # stmts_add events, which we'll append onto an array body.
1972
1966
  def on_stmts_new
1973
1967
  Stmts.new(
1968
+ self,
1974
1969
  type: :stmts,
1975
1970
  body: [],
1976
- start: lineno,
1977
- end: lineno,
1978
- char_start: char_pos,
1979
- char_end: char_pos
1971
+ sl: lineno,
1972
+ el: lineno,
1973
+ sc: char_pos,
1974
+ ec: char_pos
1980
1975
  )
1981
1976
  end
1982
1977
 
@@ -1998,10 +1993,10 @@ class Prettier::Parser < Ripper
1998
1993
  {
1999
1994
  type: :string_concat,
2000
1995
  body: [left, right],
2001
- start: left[:start],
2002
- char_start: left[:char_start],
2003
- end: right[:end],
2004
- char_end: right[:char_end]
1996
+ sl: left[:sl],
1997
+ sc: left[:sc],
1998
+ el: right[:el],
1999
+ ec: right[:ec]
2005
2000
  }
2006
2001
  end
2007
2002
 
@@ -2014,10 +2009,10 @@ class Prettier::Parser < Ripper
2014
2009
  {
2015
2010
  type: :string,
2016
2011
  body: [],
2017
- start: lineno,
2018
- end: lineno,
2019
- char_start: char_pos,
2020
- char_end: char_pos
2012
+ sl: lineno,
2013
+ el: lineno,
2014
+ sc: char_pos,
2015
+ ec: char_pos
2021
2016
  }
2022
2017
  end
2023
2018
 
@@ -2026,11 +2021,7 @@ class Prettier::Parser < Ripper
2026
2021
  # It accepts as arguments the parent string node as well as the additional
2027
2022
  # piece of the string.
2028
2023
  def on_string_add(string, piece)
2029
- string.merge!(
2030
- body: string[:body] << piece,
2031
- end: piece[:end],
2032
- char_end: piece[:char_end]
2033
- )
2024
+ string.merge!(body: string[:body] << piece, el: piece[:el], ec: piece[:ec])
2034
2025
  end
2035
2026
 
2036
2027
  # string_dvar is a parser event that represents a very special kind of
@@ -2042,8 +2033,8 @@ class Prettier::Parser < Ripper
2042
2033
  find_scanner_event(:@embvar).merge!(
2043
2034
  type: :string_dvar,
2044
2035
  body: [var_ref],
2045
- end: var_ref[:end],
2046
- char_end: var_ref[:char_end]
2036
+ el: var_ref[:el],
2037
+ ec: var_ref[:ec]
2047
2038
  )
2048
2039
  end
2049
2040
 
@@ -2055,15 +2046,15 @@ class Prettier::Parser < Ripper
2055
2046
  beging = find_scanner_event(:@embexpr_beg)
2056
2047
  ending = find_scanner_event(:@embexpr_end)
2057
2048
 
2058
- stmts.bind(beging[:char_end], ending[:char_start])
2049
+ stmts.bind(beging[:ec], ending[:sc])
2059
2050
 
2060
2051
  {
2061
2052
  type: :string_embexpr,
2062
2053
  body: [stmts],
2063
- start: beging[:start],
2064
- char_start: beging[:char_start],
2065
- end: ending[:end],
2066
- char_end: ending[:char_end]
2054
+ sl: beging[:sl],
2055
+ sc: beging[:sc],
2056
+ el: ending[:el],
2057
+ ec: ending[:ec]
2067
2058
  }
2068
2059
  end
2069
2060
 
@@ -2082,10 +2073,10 @@ class Prettier::Parser < Ripper
2082
2073
  type: :string_literal,
2083
2074
  body: string[:body],
2084
2075
  quote: beging[:body],
2085
- start: beging[:start],
2086
- char_start: beging[:char_start],
2087
- end: ending[:end],
2088
- char_end: ending[:char_end]
2076
+ sl: beging[:sl],
2077
+ sc: beging[:sc],
2078
+ el: ending[:el],
2079
+ ec: ending[:ec]
2089
2080
  }
2090
2081
  end
2091
2082
  end
@@ -2098,8 +2089,8 @@ class Prettier::Parser < Ripper
2098
2089
  find_scanner_event(:@kw, 'super').merge!(
2099
2090
  type: :super,
2100
2091
  body: [contents],
2101
- end: contents[:end],
2102
- char_end: contents[:char_end]
2092
+ el: contents[:el],
2093
+ ec: contents[:ec]
2103
2094
  )
2104
2095
  end
2105
2096
 
@@ -2130,7 +2121,7 @@ class Prettier::Parser < Ripper
2130
2121
  contents.merge(type: :symbol_literal, body: [contents])
2131
2122
  else
2132
2123
  beging = find_scanner_event(:@symbeg)
2133
- contents.merge!(type: :symbol_literal, char_start: beging[:char_start])
2124
+ contents.merge!(type: :symbol_literal, sc: beging[:sc])
2134
2125
  end
2135
2126
  end
2136
2127
 
@@ -2149,8 +2140,8 @@ class Prettier::Parser < Ripper
2149
2140
  def on_symbols_add(symbols, word_add)
2150
2141
  symbols.merge!(
2151
2142
  body: symbols[:body] << word_add,
2152
- end: word_add[:end],
2153
- char_end: word_add[:char_end]
2143
+ el: word_add[:el],
2144
+ ec: word_add[:ec]
2154
2145
  )
2155
2146
  end
2156
2147
 
@@ -2161,8 +2152,7 @@ class Prettier::Parser < Ripper
2161
2152
  def find_colon2_before(const)
2162
2153
  index =
2163
2154
  scanner_events.rindex do |event|
2164
- event[:type] == :@op && event[:body] == '::' &&
2165
- event[:char_start] < const[:char_start]
2155
+ event[:type] == :@op && event[:body] == '::' && event[:sc] < const[:sc]
2166
2156
  end
2167
2157
 
2168
2158
  scanner_events[index]
@@ -2179,8 +2169,8 @@ class Prettier::Parser < Ripper
2179
2169
  const.merge(
2180
2170
  type: :top_const_field,
2181
2171
  body: [const],
2182
- start: beging[:start],
2183
- char_start: beging[:char_start]
2172
+ sl: beging[:sl],
2173
+ sc: beging[:sc]
2184
2174
  )
2185
2175
  end
2186
2176
 
@@ -2195,8 +2185,8 @@ class Prettier::Parser < Ripper
2195
2185
  const.merge(
2196
2186
  type: :top_const_ref,
2197
2187
  body: [const],
2198
- start: beging[:start],
2199
- char_start: beging[:char_start]
2188
+ sl: beging[:sl],
2189
+ sc: beging[:sc]
2200
2190
  )
2201
2191
  end
2202
2192
 
@@ -2208,15 +2198,15 @@ class Prettier::Parser < Ripper
2208
2198
  if oper == :not
2209
2199
  node = find_scanner_event(:@kw, 'not')
2210
2200
 
2211
- paren = source[node[:char_end]...value[:char_start]].include?('(')
2201
+ paren = source[node[:ec]...value[:sc]].include?('(')
2212
2202
  ending = paren ? find_scanner_event(:@rparen) : value
2213
2203
 
2214
2204
  node.merge!(
2215
2205
  type: :unary,
2216
2206
  oper: oper,
2217
2207
  body: [value],
2218
- end: ending[:end],
2219
- char_end: ending[:char_end],
2208
+ el: ending[:el],
2209
+ ec: ending[:ec],
2220
2210
  paren: paren
2221
2211
  )
2222
2212
  else
@@ -2226,7 +2216,7 @@ class Prettier::Parser < Ripper
2226
2216
  # stack. So we need to explicitly disallow those operators.
2227
2217
  index =
2228
2218
  scanner_events.rindex do |scanner_event|
2229
- scanner_event[:type] == :@op &&
2219
+ scanner_event[:type] == :@op && scanner_event[:sc] < value[:sc] &&
2230
2220
  !%w[.. ...].include?(scanner_event[:body])
2231
2221
  end
2232
2222
 
@@ -2235,8 +2225,8 @@ class Prettier::Parser < Ripper
2235
2225
  type: :unary,
2236
2226
  oper: oper[0],
2237
2227
  body: [value],
2238
- end: value[:end],
2239
- char_end: value[:char_end]
2228
+ el: value[:el],
2229
+ ec: value[:ec]
2240
2230
  )
2241
2231
  end
2242
2232
  end
@@ -2251,8 +2241,8 @@ class Prettier::Parser < Ripper
2251
2241
  find_scanner_event(:@kw, 'undef').merge!(
2252
2242
  type: :undef,
2253
2243
  body: symbol_literals,
2254
- end: last[:end],
2255
- char_end: last[:char_end]
2244
+ el: last[:el],
2245
+ ec: last[:ec]
2256
2246
  )
2257
2247
  end
2258
2248
 
@@ -2264,15 +2254,15 @@ class Prettier::Parser < Ripper
2264
2254
  beging = find_scanner_event(:@kw, 'unless')
2265
2255
  ending = consequent || find_scanner_event(:@kw, 'end')
2266
2256
 
2267
- stmts.bind(predicate[:char_end], ending[:char_start])
2257
+ stmts.bind(predicate[:ec], ending[:sc])
2268
2258
 
2269
2259
  {
2270
2260
  type: :unless,
2271
2261
  body: [predicate, stmts, consequent],
2272
- start: beging[:start],
2273
- char_start: beging[:char_start],
2274
- end: ending[:end],
2275
- char_end: ending[:char_end]
2262
+ sl: beging[:sl],
2263
+ sc: beging[:sc],
2264
+ el: ending[:el],
2265
+ ec: ending[:ec]
2276
2266
  }
2277
2267
  end
2278
2268
 
@@ -2285,10 +2275,10 @@ class Prettier::Parser < Ripper
2285
2275
  {
2286
2276
  type: :unless_mod,
2287
2277
  body: [predicate, statement],
2288
- start: statement[:start],
2289
- char_start: statement[:char_start],
2290
- end: predicate[:end],
2291
- char_end: predicate[:char_end]
2278
+ sl: statement[:sl],
2279
+ sc: statement[:sc],
2280
+ el: predicate[:el],
2281
+ ec: predicate[:ec]
2292
2282
  }
2293
2283
  end
2294
2284
 
@@ -2299,15 +2289,22 @@ class Prettier::Parser < Ripper
2299
2289
  beging = find_scanner_event(:@kw, 'until')
2300
2290
  ending = find_scanner_event(:@kw, 'end')
2301
2291
 
2302
- stmts.bind(predicate[:char_end], ending[:char_start])
2292
+ # Consume the do keyword if it exists so that it doesn't get confused for
2293
+ # some other block
2294
+ do_event = find_scanner_event(:@kw, 'do', consume: false)
2295
+ if do_event && do_event[:sc] > predicate[:ec] && do_event[:ec] < ending[:sc]
2296
+ scanner_events.delete(do_event)
2297
+ end
2298
+
2299
+ stmts.bind(predicate[:ec], ending[:sc])
2303
2300
 
2304
2301
  {
2305
2302
  type: :until,
2306
2303
  body: [predicate, stmts],
2307
- start: beging[:start],
2308
- char_start: beging[:char_start],
2309
- end: ending[:end],
2310
- char_end: ending[:char_end]
2304
+ sl: beging[:sl],
2305
+ sc: beging[:sc],
2306
+ el: ending[:el],
2307
+ ec: ending[:ec]
2311
2308
  }
2312
2309
  end
2313
2310
 
@@ -2320,10 +2317,10 @@ class Prettier::Parser < Ripper
2320
2317
  {
2321
2318
  type: :until_mod,
2322
2319
  body: [predicate, statement],
2323
- start: statement[:start],
2324
- char_start: statement[:char_start],
2325
- end: predicate[:end],
2326
- char_end: predicate[:char_end]
2320
+ sl: statement[:sl],
2321
+ sc: statement[:sc],
2322
+ el: predicate[:el],
2323
+ ec: predicate[:ec]
2327
2324
  }
2328
2325
  end
2329
2326
 
@@ -2334,16 +2331,16 @@ class Prettier::Parser < Ripper
2334
2331
  def on_var_alias(left, right)
2335
2332
  beging = find_scanner_event(:@kw, 'alias')
2336
2333
 
2337
- paren = source[beging[:char_end]...left[:char_start]].include?('(')
2334
+ paren = source[beging[:ec]...left[:sc]].include?('(')
2338
2335
  ending = paren ? find_scanner_event(:@rparen) : right
2339
2336
 
2340
2337
  {
2341
2338
  type: :var_alias,
2342
2339
  body: [left, right],
2343
- start: beging[:start],
2344
- char_start: beging[:char_start],
2345
- end: ending[:end],
2346
- char_end: ending[:char_end]
2340
+ sl: beging[:sl],
2341
+ sc: beging[:sc],
2342
+ el: ending[:el],
2343
+ ec: ending[:ec]
2347
2344
  }
2348
2345
  end
2349
2346
 
@@ -2395,13 +2392,7 @@ class Prettier::Parser < Ripper
2395
2392
  # block of code. It often will have comments attached to it, so it requires
2396
2393
  # some special handling.
2397
2394
  def on_void_stmt
2398
- {
2399
- type: :void_stmt,
2400
- start: lineno,
2401
- end: lineno,
2402
- char_start: char_pos,
2403
- char_end: char_pos
2404
- }
2395
+ { type: :void_stmt, sl: lineno, el: lineno, sc: char_pos, ec: char_pos }
2405
2396
  end
2406
2397
 
2407
2398
  # when is a parser event that represents another clause in a case chain.
@@ -2412,15 +2403,15 @@ class Prettier::Parser < Ripper
2412
2403
  beging = find_scanner_event(:@kw, 'when')
2413
2404
  ending = consequent || find_scanner_event(:@kw, 'end')
2414
2405
 
2415
- stmts.bind(predicate[:char_end], ending[:char_start])
2406
+ stmts.bind(predicate[:ec], ending[:sc])
2416
2407
 
2417
2408
  {
2418
2409
  type: :when,
2419
2410
  body: [predicate, stmts, consequent],
2420
- start: beging[:start],
2421
- char_start: beging[:char_start],
2422
- end: ending[:end],
2423
- char_end: ending[:char_end]
2411
+ sl: beging[:sl],
2412
+ sc: beging[:sc],
2413
+ el: ending[:el],
2414
+ ec: ending[:ec]
2424
2415
  }
2425
2416
  end
2426
2417
 
@@ -2431,15 +2422,22 @@ class Prettier::Parser < Ripper
2431
2422
  beging = find_scanner_event(:@kw, 'while')
2432
2423
  ending = find_scanner_event(:@kw, 'end')
2433
2424
 
2434
- stmts.bind(predicate[:char_end], ending[:char_start])
2425
+ # Consume the do keyword if it exists so that it doesn't get confused for
2426
+ # some other block
2427
+ do_event = find_scanner_event(:@kw, 'do', consume: false)
2428
+ if do_event && do_event[:sc] > predicate[:ec] && do_event[:ec] < ending[:sc]
2429
+ scanner_events.delete(do_event)
2430
+ end
2431
+
2432
+ stmts.bind(predicate[:ec], ending[:sc])
2435
2433
 
2436
2434
  {
2437
2435
  type: :while,
2438
2436
  body: [predicate, stmts],
2439
- start: beging[:start],
2440
- char_start: beging[:char_start],
2441
- end: ending[:end],
2442
- char_end: ending[:char_end]
2437
+ sl: beging[:sl],
2438
+ sc: beging[:sc],
2439
+ el: ending[:el],
2440
+ ec: ending[:ec]
2443
2441
  }
2444
2442
  end
2445
2443
 
@@ -2452,10 +2450,10 @@ class Prettier::Parser < Ripper
2452
2450
  {
2453
2451
  type: :while_mod,
2454
2452
  body: [predicate, statement],
2455
- start: statement[:start],
2456
- char_start: statement[:char_start],
2457
- end: predicate[:end],
2458
- char_end: predicate[:char_end]
2453
+ sl: statement[:sl],
2454
+ sc: statement[:sc],
2455
+ el: predicate[:el],
2456
+ ec: predicate[:ec]
2459
2457
  }
2460
2458
  end
2461
2459
 
@@ -2485,11 +2483,7 @@ class Prettier::Parser < Ripper
2485
2483
  # location information from the first piece.
2486
2484
  piece.merge(type: :word, body: [piece])
2487
2485
  else
2488
- word.merge!(
2489
- body: word[:body] << piece,
2490
- end: piece[:end],
2491
- char_end: piece[:char_end]
2492
- )
2486
+ word.merge!(body: word[:body] << piece, el: piece[:el], ec: piece[:ec])
2493
2487
  end
2494
2488
  end
2495
2489
 
@@ -2508,8 +2502,8 @@ class Prettier::Parser < Ripper
2508
2502
  def on_words_add(words, word_add)
2509
2503
  words.merge!(
2510
2504
  body: words[:body] << word_add,
2511
- end: word_add[:end],
2512
- char_end: word_add[:char_end]
2505
+ el: word_add[:el],
2506
+ ec: word_add[:ec]
2513
2507
  )
2514
2508
  end
2515
2509
 
@@ -2542,8 +2536,8 @@ class Prettier::Parser < Ripper
2542
2536
  def on_xstring_add(xstring, piece)
2543
2537
  xstring.merge!(
2544
2538
  body: xstring[:body] << piece,
2545
- end: piece[:end],
2546
- char_end: piece[:char_end]
2539
+ el: piece[:el],
2540
+ ec: piece[:ec]
2547
2541
  )
2548
2542
  end
2549
2543
 
@@ -2568,11 +2562,7 @@ class Prettier::Parser < Ripper
2568
2562
  heredoc.merge!(body: xstring[:body])
2569
2563
  else
2570
2564
  ending = find_scanner_event(:@tstring_end)
2571
- xstring.merge!(
2572
- type: :xstring_literal,
2573
- end: ending[:end],
2574
- char_end: ending[:char_end]
2575
- )
2565
+ xstring.merge!(type: :xstring_literal, el: ending[:el], ec: ending[:ec])
2576
2566
  end
2577
2567
  end
2578
2568
 
@@ -2583,8 +2573,8 @@ class Prettier::Parser < Ripper
2583
2573
  find_scanner_event(:@kw, 'yield').merge!(
2584
2574
  type: :yield,
2585
2575
  body: [args_add_block],
2586
- end: args_add_block[:end],
2587
- char_end: args_add_block[:char_end]
2576
+ el: args_add_block[:el],
2577
+ ec: args_add_block[:ec]
2588
2578
  )
2589
2579
  end
2590
2580
 
@@ -2604,24 +2594,3 @@ class Prettier::Parser < Ripper
2604
2594
  find_scanner_event(:@kw, 'super').merge!(type: :zsuper)
2605
2595
  end
2606
2596
  end
2607
-
2608
- # If this is the main file we're executing, then most likely this is being
2609
- # executed from the parser.js spawn. In that case, read the ruby source from
2610
- # stdin and report back the AST over stdout.
2611
-
2612
- if $0 == __FILE__
2613
- builder = Prettier::Parser.new($stdin.read)
2614
- response = builder.parse
2615
-
2616
- if !response || builder.error?
2617
- warn(
2618
- '@prettier/plugin-ruby encountered an error when attempting to parse ' \
2619
- 'the ruby source. This usually means there was a syntax error in the ' \
2620
- 'file in question. You can verify by running `ruby -i [path/to/file]`.'
2621
- )
2622
-
2623
- exit 1
2624
- end
2625
-
2626
- puts JSON.fast_generate(response)
2627
- end