prettier 1.2.1 → 1.3.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.
- checksums.yaml +4 -4
- data/CHANGELOG.md +61 -1
- data/CONTRIBUTING.md +2 -2
- data/README.md +21 -91
- data/lib/prettier.rb +2 -2
- data/node_modules/prettier/index.js +54 -54
- data/package.json +3 -4
- data/rubocop.yml +26 -0
- data/src/haml/embed.js +87 -0
- data/src/haml/nodes/comment.js +27 -0
- data/src/haml/nodes/doctype.js +34 -0
- data/src/haml/nodes/filter.js +16 -0
- data/src/haml/nodes/hamlComment.js +21 -0
- data/src/haml/nodes/plain.js +6 -0
- data/src/haml/nodes/root.js +8 -0
- data/src/haml/nodes/script.js +33 -0
- data/src/haml/nodes/silentScript.js +59 -0
- data/src/haml/nodes/tag.js +193 -0
- data/src/haml/parser.js +33 -0
- data/src/haml/parser.rb +141 -0
- data/src/haml/printer.js +28 -0
- data/src/{ruby.js → plugin.js} +25 -4
- data/src/prettier.js +1 -0
- data/src/rbs/parser.js +51 -0
- data/src/rbs/parser.rb +91 -0
- data/src/rbs/printer.js +605 -0
- data/src/{embed.js → ruby/embed.js} +6 -2
- data/src/{nodes.js → ruby/nodes.js} +0 -0
- data/src/{nodes → ruby/nodes}/alias.js +1 -1
- data/src/{nodes → ruby/nodes}/aref.js +8 -1
- data/src/{nodes → ruby/nodes}/args.js +2 -2
- data/src/{nodes → ruby/nodes}/arrays.js +2 -3
- data/src/{nodes → ruby/nodes}/assign.js +7 -3
- data/src/ruby/nodes/blocks.js +90 -0
- data/src/{nodes → ruby/nodes}/calls.js +18 -11
- data/src/{nodes → ruby/nodes}/case.js +1 -1
- data/src/{nodes → ruby/nodes}/class.js +1 -1
- data/src/ruby/nodes/commands.js +131 -0
- data/src/{nodes → ruby/nodes}/conditionals.js +3 -3
- data/src/{nodes → ruby/nodes}/constants.js +2 -2
- data/src/{nodes → ruby/nodes}/flow.js +2 -2
- data/src/{nodes → ruby/nodes}/hashes.js +32 -10
- data/src/{nodes → ruby/nodes}/heredocs.js +2 -2
- data/src/ruby/nodes/hooks.js +34 -0
- data/src/{nodes → ruby/nodes}/ints.js +0 -0
- data/src/{nodes → ruby/nodes}/lambdas.js +2 -2
- data/src/{nodes → ruby/nodes}/loops.js +10 -7
- data/src/{nodes → ruby/nodes}/massign.js +8 -1
- data/src/{nodes → ruby/nodes}/methods.js +10 -9
- data/src/{nodes → ruby/nodes}/operators.js +2 -2
- data/src/{nodes → ruby/nodes}/params.js +31 -16
- data/src/{nodes → ruby/nodes}/patterns.js +17 -6
- data/src/{nodes → ruby/nodes}/regexp.js +2 -2
- data/src/{nodes → ruby/nodes}/rescue.js +2 -2
- data/src/ruby/nodes/return.js +94 -0
- data/src/{nodes → ruby/nodes}/statements.js +6 -9
- data/src/{nodes → ruby/nodes}/strings.js +27 -36
- data/src/{nodes → ruby/nodes}/super.js +2 -2
- data/src/{nodes → ruby/nodes}/undef.js +1 -1
- data/src/{parser.js → ruby/parser.js} +4 -3
- data/src/{parser.rb → ruby/parser.rb} +450 -501
- data/src/{printer.js → ruby/printer.js} +33 -1
- data/src/{toProc.js → ruby/toProc.js} +4 -8
- data/src/utils.js +10 -93
- data/src/utils/containsAssignment.js +11 -0
- data/src/utils/getTrailingComma.js +5 -0
- data/src/utils/hasAncestor.js +17 -0
- data/src/utils/literal.js +7 -0
- data/src/utils/makeCall.js +14 -0
- data/src/utils/noIndent.js +11 -0
- data/src/utils/skipAssignIndent.js +10 -0
- metadata +65 -41
- data/src/nodes/blocks.js +0 -85
- data/src/nodes/commands.js +0 -91
- data/src/nodes/hooks.js +0 -44
- data/src/nodes/return.js +0 -72
@@ -10,7 +10,7 @@ const {
|
|
10
10
|
literalline,
|
11
11
|
softline,
|
12
12
|
trim
|
13
|
-
} = require("
|
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
|
|
@@ -123,12 +120,12 @@ module.exports = {
|
|
123
120
|
if (lineNo === null) {
|
124
121
|
parts.push(printed);
|
125
122
|
} else if (
|
126
|
-
stmt.
|
123
|
+
stmt.sl - lineNo > 1 ||
|
127
124
|
[stmt.type, stmts[index - 1].type].includes("access_ctrl")
|
128
125
|
) {
|
129
126
|
parts.push(hardline, hardline, printed);
|
130
127
|
} else if (
|
131
|
-
stmt.
|
128
|
+
stmt.sl !== lineNo ||
|
132
129
|
path.getParentNode().type !== "string_embexpr"
|
133
130
|
) {
|
134
131
|
parts.push(hardline, printed);
|
@@ -136,7 +133,7 @@ module.exports = {
|
|
136
133
|
parts.push("; ", printed);
|
137
134
|
}
|
138
135
|
|
139
|
-
lineNo = stmt.
|
136
|
+
lineNo = stmt.el;
|
140
137
|
});
|
141
138
|
|
142
139
|
return concat(parts);
|
@@ -6,7 +6,7 @@ const {
|
|
6
6
|
literalline,
|
7
7
|
softline,
|
8
8
|
join
|
9
|
-
} = require("
|
9
|
+
} = require("../../prettier");
|
10
10
|
|
11
11
|
// If there is some part of this string that matches an escape sequence or that
|
12
12
|
// contains the interpolation pattern ("#{"), then we are locked into whichever
|
@@ -31,17 +31,10 @@ function isSingleQuotable(node) {
|
|
31
31
|
|
32
32
|
const quotePattern = new RegExp("\\\\([\\s\\S])|(['\"])", "g");
|
33
33
|
|
34
|
-
function normalizeQuotes(content, enclosingQuote
|
35
|
-
const replaceOther = originalQuote === '"';
|
36
|
-
const otherQuote = enclosingQuote === '"' ? "'" : '"';
|
37
|
-
|
34
|
+
function normalizeQuotes(content, enclosingQuote) {
|
38
35
|
// Escape and unescape single and double quotes as needed to be able to
|
39
36
|
// enclose `content` with `enclosingQuote`.
|
40
37
|
return content.replace(quotePattern, (match, escaped, quote) => {
|
41
|
-
if (replaceOther && escaped === otherQuote) {
|
42
|
-
return escaped;
|
43
|
-
}
|
44
|
-
|
45
38
|
if (quote === enclosingQuote) {
|
46
39
|
return `\\${quote}`;
|
47
40
|
}
|
@@ -97,12 +90,34 @@ function printDynaSymbol(path, opts, print) {
|
|
97
90
|
return concat([":", quote].concat(path.map(print, "body")).concat(quote));
|
98
91
|
}
|
99
92
|
|
93
|
+
function printStringConcat(path, opts, print) {
|
94
|
+
const [leftDoc, rightDoc] = path.map(print, "body");
|
95
|
+
|
96
|
+
return group(concat([leftDoc, " \\", indent(concat([hardline, rightDoc]))]));
|
97
|
+
}
|
98
|
+
|
100
99
|
// Prints out an interpolated variable in the string by converting it into an
|
101
100
|
// embedded expression.
|
102
101
|
function printStringDVar(path, opts, print) {
|
103
102
|
return concat(["#{", path.call(print, "body", 0), "}"]);
|
104
103
|
}
|
105
104
|
|
105
|
+
function printStringEmbExpr(path, opts, print) {
|
106
|
+
const parts = path.call(print, "body", 0);
|
107
|
+
|
108
|
+
// If the interpolated expression is inside of a heredoc or an xstring
|
109
|
+
// literal (a string that gets sent to the command line) then we don't want
|
110
|
+
// to automatically indent, as this can lead to some very odd looking
|
111
|
+
// expressions
|
112
|
+
if (["heredoc", "xstring_literal"].includes(path.getParentNode().type)) {
|
113
|
+
return concat(["#{", parts, "}"]);
|
114
|
+
}
|
115
|
+
|
116
|
+
return group(
|
117
|
+
concat(["#{", indent(concat([softline, parts])), concat([softline, "}"])])
|
118
|
+
);
|
119
|
+
}
|
120
|
+
|
106
121
|
// Prints out a literal string. This function does its best to respect the
|
107
122
|
// wishes of the user with regards to single versus double quotes, but if the
|
108
123
|
// string contains any escape expressions then it will just keep the original
|
@@ -131,10 +146,7 @@ function printStringLiteral(path, { rubySingleQuote }, print) {
|
|
131
146
|
}
|
132
147
|
|
133
148
|
// 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
|
-
);
|
149
|
+
return join(literalline, normalizeQuotes(part.body, quote).split("\n"));
|
138
150
|
});
|
139
151
|
|
140
152
|
return concat([quote].concat(parts).concat(getClosingQuote(quote)));
|
@@ -155,30 +167,9 @@ function printXStringLiteral(path, opts, print) {
|
|
155
167
|
module.exports = {
|
156
168
|
"@CHAR": printChar,
|
157
169
|
dyna_symbol: printDynaSymbol,
|
158
|
-
string_concat:
|
159
|
-
group(
|
160
|
-
concat([
|
161
|
-
path.call(print, "body", 0),
|
162
|
-
" \\",
|
163
|
-
indent(concat([hardline, path.call(print, "body", 1)]))
|
164
|
-
])
|
165
|
-
),
|
170
|
+
string_concat: printStringConcat,
|
166
171
|
string_dvar: printStringDVar,
|
167
|
-
string_embexpr:
|
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
|
-
},
|
172
|
+
string_embexpr: printStringEmbExpr,
|
182
173
|
string_literal: printStringLiteral,
|
183
174
|
symbol_literal: printSymbolLiteral,
|
184
175
|
xstring_literal: printXStringLiteral
|
@@ -1,5 +1,5 @@
|
|
1
|
-
const { align, concat, group, join, line } = require("
|
2
|
-
const { literal } = require("
|
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];
|
@@ -4,6 +4,7 @@ const path = require("path");
|
|
4
4
|
// In order to properly parse ruby code, we need to tell the ruby process to
|
5
5
|
// parse using UTF-8. Unfortunately, the way that you accomplish this looks
|
6
6
|
// differently depending on your platform.
|
7
|
+
/* istanbul ignore next */
|
7
8
|
const LANG = (() => {
|
8
9
|
const { env, platform } = process;
|
9
10
|
const envValue = env.LC_ALL || env.LC_CTYPE || env.LANG;
|
@@ -42,7 +43,7 @@ function parse(text, _parsers, _opts) {
|
|
42
43
|
{
|
43
44
|
env: Object.assign({}, process.env, { LANG }),
|
44
45
|
input: text,
|
45
|
-
maxBuffer:
|
46
|
+
maxBuffer: 15 * 1024 * 1024 // 15MB
|
46
47
|
}
|
47
48
|
);
|
48
49
|
|
@@ -67,14 +68,14 @@ function hasPragma(text) {
|
|
67
68
|
// for returning the index of the character within the source string that is the
|
68
69
|
// beginning of the given node.
|
69
70
|
function locStart(node) {
|
70
|
-
return node.
|
71
|
+
return node.sc;
|
71
72
|
}
|
72
73
|
|
73
74
|
// This function is critical for comments and cursor support, and is responsible
|
74
75
|
// for returning the index of the character within the source string that is the
|
75
76
|
// ending of the given node.
|
76
77
|
function locEnd(node) {
|
77
|
-
return node.
|
78
|
+
return node.ec;
|
78
79
|
}
|
79
80
|
|
80
81
|
module.exports = {
|
@@ -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'
|
17
|
+
require 'json'
|
18
18
|
require 'ripper'
|
19
19
|
|
20
20
|
module Prettier; end
|
@@ -40,6 +40,13 @@ class Prettier::Parser < Ripper
|
|
40
40
|
@source.lines.each { |line| @line_counts << @line_counts.last + line.size }
|
41
41
|
end
|
42
42
|
|
43
|
+
def self.parse(source)
|
44
|
+
builder = new(source)
|
45
|
+
|
46
|
+
response = builder.parse
|
47
|
+
response unless builder.error?
|
48
|
+
end
|
49
|
+
|
43
50
|
private
|
44
51
|
|
45
52
|
# This represents the current place in the source string that we've gotten to
|
@@ -89,14 +96,14 @@ class Prettier::Parser < Ripper
|
|
89
96
|
|
90
97
|
(SCANNER_EVENTS - defined).each do |event|
|
91
98
|
define_method(:"on_#{event}") do |value|
|
92
|
-
|
99
|
+
ec = char_pos + value.size
|
93
100
|
node = {
|
94
101
|
type: :"@#{event}",
|
95
102
|
body: value,
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
103
|
+
sl: lineno,
|
104
|
+
el: lineno,
|
105
|
+
sc: char_pos,
|
106
|
+
ec: ec
|
100
107
|
}
|
101
108
|
|
102
109
|
scanner_events << node
|
@@ -118,10 +125,10 @@ class Prettier::Parser < Ripper
|
|
118
125
|
@comments << {
|
119
126
|
type: :@comment,
|
120
127
|
value: value[1..-1].chomp.force_encoding('UTF-8'),
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
|
128
|
+
sl: lineno,
|
129
|
+
el: lineno,
|
130
|
+
sc: char_pos,
|
131
|
+
ec: char_pos + value.length - 1
|
125
132
|
}
|
126
133
|
end
|
127
134
|
|
@@ -138,10 +145,10 @@ class Prettier::Parser < Ripper
|
|
138
145
|
{
|
139
146
|
type: :ignored_nl,
|
140
147
|
body: nil,
|
141
|
-
|
142
|
-
|
143
|
-
|
144
|
-
|
148
|
+
sl: lineno,
|
149
|
+
el: lineno,
|
150
|
+
sc: char_pos,
|
151
|
+
ec: char_pos
|
145
152
|
}
|
146
153
|
end
|
147
154
|
|
@@ -186,16 +193,13 @@ class Prettier::Parser < Ripper
|
|
186
193
|
beging = find_scanner_event(:@lbrace)
|
187
194
|
ending = find_scanner_event(:@rbrace)
|
188
195
|
|
189
|
-
stmts.bind(
|
190
|
-
find_next_statement_start(beging[:char_end]),
|
191
|
-
ending[:char_start]
|
192
|
-
)
|
196
|
+
stmts.bind(find_next_statement_start(beging[:ec]), ending[:sc])
|
193
197
|
|
194
198
|
find_scanner_event(:@kw, 'BEGIN').merge!(
|
195
199
|
type: :BEGIN,
|
196
200
|
body: [beging, stmts],
|
197
|
-
|
198
|
-
|
201
|
+
el: ending[:el],
|
202
|
+
ec: ending[:ec]
|
199
203
|
)
|
200
204
|
end
|
201
205
|
|
@@ -213,16 +217,13 @@ class Prettier::Parser < Ripper
|
|
213
217
|
beging = find_scanner_event(:@lbrace)
|
214
218
|
ending = find_scanner_event(:@rbrace)
|
215
219
|
|
216
|
-
stmts.bind(
|
217
|
-
find_next_statement_start(beging[:char_end]),
|
218
|
-
ending[:char_start]
|
219
|
-
)
|
220
|
+
stmts.bind(find_next_statement_start(beging[:ec]), ending[:sc])
|
220
221
|
|
221
222
|
find_scanner_event(:@kw, 'END').merge!(
|
222
223
|
type: :END,
|
223
224
|
body: [beging, stmts],
|
224
|
-
|
225
|
-
|
225
|
+
el: ending[:el],
|
226
|
+
ec: ending[:ec]
|
226
227
|
)
|
227
228
|
end
|
228
229
|
|
@@ -234,16 +235,16 @@ class Prettier::Parser < Ripper
|
|
234
235
|
def on_alias(left, right)
|
235
236
|
beging = find_scanner_event(:@kw, 'alias')
|
236
237
|
|
237
|
-
paren = source[beging[:
|
238
|
+
paren = source[beging[:ec]...left[:sc]].include?('(')
|
238
239
|
ending = paren ? find_scanner_event(:@rparen) : right
|
239
240
|
|
240
241
|
{
|
241
242
|
type: :alias,
|
242
243
|
body: [left, right],
|
243
|
-
|
244
|
-
|
245
|
-
|
246
|
-
|
244
|
+
sl: beging[:sl],
|
245
|
+
sc: beging[:sc],
|
246
|
+
el: ending[:el],
|
247
|
+
ec: ending[:ec]
|
247
248
|
}
|
248
249
|
end
|
249
250
|
|
@@ -268,10 +269,10 @@ class Prettier::Parser < Ripper
|
|
268
269
|
{
|
269
270
|
type: :aref,
|
270
271
|
body: [collection, index],
|
271
|
-
|
272
|
-
|
273
|
-
|
274
|
-
|
272
|
+
sl: collection[:sl],
|
273
|
+
sc: collection[:sc],
|
274
|
+
el: ending[:el],
|
275
|
+
ec: ending[:ec]
|
275
276
|
}
|
276
277
|
end
|
277
278
|
|
@@ -284,10 +285,10 @@ class Prettier::Parser < Ripper
|
|
284
285
|
{
|
285
286
|
type: :aref_field,
|
286
287
|
body: [collection, index],
|
287
|
-
|
288
|
-
|
289
|
-
|
290
|
-
|
288
|
+
sl: collection[:sl],
|
289
|
+
sc: collection[:sc],
|
290
|
+
el: ending[:el],
|
291
|
+
ec: ending[:ec]
|
291
292
|
}
|
292
293
|
end
|
293
294
|
|
@@ -298,10 +299,10 @@ class Prettier::Parser < Ripper
|
|
298
299
|
{
|
299
300
|
type: :args,
|
300
301
|
body: [],
|
301
|
-
|
302
|
-
|
303
|
-
|
304
|
-
|
302
|
+
sl: lineno,
|
303
|
+
sc: char_pos,
|
304
|
+
el: lineno,
|
305
|
+
ec: char_pos
|
305
306
|
}
|
306
307
|
end
|
307
308
|
|
@@ -313,11 +314,7 @@ class Prettier::Parser < Ripper
|
|
313
314
|
if args[:body].empty?
|
314
315
|
arg.merge(type: :args, body: [arg])
|
315
316
|
else
|
316
|
-
args.merge!(
|
317
|
-
body: args[:body] << arg,
|
318
|
-
end: arg[:end],
|
319
|
-
char_end: arg[:char_end]
|
320
|
-
)
|
317
|
+
args.merge!(body: args[:body] << arg, el: arg[:el], ec: arg[:ec])
|
321
318
|
end
|
322
319
|
end
|
323
320
|
|
@@ -330,8 +327,8 @@ class Prettier::Parser < Ripper
|
|
330
327
|
args.merge(
|
331
328
|
type: :args_add_block,
|
332
329
|
body: [args, block],
|
333
|
-
|
334
|
-
|
330
|
+
el: ending[:el],
|
331
|
+
ec: ending[:ec]
|
335
332
|
)
|
336
333
|
end
|
337
334
|
|
@@ -345,10 +342,10 @@ class Prettier::Parser < Ripper
|
|
345
342
|
{
|
346
343
|
type: :args_add_star,
|
347
344
|
body: [args, part],
|
348
|
-
|
349
|
-
|
350
|
-
|
351
|
-
|
345
|
+
sl: beging[:sl],
|
346
|
+
sc: beging[:sc],
|
347
|
+
el: ending[:el],
|
348
|
+
ec: ending[:ec]
|
352
349
|
}
|
353
350
|
end
|
354
351
|
|
@@ -367,15 +364,15 @@ class Prettier::Parser < Ripper
|
|
367
364
|
# If the arguments exceed the ending of the parentheses, then we know we
|
368
365
|
# have a heredoc in the arguments, and we need to use the bounds of the
|
369
366
|
# arguments to determine how large the arg_paren is.
|
370
|
-
ending = (args && args[:
|
367
|
+
ending = (args && args[:el] > rparen[:el]) ? args : rparen
|
371
368
|
|
372
369
|
{
|
373
370
|
type: :arg_paren,
|
374
371
|
body: [args],
|
375
|
-
|
376
|
-
|
377
|
-
|
378
|
-
|
372
|
+
sl: beging[:sl],
|
373
|
+
sc: beging[:sc],
|
374
|
+
el: ending[:el],
|
375
|
+
ec: ending[:ec]
|
379
376
|
}
|
380
377
|
end
|
381
378
|
|
@@ -391,20 +388,20 @@ class Prettier::Parser < Ripper
|
|
391
388
|
{
|
392
389
|
type: :array,
|
393
390
|
body: [contents],
|
394
|
-
|
395
|
-
|
396
|
-
|
397
|
-
|
391
|
+
sl: beging[:sl],
|
392
|
+
sc: beging[:sc],
|
393
|
+
el: ending[:el],
|
394
|
+
ec: ending[:ec]
|
398
395
|
}
|
399
396
|
else
|
400
397
|
ending = find_scanner_event(:@tstring_end)
|
401
|
-
contents[:
|
398
|
+
contents[:ec] = ending[:ec]
|
402
399
|
|
403
400
|
ending.merge!(
|
404
401
|
type: :array,
|
405
402
|
body: [contents],
|
406
|
-
|
407
|
-
|
403
|
+
sl: contents[:sl],
|
404
|
+
sc: contents[:sc]
|
408
405
|
)
|
409
406
|
end
|
410
407
|
end
|
@@ -417,10 +414,10 @@ class Prettier::Parser < Ripper
|
|
417
414
|
{
|
418
415
|
type: :aryptn,
|
419
416
|
body: [const, preargs, splatarg, postargs],
|
420
|
-
|
421
|
-
|
422
|
-
|
423
|
-
|
417
|
+
sl: pieces[0][:sl],
|
418
|
+
sc: pieces[0][:sc],
|
419
|
+
el: pieces[-1][:el],
|
420
|
+
ec: pieces[-1][:ec]
|
424
421
|
}
|
425
422
|
end
|
426
423
|
|
@@ -431,8 +428,8 @@ class Prettier::Parser < Ripper
|
|
431
428
|
left.merge(
|
432
429
|
type: :assign,
|
433
430
|
body: [left, right],
|
434
|
-
|
435
|
-
|
431
|
+
el: right[:el],
|
432
|
+
ec: right[:ec]
|
436
433
|
)
|
437
434
|
end
|
438
435
|
|
@@ -443,10 +440,10 @@ class Prettier::Parser < Ripper
|
|
443
440
|
{
|
444
441
|
type: :assoc_new,
|
445
442
|
body: [key, value],
|
446
|
-
|
447
|
-
|
448
|
-
|
449
|
-
|
443
|
+
sl: key[:sl],
|
444
|
+
sc: key[:sc],
|
445
|
+
el: value[:el],
|
446
|
+
ec: value[:ec]
|
450
447
|
}
|
451
448
|
end
|
452
449
|
|
@@ -456,8 +453,8 @@ class Prettier::Parser < Ripper
|
|
456
453
|
find_scanner_event(:@op, '**').merge!(
|
457
454
|
type: :assoc_splat,
|
458
455
|
body: [contents],
|
459
|
-
|
460
|
-
|
456
|
+
el: contents[:el],
|
457
|
+
ec: contents[:ec]
|
461
458
|
)
|
462
459
|
end
|
463
460
|
|
@@ -469,10 +466,10 @@ class Prettier::Parser < Ripper
|
|
469
466
|
{
|
470
467
|
type: :assoclist_from_args,
|
471
468
|
body: assocs,
|
472
|
-
|
473
|
-
|
474
|
-
|
475
|
-
|
469
|
+
sl: assocs[0][:sl],
|
470
|
+
sc: assocs[0][:sc],
|
471
|
+
el: assocs[-1][:el],
|
472
|
+
ec: assocs[-1][:ec]
|
476
473
|
}
|
477
474
|
end
|
478
475
|
|
@@ -484,10 +481,10 @@ class Prettier::Parser < Ripper
|
|
484
481
|
{
|
485
482
|
type: :bare_assoc_hash,
|
486
483
|
body: assoc_news,
|
487
|
-
|
488
|
-
|
489
|
-
|
490
|
-
|
484
|
+
sl: assoc_news[0][:sl],
|
485
|
+
sc: assoc_news[0][:sc],
|
486
|
+
el: assoc_news[-1][:el],
|
487
|
+
ec: assoc_news[-1][:ec]
|
491
488
|
}
|
492
489
|
end
|
493
490
|
|
@@ -495,20 +492,20 @@ class Prettier::Parser < Ripper
|
|
495
492
|
# It includes a bodystmt event that has all of the consequent clauses.
|
496
493
|
def on_begin(bodystmt)
|
497
494
|
beging = find_scanner_event(:@kw, 'begin')
|
498
|
-
|
495
|
+
ec =
|
499
496
|
if bodystmt[:body][1..-1].any?
|
500
|
-
bodystmt[:
|
497
|
+
bodystmt[:ec]
|
501
498
|
else
|
502
|
-
find_scanner_event(:@kw, 'end')[:
|
499
|
+
find_scanner_event(:@kw, 'end')[:ec]
|
503
500
|
end
|
504
501
|
|
505
|
-
bodystmt.bind(beging[:
|
502
|
+
bodystmt.bind(beging[:ec], ec)
|
506
503
|
|
507
504
|
beging.merge!(
|
508
505
|
type: :begin,
|
509
506
|
body: [bodystmt],
|
510
|
-
|
511
|
-
|
507
|
+
el: bodystmt[:el],
|
508
|
+
ec: bodystmt[:ec]
|
512
509
|
)
|
513
510
|
end
|
514
511
|
|
@@ -518,10 +515,10 @@ class Prettier::Parser < Ripper
|
|
518
515
|
{
|
519
516
|
type: :binary,
|
520
517
|
body: [left, oper, right],
|
521
|
-
|
522
|
-
|
523
|
-
|
524
|
-
|
518
|
+
sl: left[:sl],
|
519
|
+
sc: left[:sc],
|
520
|
+
el: right[:el],
|
521
|
+
ec: right[:ec]
|
525
522
|
}
|
526
523
|
end
|
527
524
|
|
@@ -531,7 +528,7 @@ class Prettier::Parser < Ripper
|
|
531
528
|
index =
|
532
529
|
scanner_events.rindex do |event|
|
533
530
|
event[:type] == :@op && %w[| ||].include?(event[:body]) &&
|
534
|
-
event[:
|
531
|
+
event[:sc] < params[:sc]
|
535
532
|
end
|
536
533
|
|
537
534
|
beging = scanner_events[index]
|
@@ -540,10 +537,10 @@ class Prettier::Parser < Ripper
|
|
540
537
|
{
|
541
538
|
type: :block_var,
|
542
539
|
body: [params, locals],
|
543
|
-
|
544
|
-
|
545
|
-
|
546
|
-
|
540
|
+
sl: beging[:sl],
|
541
|
+
sc: beging[:sc],
|
542
|
+
el: ending[:el],
|
543
|
+
ec: ending[:ec]
|
547
544
|
}
|
548
545
|
end
|
549
546
|
|
@@ -553,8 +550,8 @@ class Prettier::Parser < Ripper
|
|
553
550
|
find_scanner_event(:@op, '&').merge!(
|
554
551
|
type: :blockarg,
|
555
552
|
body: [ident],
|
556
|
-
|
557
|
-
|
553
|
+
el: ident[:el],
|
554
|
+
ec: ident[:ec]
|
558
555
|
)
|
559
556
|
end
|
560
557
|
|
@@ -562,21 +559,18 @@ class Prettier::Parser < Ripper
|
|
562
559
|
# doesn't necessarily know where it started. So the parent node needs to
|
563
560
|
# report back down into this one where it goes.
|
564
561
|
class BodyStmt < SimpleDelegator
|
565
|
-
def bind(
|
566
|
-
merge!(
|
562
|
+
def bind(sc, ec)
|
563
|
+
merge!(sc: sc, ec: ec)
|
567
564
|
parts = self[:body]
|
568
565
|
|
569
566
|
# Here we're going to determine the bounds for the stmts
|
570
567
|
consequent = parts[1..-1].compact.first
|
571
|
-
self[:body][0].bind(
|
572
|
-
char_start,
|
573
|
-
consequent ? consequent[:char_start] : char_end
|
574
|
-
)
|
568
|
+
self[:body][0].bind(sc, consequent ? consequent[:sc] : ec)
|
575
569
|
|
576
570
|
# Next we're going to determine the rescue clause if there is one
|
577
571
|
if parts[1]
|
578
572
|
consequent = parts[2..-1].compact.first
|
579
|
-
self[:body][1].bind_end(consequent ? consequent[:
|
573
|
+
self[:body][1].bind_end(consequent ? consequent[:sc] : ec)
|
580
574
|
end
|
581
575
|
end
|
582
576
|
end
|
@@ -587,10 +581,10 @@ class Prettier::Parser < Ripper
|
|
587
581
|
BodyStmt.new(
|
588
582
|
type: :bodystmt,
|
589
583
|
body: [stmts, rescued, ensured, elsed],
|
590
|
-
|
591
|
-
|
592
|
-
|
593
|
-
|
584
|
+
sl: lineno,
|
585
|
+
sc: char_pos,
|
586
|
+
el: lineno,
|
587
|
+
ec: char_pos
|
594
588
|
)
|
595
589
|
end
|
596
590
|
|
@@ -602,15 +596,15 @@ class Prettier::Parser < Ripper
|
|
602
596
|
beging = find_scanner_event(:@lbrace)
|
603
597
|
ending = find_scanner_event(:@rbrace)
|
604
598
|
|
605
|
-
stmts.bind((block_var || beging)[:
|
599
|
+
stmts.bind((block_var || beging)[:ec], ending[:sc])
|
606
600
|
|
607
601
|
{
|
608
602
|
type: :brace_block,
|
609
603
|
body: [block_var, stmts],
|
610
|
-
|
611
|
-
|
612
|
-
|
613
|
-
|
604
|
+
sl: beging[:sl],
|
605
|
+
sc: beging[:sc],
|
606
|
+
el: ending[:el],
|
607
|
+
ec: ending[:ec]
|
614
608
|
}
|
615
609
|
end
|
616
610
|
|
@@ -630,8 +624,8 @@ class Prettier::Parser < Ripper
|
|
630
624
|
beging.merge!(
|
631
625
|
type: :break,
|
632
626
|
body: [args_add_block],
|
633
|
-
|
634
|
-
|
627
|
+
el: args_add_block[:el],
|
628
|
+
ec: args_add_block[:ec]
|
635
629
|
)
|
636
630
|
end
|
637
631
|
|
@@ -647,10 +641,6 @@ class Prettier::Parser < Ripper
|
|
647
641
|
# foo.(1, 2, 3)
|
648
642
|
#
|
649
643
|
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
644
|
ending = sending
|
655
645
|
|
656
646
|
if sending == :call
|
@@ -664,10 +654,10 @@ class Prettier::Parser < Ripper
|
|
664
654
|
{
|
665
655
|
type: :call,
|
666
656
|
body: [receiver, oper, sending],
|
667
|
-
|
668
|
-
|
669
|
-
|
670
|
-
|
657
|
+
sl: receiver[:sl],
|
658
|
+
sc: receiver[:sc],
|
659
|
+
el: ending[:el],
|
660
|
+
ec: ending[:ec]
|
671
661
|
}
|
672
662
|
end
|
673
663
|
|
@@ -685,8 +675,8 @@ class Prettier::Parser < Ripper
|
|
685
675
|
|
686
676
|
beging.merge!(
|
687
677
|
body: [switch, consequent],
|
688
|
-
|
689
|
-
|
678
|
+
el: consequent[:el],
|
679
|
+
ec: consequent[:ec]
|
690
680
|
)
|
691
681
|
end
|
692
682
|
|
@@ -720,17 +710,17 @@ class Prettier::Parser < Ripper
|
|
720
710
|
ending = find_scanner_event(:@kw, 'end')
|
721
711
|
|
722
712
|
bodystmt.bind(
|
723
|
-
find_next_statement_start((superclass || const)[:
|
724
|
-
ending[:
|
713
|
+
find_next_statement_start((superclass || const)[:ec]),
|
714
|
+
ending[:sc]
|
725
715
|
)
|
726
716
|
|
727
717
|
{
|
728
718
|
type: :class,
|
729
719
|
body: [const, superclass, bodystmt],
|
730
|
-
|
731
|
-
|
732
|
-
|
733
|
-
|
720
|
+
sl: beging[:sl],
|
721
|
+
sc: beging[:sc],
|
722
|
+
el: ending[:el],
|
723
|
+
ec: ending[:ec]
|
734
724
|
}
|
735
725
|
end
|
736
726
|
|
@@ -741,10 +731,10 @@ class Prettier::Parser < Ripper
|
|
741
731
|
{
|
742
732
|
type: :command,
|
743
733
|
body: [ident, args],
|
744
|
-
|
745
|
-
|
746
|
-
|
747
|
-
|
734
|
+
sl: ident[:sl],
|
735
|
+
sc: ident[:sc],
|
736
|
+
el: args[:el],
|
737
|
+
ec: args[:ec]
|
748
738
|
}
|
749
739
|
end
|
750
740
|
|
@@ -758,10 +748,10 @@ class Prettier::Parser < Ripper
|
|
758
748
|
{
|
759
749
|
type: :command_call,
|
760
750
|
body: [receiver, oper, ident, args],
|
761
|
-
|
762
|
-
|
763
|
-
|
764
|
-
|
751
|
+
sl: receiver[:sl],
|
752
|
+
sc: receiver[:sc],
|
753
|
+
el: ending[:el],
|
754
|
+
ec: ending[:ec]
|
765
755
|
}
|
766
756
|
end
|
767
757
|
|
@@ -775,10 +765,10 @@ class Prettier::Parser < Ripper
|
|
775
765
|
{
|
776
766
|
type: :const_path_field,
|
777
767
|
body: [left, const],
|
778
|
-
|
779
|
-
|
780
|
-
|
781
|
-
|
768
|
+
sl: left[:sl],
|
769
|
+
sc: left[:sc],
|
770
|
+
el: const[:el],
|
771
|
+
ec: const[:ec]
|
782
772
|
}
|
783
773
|
end
|
784
774
|
|
@@ -792,10 +782,10 @@ class Prettier::Parser < Ripper
|
|
792
782
|
{
|
793
783
|
type: :const_path_ref,
|
794
784
|
body: [left, const],
|
795
|
-
|
796
|
-
|
797
|
-
|
798
|
-
|
785
|
+
sl: left[:sl],
|
786
|
+
sc: left[:sc],
|
787
|
+
el: const[:el],
|
788
|
+
ec: const[:ec]
|
799
789
|
}
|
800
790
|
end
|
801
791
|
|
@@ -845,33 +835,30 @@ class Prettier::Parser < Ripper
|
|
845
835
|
{
|
846
836
|
type: :defsl,
|
847
837
|
body: [ident, params, bodystmt],
|
848
|
-
|
849
|
-
|
850
|
-
|
851
|
-
|
838
|
+
sl: beging[:sl],
|
839
|
+
sc: beging[:sc],
|
840
|
+
el: bodystmt[:el],
|
841
|
+
ec: bodystmt[:ec]
|
852
842
|
}
|
853
843
|
)
|
854
844
|
end
|
855
845
|
|
856
846
|
if params[:type] == :params && !params[:body].any?
|
857
|
-
location = ident[:
|
858
|
-
params.merge!(
|
847
|
+
location = ident[:ec]
|
848
|
+
params.merge!(sc: location, ec: location)
|
859
849
|
end
|
860
850
|
|
861
851
|
ending = find_scanner_event(:@kw, 'end')
|
862
852
|
|
863
|
-
bodystmt.bind(
|
864
|
-
find_next_statement_start(params[:char_end]),
|
865
|
-
ending[:char_start]
|
866
|
-
)
|
853
|
+
bodystmt.bind(find_next_statement_start(params[:ec]), ending[:sc])
|
867
854
|
|
868
855
|
{
|
869
856
|
type: :def,
|
870
857
|
body: [ident, params, bodystmt],
|
871
|
-
|
872
|
-
|
873
|
-
|
874
|
-
|
858
|
+
sl: beging[:sl],
|
859
|
+
sc: beging[:sc],
|
860
|
+
el: ending[:el],
|
861
|
+
ec: ending[:ec]
|
875
862
|
}
|
876
863
|
end
|
877
864
|
|
@@ -896,25 +883,22 @@ class Prettier::Parser < Ripper
|
|
896
883
|
scanner_events.delete(ident)
|
897
884
|
|
898
885
|
if params[:type] == :params && !params[:body].any?
|
899
|
-
location = ident[:
|
900
|
-
params.merge!(
|
886
|
+
location = ident[:ec]
|
887
|
+
params.merge!(sc: location, ec: location)
|
901
888
|
end
|
902
889
|
|
903
890
|
beging = find_scanner_event(:@kw, 'def')
|
904
891
|
ending = find_scanner_event(:@kw, 'end')
|
905
892
|
|
906
|
-
bodystmt.bind(
|
907
|
-
find_next_statement_start(params[:char_end]),
|
908
|
-
ending[:char_start]
|
909
|
-
)
|
893
|
+
bodystmt.bind(find_next_statement_start(params[:ec]), ending[:sc])
|
910
894
|
|
911
895
|
{
|
912
896
|
type: :defs,
|
913
897
|
body: [target, oper, ident, params, bodystmt],
|
914
|
-
|
915
|
-
|
916
|
-
|
917
|
-
|
898
|
+
sl: beging[:sl],
|
899
|
+
sc: beging[:sc],
|
900
|
+
el: ending[:el],
|
901
|
+
ec: ending[:ec]
|
918
902
|
}
|
919
903
|
end
|
920
904
|
|
@@ -925,14 +909,14 @@ class Prettier::Parser < Ripper
|
|
925
909
|
def on_defined(value)
|
926
910
|
beging = find_scanner_event(:@kw, 'defined?')
|
927
911
|
|
928
|
-
paren = source[beging[:
|
912
|
+
paren = source[beging[:ec]...value[:sc]].include?('(')
|
929
913
|
ending = paren ? find_scanner_event(:@rparen) : value
|
930
914
|
|
931
915
|
beging.merge!(
|
932
916
|
type: :defined,
|
933
917
|
body: [value],
|
934
|
-
|
935
|
-
|
918
|
+
el: ending[:el],
|
919
|
+
ec: ending[:ec]
|
936
920
|
)
|
937
921
|
end
|
938
922
|
|
@@ -944,15 +928,15 @@ class Prettier::Parser < Ripper
|
|
944
928
|
beging = find_scanner_event(:@kw, 'do')
|
945
929
|
ending = find_scanner_event(:@kw, 'end')
|
946
930
|
|
947
|
-
bodystmt.bind((block_var || beging)[:
|
931
|
+
bodystmt.bind((block_var || beging)[:ec], ending[:sc])
|
948
932
|
|
949
933
|
{
|
950
934
|
type: :do_block,
|
951
935
|
body: [block_var, bodystmt],
|
952
|
-
|
953
|
-
|
954
|
-
|
955
|
-
|
936
|
+
sl: beging[:sl],
|
937
|
+
sc: beging[:sc],
|
938
|
+
el: ending[:el],
|
939
|
+
ec: ending[:ec]
|
956
940
|
}
|
957
941
|
end
|
958
942
|
|
@@ -968,10 +952,10 @@ class Prettier::Parser < Ripper
|
|
968
952
|
{
|
969
953
|
type: :dot2,
|
970
954
|
body: [left, right],
|
971
|
-
|
972
|
-
|
973
|
-
|
974
|
-
|
955
|
+
sl: beging[:sl],
|
956
|
+
sc: beging[:sc],
|
957
|
+
el: ending[:el],
|
958
|
+
ec: ending[:ec]
|
975
959
|
}
|
976
960
|
end
|
977
961
|
|
@@ -987,10 +971,10 @@ class Prettier::Parser < Ripper
|
|
987
971
|
{
|
988
972
|
type: :dot3,
|
989
973
|
body: [left, right],
|
990
|
-
|
991
|
-
|
992
|
-
|
993
|
-
|
974
|
+
sl: beging[:sl],
|
975
|
+
sc: beging[:sc],
|
976
|
+
el: ending[:el],
|
977
|
+
ec: ending[:ec]
|
994
978
|
}
|
995
979
|
end
|
996
980
|
|
@@ -1022,8 +1006,8 @@ class Prettier::Parser < Ripper
|
|
1022
1006
|
type: :dyna_symbol,
|
1023
1007
|
quote: beging[:body][1],
|
1024
1008
|
body: string[:body],
|
1025
|
-
|
1026
|
-
|
1009
|
+
el: ending[:el],
|
1010
|
+
ec: ending[:ec]
|
1027
1011
|
)
|
1028
1012
|
else
|
1029
1013
|
# A dynamic symbol as a hash key
|
@@ -1033,10 +1017,10 @@ class Prettier::Parser < Ripper
|
|
1033
1017
|
string.merge!(
|
1034
1018
|
type: :dyna_symbol,
|
1035
1019
|
quote: ending[:body][0],
|
1036
|
-
|
1037
|
-
|
1038
|
-
|
1039
|
-
|
1020
|
+
sl: beging[:sl],
|
1021
|
+
sc: beging[:sc],
|
1022
|
+
el: ending[:el],
|
1023
|
+
ec: ending[:ec]
|
1040
1024
|
)
|
1041
1025
|
end
|
1042
1026
|
end
|
@@ -1061,15 +1045,15 @@ class Prettier::Parser < Ripper
|
|
1061
1045
|
beging = find_scanner_event(:@kw, 'else')
|
1062
1046
|
ending = find_else_ending
|
1063
1047
|
|
1064
|
-
stmts.bind(beging[:
|
1048
|
+
stmts.bind(beging[:ec], ending[:sc])
|
1065
1049
|
|
1066
1050
|
{
|
1067
1051
|
type: :else,
|
1068
1052
|
body: [stmts],
|
1069
|
-
|
1070
|
-
|
1071
|
-
|
1072
|
-
|
1053
|
+
sl: beging[:sl],
|
1054
|
+
sc: beging[:sc],
|
1055
|
+
el: ending[:el],
|
1056
|
+
ec: ending[:ec]
|
1073
1057
|
}
|
1074
1058
|
end
|
1075
1059
|
|
@@ -1081,15 +1065,15 @@ class Prettier::Parser < Ripper
|
|
1081
1065
|
beging = find_scanner_event(:@kw, 'elsif')
|
1082
1066
|
ending = consequent || find_scanner_event(:@kw, 'end')
|
1083
1067
|
|
1084
|
-
stmts.bind(predicate[:
|
1068
|
+
stmts.bind(predicate[:ec], ending[:sc])
|
1085
1069
|
|
1086
1070
|
{
|
1087
1071
|
type: :elsif,
|
1088
1072
|
body: [predicate, stmts, consequent],
|
1089
|
-
|
1090
|
-
|
1091
|
-
|
1092
|
-
|
1073
|
+
sl: beging[:sl],
|
1074
|
+
sc: beging[:sc],
|
1075
|
+
el: ending[:el],
|
1076
|
+
ec: ending[:ec]
|
1093
1077
|
}
|
1094
1078
|
end
|
1095
1079
|
|
@@ -1099,12 +1083,7 @@ class Prettier::Parser < Ripper
|
|
1099
1083
|
# and add to it as we get content. It always starts with this scanner
|
1100
1084
|
# event, so here we'll initialize the current embdoc.
|
1101
1085
|
def on_embdoc_beg(value)
|
1102
|
-
@embdoc = {
|
1103
|
-
type: :@embdoc,
|
1104
|
-
value: value,
|
1105
|
-
start: lineno,
|
1106
|
-
char_start: char_pos
|
1107
|
-
}
|
1086
|
+
@embdoc = { type: :@embdoc, value: value, sl: lineno, sc: char_pos }
|
1108
1087
|
end
|
1109
1088
|
|
1110
1089
|
# This is a scanner event that gets hit when we're inside an embdoc and
|
@@ -1123,8 +1102,8 @@ class Prettier::Parser < Ripper
|
|
1123
1102
|
@comments <<
|
1124
1103
|
@embdoc.merge!(
|
1125
1104
|
value: @embdoc[:value] << value.chomp,
|
1126
|
-
|
1127
|
-
|
1105
|
+
el: lineno,
|
1106
|
+
ec: char_pos + value.length - 1
|
1128
1107
|
)
|
1129
1108
|
|
1130
1109
|
@embdoc = nil
|
@@ -1143,18 +1122,15 @@ class Prettier::Parser < Ripper
|
|
1143
1122
|
end
|
1144
1123
|
|
1145
1124
|
ending = scanner_events[index]
|
1146
|
-
stmts.bind(
|
1147
|
-
find_next_statement_start(beging[:char_end]),
|
1148
|
-
ending[:char_start]
|
1149
|
-
)
|
1125
|
+
stmts.bind(find_next_statement_start(beging[:ec]), ending[:sc])
|
1150
1126
|
|
1151
1127
|
{
|
1152
1128
|
type: :ensure,
|
1153
1129
|
body: [beging, stmts],
|
1154
|
-
|
1155
|
-
|
1156
|
-
|
1157
|
-
|
1130
|
+
sl: beging[:sl],
|
1131
|
+
sc: beging[:sc],
|
1132
|
+
el: ending[:el],
|
1133
|
+
ec: ending[:ec]
|
1158
1134
|
}
|
1159
1135
|
end
|
1160
1136
|
|
@@ -1182,10 +1158,10 @@ class Prettier::Parser < Ripper
|
|
1182
1158
|
{
|
1183
1159
|
type: :field,
|
1184
1160
|
body: [left, oper, right],
|
1185
|
-
|
1186
|
-
|
1187
|
-
|
1188
|
-
|
1161
|
+
sl: left[:sl],
|
1162
|
+
sc: left[:sc],
|
1163
|
+
el: right[:el],
|
1164
|
+
ec: right[:ec]
|
1189
1165
|
}
|
1190
1166
|
end
|
1191
1167
|
|
@@ -1195,15 +1171,13 @@ class Prettier::Parser < Ripper
|
|
1195
1171
|
beging = const || find_scanner_event(:@lbracket)
|
1196
1172
|
ending = find_scanner_event(:@rbracket)
|
1197
1173
|
|
1198
|
-
pieces = [const, presplat, *args, postsplat].compact
|
1199
|
-
|
1200
1174
|
{
|
1201
1175
|
type: :fndptn,
|
1202
1176
|
body: [const, presplat, args, postsplat],
|
1203
|
-
|
1204
|
-
|
1205
|
-
|
1206
|
-
|
1177
|
+
sl: beging[:sl],
|
1178
|
+
sc: beging[:sc],
|
1179
|
+
el: ending[:el],
|
1180
|
+
ec: ending[:ec]
|
1207
1181
|
}
|
1208
1182
|
end
|
1209
1183
|
|
@@ -1215,15 +1189,15 @@ class Prettier::Parser < Ripper
|
|
1215
1189
|
beging = find_scanner_event(:@kw, 'for')
|
1216
1190
|
ending = find_scanner_event(:@kw, 'end')
|
1217
1191
|
|
1218
|
-
stmts.bind(enumerable[:
|
1192
|
+
stmts.bind(enumerable[:ec], ending[:sc])
|
1219
1193
|
|
1220
1194
|
{
|
1221
1195
|
type: :for,
|
1222
1196
|
body: [ident, enumerable, stmts],
|
1223
|
-
|
1224
|
-
|
1225
|
-
|
1226
|
-
|
1197
|
+
sl: beging[:sl],
|
1198
|
+
sc: beging[:sc],
|
1199
|
+
el: ending[:el],
|
1200
|
+
ec: ending[:ec]
|
1227
1201
|
}
|
1228
1202
|
end
|
1229
1203
|
|
@@ -1237,19 +1211,16 @@ class Prettier::Parser < Ripper
|
|
1237
1211
|
if assoclist_from_args
|
1238
1212
|
# Here we're going to expand out the location information for the assocs
|
1239
1213
|
# node so that it can grab up any remaining comments inside the hash.
|
1240
|
-
assoclist_from_args.merge!(
|
1241
|
-
char_start: beging[:char_end],
|
1242
|
-
char_end: ending[:char_start]
|
1243
|
-
)
|
1214
|
+
assoclist_from_args.merge!(sc: beging[:ec], ec: ending[:sc])
|
1244
1215
|
end
|
1245
1216
|
|
1246
1217
|
{
|
1247
1218
|
type: :hash,
|
1248
1219
|
body: [assoclist_from_args],
|
1249
|
-
|
1250
|
-
|
1251
|
-
|
1252
|
-
|
1220
|
+
sl: beging[:sl],
|
1221
|
+
sc: beging[:sc],
|
1222
|
+
el: ending[:el],
|
1223
|
+
ec: ending[:ec]
|
1253
1224
|
}
|
1254
1225
|
end
|
1255
1226
|
|
@@ -1261,10 +1232,10 @@ class Prettier::Parser < Ripper
|
|
1261
1232
|
# printer through our embed function.
|
1262
1233
|
def on_heredoc_beg(beging)
|
1263
1234
|
location = {
|
1264
|
-
|
1265
|
-
|
1266
|
-
|
1267
|
-
|
1235
|
+
sl: lineno,
|
1236
|
+
el: lineno,
|
1237
|
+
sc: char_pos,
|
1238
|
+
ec: char_pos + beging.length + 1
|
1268
1239
|
}
|
1269
1240
|
|
1270
1241
|
# Here we're going to artificially create an extra node type so that if
|
@@ -1286,7 +1257,7 @@ class Prettier::Parser < Ripper
|
|
1286
1257
|
|
1287
1258
|
# This is a scanner event that represents the end of the heredoc.
|
1288
1259
|
def on_heredoc_end(ending)
|
1289
|
-
@heredocs[-1].merge!(ending: ending.chomp,
|
1260
|
+
@heredocs[-1].merge!(ending: ending.chomp, el: lineno, ec: char_pos)
|
1290
1261
|
end
|
1291
1262
|
|
1292
1263
|
# hshptn is a parser event that represents matching against a hash pattern
|
@@ -1297,10 +1268,10 @@ class Prettier::Parser < Ripper
|
|
1297
1268
|
{
|
1298
1269
|
type: :hshptn,
|
1299
1270
|
body: [const, kw, kwrest],
|
1300
|
-
|
1301
|
-
|
1302
|
-
|
1303
|
-
|
1271
|
+
sl: pieces[0][:sl],
|
1272
|
+
sc: pieces[0][:sc],
|
1273
|
+
el: pieces[-1][:el],
|
1274
|
+
ec: pieces[-1][:ec]
|
1304
1275
|
}
|
1305
1276
|
end
|
1306
1277
|
|
@@ -1311,15 +1282,15 @@ class Prettier::Parser < Ripper
|
|
1311
1282
|
beging = find_scanner_event(:@kw, 'if')
|
1312
1283
|
ending = consequent || find_scanner_event(:@kw, 'end')
|
1313
1284
|
|
1314
|
-
stmts.bind(predicate[:
|
1285
|
+
stmts.bind(predicate[:ec], ending[:sc])
|
1315
1286
|
|
1316
1287
|
{
|
1317
1288
|
type: :if,
|
1318
1289
|
body: [predicate, stmts, consequent],
|
1319
|
-
|
1320
|
-
|
1321
|
-
|
1322
|
-
|
1290
|
+
sl: beging[:sl],
|
1291
|
+
sc: beging[:sc],
|
1292
|
+
el: ending[:el],
|
1293
|
+
ec: ending[:ec]
|
1323
1294
|
}
|
1324
1295
|
end
|
1325
1296
|
|
@@ -1330,8 +1301,8 @@ class Prettier::Parser < Ripper
|
|
1330
1301
|
predicate.merge(
|
1331
1302
|
type: :ifop,
|
1332
1303
|
body: [predicate, truthy, falsy],
|
1333
|
-
|
1334
|
-
|
1304
|
+
el: falsy[:el],
|
1305
|
+
ec: falsy[:ec]
|
1335
1306
|
)
|
1336
1307
|
end
|
1337
1308
|
|
@@ -1344,10 +1315,10 @@ class Prettier::Parser < Ripper
|
|
1344
1315
|
{
|
1345
1316
|
type: :if_mod,
|
1346
1317
|
body: [predicate, statement],
|
1347
|
-
|
1348
|
-
|
1349
|
-
|
1350
|
-
|
1318
|
+
sl: statement[:sl],
|
1319
|
+
sc: statement[:sc],
|
1320
|
+
el: predicate[:el],
|
1321
|
+
ec: predicate[:ec]
|
1351
1322
|
}
|
1352
1323
|
end
|
1353
1324
|
|
@@ -1361,13 +1332,13 @@ class Prettier::Parser < Ripper
|
|
1361
1332
|
beging = find_scanner_event(:@kw, 'in')
|
1362
1333
|
ending = consequent || find_scanner_event(:@kw, 'end')
|
1363
1334
|
|
1364
|
-
stmts.bind(beging[:
|
1335
|
+
stmts.bind(beging[:ec], ending[:sc])
|
1365
1336
|
|
1366
1337
|
beging.merge!(
|
1367
1338
|
type: :in,
|
1368
1339
|
body: [pattern, stmts, consequent],
|
1369
|
-
|
1370
|
-
|
1340
|
+
el: ending[:el],
|
1341
|
+
ec: ending[:ec]
|
1371
1342
|
)
|
1372
1343
|
end
|
1373
1344
|
|
@@ -1380,8 +1351,8 @@ class Prettier::Parser < Ripper
|
|
1380
1351
|
oper.merge!(
|
1381
1352
|
type: :kwrest_param,
|
1382
1353
|
body: [ident],
|
1383
|
-
|
1384
|
-
|
1354
|
+
el: ident[:el],
|
1355
|
+
ec: ident[:ec]
|
1385
1356
|
)
|
1386
1357
|
end
|
1387
1358
|
|
@@ -1403,15 +1374,15 @@ class Prettier::Parser < Ripper
|
|
1403
1374
|
closing = find_scanner_event(:@kw, 'end')
|
1404
1375
|
end
|
1405
1376
|
|
1406
|
-
stmts.bind(opening[:
|
1377
|
+
stmts.bind(opening[:ec], closing[:sc])
|
1407
1378
|
|
1408
1379
|
{
|
1409
1380
|
type: :lambda,
|
1410
1381
|
body: [params, stmts],
|
1411
|
-
|
1412
|
-
|
1413
|
-
|
1414
|
-
|
1382
|
+
sl: beging[:sl],
|
1383
|
+
sc: beging[:sc],
|
1384
|
+
el: closing[:el],
|
1385
|
+
ec: closing[:ec]
|
1415
1386
|
}
|
1416
1387
|
end
|
1417
1388
|
|
@@ -1433,17 +1404,15 @@ class Prettier::Parser < Ripper
|
|
1433
1404
|
# in which case we need to explicitly track the comma and add it onto the
|
1434
1405
|
# child node.
|
1435
1406
|
def on_massign(left, right)
|
1436
|
-
if source[left[:
|
1437
|
-
left[:comma] = true
|
1438
|
-
end
|
1407
|
+
left[:comma] = true if source[left[:ec]...right[:sc]].strip.start_with?(',')
|
1439
1408
|
|
1440
1409
|
{
|
1441
1410
|
type: :massign,
|
1442
1411
|
body: [left, right],
|
1443
|
-
|
1444
|
-
|
1445
|
-
|
1446
|
-
|
1412
|
+
sl: left[:sl],
|
1413
|
+
sc: left[:sc],
|
1414
|
+
el: right[:el],
|
1415
|
+
ec: right[:ec]
|
1447
1416
|
}
|
1448
1417
|
end
|
1449
1418
|
|
@@ -1462,10 +1431,10 @@ class Prettier::Parser < Ripper
|
|
1462
1431
|
{
|
1463
1432
|
type: :method_add_arg,
|
1464
1433
|
body: [fcall, arg_paren],
|
1465
|
-
|
1466
|
-
|
1467
|
-
|
1468
|
-
|
1434
|
+
sl: fcall[:sl],
|
1435
|
+
sc: fcall[:sc],
|
1436
|
+
el: arg_paren[:el],
|
1437
|
+
ec: arg_paren[:ec]
|
1469
1438
|
}
|
1470
1439
|
end
|
1471
1440
|
|
@@ -1476,10 +1445,10 @@ class Prettier::Parser < Ripper
|
|
1476
1445
|
{
|
1477
1446
|
type: :method_add_block,
|
1478
1447
|
body: [method_add_arg, block],
|
1479
|
-
|
1480
|
-
|
1481
|
-
|
1482
|
-
|
1448
|
+
sl: method_add_arg[:sl],
|
1449
|
+
sc: method_add_arg[:sc],
|
1450
|
+
el: block[:el],
|
1451
|
+
ec: block[:ec]
|
1483
1452
|
}
|
1484
1453
|
end
|
1485
1454
|
|
@@ -1490,10 +1459,10 @@ class Prettier::Parser < Ripper
|
|
1490
1459
|
{
|
1491
1460
|
type: :mlhs,
|
1492
1461
|
body: [],
|
1493
|
-
|
1494
|
-
|
1495
|
-
|
1496
|
-
|
1462
|
+
sl: lineno,
|
1463
|
+
sc: char_pos,
|
1464
|
+
el: lineno,
|
1465
|
+
ec: char_pos
|
1497
1466
|
}
|
1498
1467
|
end
|
1499
1468
|
|
@@ -1504,11 +1473,7 @@ class Prettier::Parser < Ripper
|
|
1504
1473
|
if mlhs[:body].empty?
|
1505
1474
|
part.merge(type: :mlhs, body: [part])
|
1506
1475
|
else
|
1507
|
-
mlhs.merge!(
|
1508
|
-
body: mlhs[:body] << part,
|
1509
|
-
end: part[:end],
|
1510
|
-
char_end: part[:char_end]
|
1511
|
-
)
|
1476
|
+
mlhs.merge!(body: mlhs[:body] << part, el: part[:el], ec: part[:ec])
|
1512
1477
|
end
|
1513
1478
|
end
|
1514
1479
|
|
@@ -1521,8 +1486,8 @@ class Prettier::Parser < Ripper
|
|
1521
1486
|
mlhs_add_star.merge(
|
1522
1487
|
type: :mlhs_add_post,
|
1523
1488
|
body: [mlhs_add_star, mlhs],
|
1524
|
-
|
1525
|
-
|
1489
|
+
el: mlhs[:el],
|
1490
|
+
ec: mlhs[:ec]
|
1526
1491
|
)
|
1527
1492
|
end
|
1528
1493
|
|
@@ -1537,10 +1502,10 @@ class Prettier::Parser < Ripper
|
|
1537
1502
|
{
|
1538
1503
|
type: :mlhs_add_star,
|
1539
1504
|
body: [mlhs, part],
|
1540
|
-
|
1541
|
-
|
1542
|
-
|
1543
|
-
|
1505
|
+
sl: beging[:sl],
|
1506
|
+
sc: beging[:sc],
|
1507
|
+
el: ending[:el],
|
1508
|
+
ec: ending[:ec]
|
1544
1509
|
}
|
1545
1510
|
end
|
1546
1511
|
|
@@ -1552,17 +1517,17 @@ class Prettier::Parser < Ripper
|
|
1552
1517
|
beging = find_scanner_event(:@lparen)
|
1553
1518
|
ending = find_scanner_event(:@rparen)
|
1554
1519
|
|
1555
|
-
if source[beging[:
|
1520
|
+
if source[beging[:ec]...ending[:sc]].strip.end_with?(',')
|
1556
1521
|
contents[:comma] = true
|
1557
1522
|
end
|
1558
1523
|
|
1559
1524
|
{
|
1560
1525
|
type: :mlhs_paren,
|
1561
1526
|
body: [contents],
|
1562
|
-
|
1563
|
-
|
1564
|
-
|
1565
|
-
|
1527
|
+
sl: beging[:sl],
|
1528
|
+
sc: beging[:sc],
|
1529
|
+
el: ending[:el],
|
1530
|
+
ec: ending[:ec]
|
1566
1531
|
}
|
1567
1532
|
end
|
1568
1533
|
|
@@ -1573,18 +1538,15 @@ class Prettier::Parser < Ripper
|
|
1573
1538
|
beging = find_scanner_event(:@kw, 'module')
|
1574
1539
|
ending = find_scanner_event(:@kw, 'end')
|
1575
1540
|
|
1576
|
-
bodystmt.bind(
|
1577
|
-
find_next_statement_start(const[:char_end]),
|
1578
|
-
ending[:char_start]
|
1579
|
-
)
|
1541
|
+
bodystmt.bind(find_next_statement_start(const[:ec]), ending[:sc])
|
1580
1542
|
|
1581
1543
|
{
|
1582
1544
|
type: :module,
|
1583
1545
|
body: [const, bodystmt],
|
1584
|
-
|
1585
|
-
|
1586
|
-
|
1587
|
-
|
1546
|
+
sl: beging[:sl],
|
1547
|
+
sc: beging[:sc],
|
1548
|
+
el: ending[:el],
|
1549
|
+
ec: ending[:ec]
|
1588
1550
|
}
|
1589
1551
|
end
|
1590
1552
|
|
@@ -1596,10 +1558,10 @@ class Prettier::Parser < Ripper
|
|
1596
1558
|
{
|
1597
1559
|
type: :mrhs,
|
1598
1560
|
body: [],
|
1599
|
-
|
1600
|
-
|
1601
|
-
|
1602
|
-
|
1561
|
+
sl: lineno,
|
1562
|
+
sc: char_pos,
|
1563
|
+
el: lineno,
|
1564
|
+
ec: char_pos
|
1603
1565
|
}
|
1604
1566
|
end
|
1605
1567
|
|
@@ -1609,11 +1571,7 @@ class Prettier::Parser < Ripper
|
|
1609
1571
|
if mrhs[:body].empty?
|
1610
1572
|
part.merge(type: :mrhs, body: [part])
|
1611
1573
|
else
|
1612
|
-
mrhs.merge!(
|
1613
|
-
body: mrhs[:body] << part,
|
1614
|
-
end: part[:end],
|
1615
|
-
char_end: part[:char_end]
|
1616
|
-
)
|
1574
|
+
mrhs.merge!(body: mrhs[:body] << part, el: part[:el], ec: part[:ec])
|
1617
1575
|
end
|
1618
1576
|
end
|
1619
1577
|
|
@@ -1627,10 +1585,10 @@ class Prettier::Parser < Ripper
|
|
1627
1585
|
{
|
1628
1586
|
type: :mrhs_add_star,
|
1629
1587
|
body: [mrhs, part],
|
1630
|
-
|
1631
|
-
|
1632
|
-
|
1633
|
-
|
1588
|
+
sl: beging[:sl],
|
1589
|
+
sc: beging[:sc],
|
1590
|
+
el: ending[:el],
|
1591
|
+
ec: ending[:ec]
|
1634
1592
|
}
|
1635
1593
|
end
|
1636
1594
|
|
@@ -1653,8 +1611,8 @@ class Prettier::Parser < Ripper
|
|
1653
1611
|
find_scanner_event(:@kw, 'next').merge!(
|
1654
1612
|
type: :next,
|
1655
1613
|
body: [args_add_block],
|
1656
|
-
|
1657
|
-
|
1614
|
+
el: args_add_block[:el],
|
1615
|
+
ec: args_add_block[:ec]
|
1658
1616
|
)
|
1659
1617
|
end
|
1660
1618
|
|
@@ -1666,8 +1624,8 @@ class Prettier::Parser < Ripper
|
|
1666
1624
|
left.merge(
|
1667
1625
|
type: :opassign,
|
1668
1626
|
body: [left, oper, right],
|
1669
|
-
|
1670
|
-
|
1627
|
+
el: right[:el],
|
1628
|
+
ec: right[:ec]
|
1671
1629
|
)
|
1672
1630
|
end
|
1673
1631
|
|
@@ -1681,13 +1639,13 @@ class Prettier::Parser < Ripper
|
|
1681
1639
|
location =
|
1682
1640
|
if flattened.any?
|
1683
1641
|
{
|
1684
|
-
|
1685
|
-
|
1686
|
-
|
1687
|
-
|
1642
|
+
sl: flattened[0][:sl],
|
1643
|
+
sc: flattened[0][:sc],
|
1644
|
+
el: flattened[-1][:el],
|
1645
|
+
ec: flattened[-1][:ec]
|
1688
1646
|
}
|
1689
1647
|
else
|
1690
|
-
{
|
1648
|
+
{ sl: lineno, sc: char_pos, el: lineno, ec: char_pos }
|
1691
1649
|
end
|
1692
1650
|
|
1693
1651
|
location.merge!(type: :params, body: types)
|
@@ -1697,13 +1655,18 @@ class Prettier::Parser < Ripper
|
|
1697
1655
|
# anywhere in a Ruby program. It accepts as arguments the contents, which
|
1698
1656
|
# can be either params or statements.
|
1699
1657
|
def on_paren(contents)
|
1658
|
+
beging = find_scanner_event(:@lparen)
|
1700
1659
|
ending = find_scanner_event(:@rparen)
|
1701
1660
|
|
1702
|
-
|
1661
|
+
if contents && contents[:type] == :params
|
1662
|
+
contents.merge!(sc: beging[:ec], ec: ending[:sc])
|
1663
|
+
end
|
1664
|
+
|
1665
|
+
beging.merge!(
|
1703
1666
|
type: :paren,
|
1704
1667
|
body: [contents],
|
1705
|
-
|
1706
|
-
|
1668
|
+
el: ending[:el],
|
1669
|
+
ec: ending[:ec]
|
1707
1670
|
)
|
1708
1671
|
end
|
1709
1672
|
|
@@ -1712,12 +1675,7 @@ class Prettier::Parser < Ripper
|
|
1712
1675
|
# source string. We'll also attach on the __END__ content if there was
|
1713
1676
|
# some found at the end of the source string.
|
1714
1677
|
def on_program(stmts)
|
1715
|
-
range = {
|
1716
|
-
start: 1,
|
1717
|
-
end: lines.length,
|
1718
|
-
char_start: 0,
|
1719
|
-
char_end: source.length
|
1720
|
-
}
|
1678
|
+
range = { sl: 1, el: lines.length, sc: 0, ec: source.length }
|
1721
1679
|
|
1722
1680
|
stmts[:body] << @__end__ if @__end__
|
1723
1681
|
stmts.bind(0, source.length)
|
@@ -1739,8 +1697,8 @@ class Prettier::Parser < Ripper
|
|
1739
1697
|
def on_qsymbols_add(qsymbols, tstring_content)
|
1740
1698
|
qsymbols.merge!(
|
1741
1699
|
body: qsymbols[:body] << tstring_content,
|
1742
|
-
|
1743
|
-
|
1700
|
+
el: tstring_content[:el],
|
1701
|
+
ec: tstring_content[:ec]
|
1744
1702
|
)
|
1745
1703
|
end
|
1746
1704
|
|
@@ -1758,8 +1716,8 @@ class Prettier::Parser < Ripper
|
|
1758
1716
|
def on_qwords_add(qwords, tstring_content)
|
1759
1717
|
qwords.merge!(
|
1760
1718
|
body: qwords[:body] << tstring_content,
|
1761
|
-
|
1762
|
-
|
1719
|
+
el: tstring_content[:el],
|
1720
|
+
ec: tstring_content[:ec]
|
1763
1721
|
)
|
1764
1722
|
end
|
1765
1723
|
|
@@ -1784,8 +1742,8 @@ class Prettier::Parser < Ripper
|
|
1784
1742
|
def on_regexp_add(regexp, piece)
|
1785
1743
|
regexp.merge!(
|
1786
1744
|
body: regexp[:body] << piece,
|
1787
|
-
|
1788
|
-
|
1745
|
+
el: regexp[:el],
|
1746
|
+
ec: regexp[:ec]
|
1789
1747
|
)
|
1790
1748
|
end
|
1791
1749
|
|
@@ -1797,8 +1755,8 @@ class Prettier::Parser < Ripper
|
|
1797
1755
|
regexp.merge!(
|
1798
1756
|
type: :regexp_literal,
|
1799
1757
|
ending: ending[:body],
|
1800
|
-
|
1801
|
-
|
1758
|
+
el: ending[:el],
|
1759
|
+
ec: ending[:ec]
|
1802
1760
|
)
|
1803
1761
|
end
|
1804
1762
|
|
@@ -1807,17 +1765,17 @@ class Prettier::Parser < Ripper
|
|
1807
1765
|
# determine its ending. Therefore it relies on its parent bodystmt node to
|
1808
1766
|
# report its ending to it.
|
1809
1767
|
class Rescue < SimpleDelegator
|
1810
|
-
def bind_end(
|
1811
|
-
merge!(
|
1768
|
+
def bind_end(ec)
|
1769
|
+
merge!(ec: ec)
|
1812
1770
|
|
1813
1771
|
stmts = self[:body][2]
|
1814
1772
|
consequent = self[:body][3]
|
1815
1773
|
|
1816
1774
|
if consequent
|
1817
|
-
consequent.bind_end(
|
1818
|
-
stmts.bind_end(consequent[:
|
1775
|
+
consequent.bind_end(ec)
|
1776
|
+
stmts.bind_end(consequent[:sc])
|
1819
1777
|
else
|
1820
|
-
stmts.bind_end(
|
1778
|
+
stmts.bind_end(ec)
|
1821
1779
|
end
|
1822
1780
|
end
|
1823
1781
|
end
|
@@ -1830,14 +1788,14 @@ class Prettier::Parser < Ripper
|
|
1830
1788
|
last_exception = exceptions.is_a?(Array) ? exceptions[-1] : exceptions
|
1831
1789
|
last_node = variable || last_exception || beging
|
1832
1790
|
|
1833
|
-
stmts.bind(find_next_statement_start(last_node[:
|
1791
|
+
stmts.bind(find_next_statement_start(last_node[:ec]), char_pos)
|
1834
1792
|
|
1835
1793
|
Rescue.new(
|
1836
1794
|
beging.merge!(
|
1837
1795
|
type: :rescue,
|
1838
1796
|
body: [exceptions, variable, stmts, consequent],
|
1839
|
-
|
1840
|
-
|
1797
|
+
el: lineno,
|
1798
|
+
ec: char_pos
|
1841
1799
|
)
|
1842
1800
|
)
|
1843
1801
|
end
|
@@ -1851,10 +1809,10 @@ class Prettier::Parser < Ripper
|
|
1851
1809
|
{
|
1852
1810
|
type: :rescue_mod,
|
1853
1811
|
body: [statement, rescued],
|
1854
|
-
|
1855
|
-
|
1856
|
-
|
1857
|
-
|
1812
|
+
sl: statement[:sl],
|
1813
|
+
sc: statement[:sc],
|
1814
|
+
el: rescued[:el],
|
1815
|
+
ec: rescued[:ec]
|
1858
1816
|
}
|
1859
1817
|
end
|
1860
1818
|
|
@@ -1869,8 +1827,8 @@ class Prettier::Parser < Ripper
|
|
1869
1827
|
oper.merge!(
|
1870
1828
|
type: :rest_param,
|
1871
1829
|
body: [ident],
|
1872
|
-
|
1873
|
-
|
1830
|
+
el: ident[:el],
|
1831
|
+
ec: ident[:ec]
|
1874
1832
|
)
|
1875
1833
|
end
|
1876
1834
|
|
@@ -1887,8 +1845,8 @@ class Prettier::Parser < Ripper
|
|
1887
1845
|
find_scanner_event(:@kw, 'return').merge!(
|
1888
1846
|
type: :return,
|
1889
1847
|
body: [args_add_block],
|
1890
|
-
|
1891
|
-
|
1848
|
+
el: args_add_block[:el],
|
1849
|
+
ec: args_add_block[:ec]
|
1892
1850
|
)
|
1893
1851
|
end
|
1894
1852
|
|
@@ -1914,18 +1872,15 @@ class Prettier::Parser < Ripper
|
|
1914
1872
|
beging = find_scanner_event(:@kw, 'class')
|
1915
1873
|
ending = find_scanner_event(:@kw, 'end')
|
1916
1874
|
|
1917
|
-
bodystmt.bind(
|
1918
|
-
find_next_statement_start(target[:char_end]),
|
1919
|
-
ending[:char_start]
|
1920
|
-
)
|
1875
|
+
bodystmt.bind(find_next_statement_start(target[:ec]), ending[:sc])
|
1921
1876
|
|
1922
1877
|
{
|
1923
1878
|
type: :sclass,
|
1924
1879
|
body: [target, bodystmt],
|
1925
|
-
|
1926
|
-
|
1927
|
-
|
1928
|
-
|
1880
|
+
sl: beging[:sl],
|
1881
|
+
sc: beging[:sc],
|
1882
|
+
el: ending[:el],
|
1883
|
+
ec: ending[:ec]
|
1929
1884
|
}
|
1930
1885
|
end
|
1931
1886
|
|
@@ -1937,23 +1892,23 @@ class Prettier::Parser < Ripper
|
|
1937
1892
|
# propagate that onto void_stmt nodes inside the stmts in order to make sure
|
1938
1893
|
# all comments get printed appropriately.
|
1939
1894
|
class Stmts < SimpleDelegator
|
1940
|
-
def bind(
|
1941
|
-
merge!(
|
1895
|
+
def bind(sc, ec)
|
1896
|
+
merge!(sc: sc, ec: ec)
|
1942
1897
|
|
1943
1898
|
if self[:body][0][:type] == :void_stmt
|
1944
|
-
self[:body][0].merge!(
|
1899
|
+
self[:body][0].merge!(sc: sc, ec: sc)
|
1945
1900
|
end
|
1946
1901
|
end
|
1947
1902
|
|
1948
|
-
def bind_end(
|
1949
|
-
merge!(
|
1903
|
+
def bind_end(ec)
|
1904
|
+
merge!(ec: ec)
|
1950
1905
|
end
|
1951
1906
|
|
1952
1907
|
def <<(statement)
|
1953
1908
|
if self[:body].any?
|
1954
|
-
merge!(statement.slice(:
|
1909
|
+
merge!(statement.slice(:el, :ec))
|
1955
1910
|
else
|
1956
|
-
merge!(statement.slice(:
|
1911
|
+
merge!(statement.slice(:sl, :el, :sc, :ec))
|
1957
1912
|
end
|
1958
1913
|
|
1959
1914
|
self[:body] << statement
|
@@ -1968,10 +1923,10 @@ class Prettier::Parser < Ripper
|
|
1968
1923
|
Stmts.new(
|
1969
1924
|
type: :stmts,
|
1970
1925
|
body: [],
|
1971
|
-
|
1972
|
-
|
1973
|
-
|
1974
|
-
|
1926
|
+
sl: lineno,
|
1927
|
+
el: lineno,
|
1928
|
+
sc: char_pos,
|
1929
|
+
ec: char_pos
|
1975
1930
|
)
|
1976
1931
|
end
|
1977
1932
|
|
@@ -1993,10 +1948,10 @@ class Prettier::Parser < Ripper
|
|
1993
1948
|
{
|
1994
1949
|
type: :string_concat,
|
1995
1950
|
body: [left, right],
|
1996
|
-
|
1997
|
-
|
1998
|
-
|
1999
|
-
|
1951
|
+
sl: left[:sl],
|
1952
|
+
sc: left[:sc],
|
1953
|
+
el: right[:el],
|
1954
|
+
ec: right[:ec]
|
2000
1955
|
}
|
2001
1956
|
end
|
2002
1957
|
|
@@ -2009,10 +1964,10 @@ class Prettier::Parser < Ripper
|
|
2009
1964
|
{
|
2010
1965
|
type: :string,
|
2011
1966
|
body: [],
|
2012
|
-
|
2013
|
-
|
2014
|
-
|
2015
|
-
|
1967
|
+
sl: lineno,
|
1968
|
+
el: lineno,
|
1969
|
+
sc: char_pos,
|
1970
|
+
ec: char_pos
|
2016
1971
|
}
|
2017
1972
|
end
|
2018
1973
|
|
@@ -2021,11 +1976,7 @@ class Prettier::Parser < Ripper
|
|
2021
1976
|
# It accepts as arguments the parent string node as well as the additional
|
2022
1977
|
# piece of the string.
|
2023
1978
|
def on_string_add(string, piece)
|
2024
|
-
string.merge!(
|
2025
|
-
body: string[:body] << piece,
|
2026
|
-
end: piece[:end],
|
2027
|
-
char_end: piece[:char_end]
|
2028
|
-
)
|
1979
|
+
string.merge!(body: string[:body] << piece, el: piece[:el], ec: piece[:ec])
|
2029
1980
|
end
|
2030
1981
|
|
2031
1982
|
# string_dvar is a parser event that represents a very special kind of
|
@@ -2037,8 +1988,8 @@ class Prettier::Parser < Ripper
|
|
2037
1988
|
find_scanner_event(:@embvar).merge!(
|
2038
1989
|
type: :string_dvar,
|
2039
1990
|
body: [var_ref],
|
2040
|
-
|
2041
|
-
|
1991
|
+
el: var_ref[:el],
|
1992
|
+
ec: var_ref[:ec]
|
2042
1993
|
)
|
2043
1994
|
end
|
2044
1995
|
|
@@ -2050,15 +2001,15 @@ class Prettier::Parser < Ripper
|
|
2050
2001
|
beging = find_scanner_event(:@embexpr_beg)
|
2051
2002
|
ending = find_scanner_event(:@embexpr_end)
|
2052
2003
|
|
2053
|
-
stmts.bind(beging[:
|
2004
|
+
stmts.bind(beging[:ec], ending[:sc])
|
2054
2005
|
|
2055
2006
|
{
|
2056
2007
|
type: :string_embexpr,
|
2057
2008
|
body: [stmts],
|
2058
|
-
|
2059
|
-
|
2060
|
-
|
2061
|
-
|
2009
|
+
sl: beging[:sl],
|
2010
|
+
sc: beging[:sc],
|
2011
|
+
el: ending[:el],
|
2012
|
+
ec: ending[:ec]
|
2062
2013
|
}
|
2063
2014
|
end
|
2064
2015
|
|
@@ -2077,10 +2028,10 @@ class Prettier::Parser < Ripper
|
|
2077
2028
|
type: :string_literal,
|
2078
2029
|
body: string[:body],
|
2079
2030
|
quote: beging[:body],
|
2080
|
-
|
2081
|
-
|
2082
|
-
|
2083
|
-
|
2031
|
+
sl: beging[:sl],
|
2032
|
+
sc: beging[:sc],
|
2033
|
+
el: ending[:el],
|
2034
|
+
ec: ending[:ec]
|
2084
2035
|
}
|
2085
2036
|
end
|
2086
2037
|
end
|
@@ -2093,8 +2044,8 @@ class Prettier::Parser < Ripper
|
|
2093
2044
|
find_scanner_event(:@kw, 'super').merge!(
|
2094
2045
|
type: :super,
|
2095
2046
|
body: [contents],
|
2096
|
-
|
2097
|
-
|
2047
|
+
el: contents[:el],
|
2048
|
+
ec: contents[:ec]
|
2098
2049
|
)
|
2099
2050
|
end
|
2100
2051
|
|
@@ -2125,7 +2076,7 @@ class Prettier::Parser < Ripper
|
|
2125
2076
|
contents.merge(type: :symbol_literal, body: [contents])
|
2126
2077
|
else
|
2127
2078
|
beging = find_scanner_event(:@symbeg)
|
2128
|
-
contents.merge!(type: :symbol_literal,
|
2079
|
+
contents.merge!(type: :symbol_literal, sc: beging[:sc])
|
2129
2080
|
end
|
2130
2081
|
end
|
2131
2082
|
|
@@ -2144,8 +2095,8 @@ class Prettier::Parser < Ripper
|
|
2144
2095
|
def on_symbols_add(symbols, word_add)
|
2145
2096
|
symbols.merge!(
|
2146
2097
|
body: symbols[:body] << word_add,
|
2147
|
-
|
2148
|
-
|
2098
|
+
el: word_add[:el],
|
2099
|
+
ec: word_add[:ec]
|
2149
2100
|
)
|
2150
2101
|
end
|
2151
2102
|
|
@@ -2156,8 +2107,7 @@ class Prettier::Parser < Ripper
|
|
2156
2107
|
def find_colon2_before(const)
|
2157
2108
|
index =
|
2158
2109
|
scanner_events.rindex do |event|
|
2159
|
-
event[:type] == :@op && event[:body] == '::' &&
|
2160
|
-
event[:char_start] < const[:char_start]
|
2110
|
+
event[:type] == :@op && event[:body] == '::' && event[:sc] < const[:sc]
|
2161
2111
|
end
|
2162
2112
|
|
2163
2113
|
scanner_events[index]
|
@@ -2174,8 +2124,8 @@ class Prettier::Parser < Ripper
|
|
2174
2124
|
const.merge(
|
2175
2125
|
type: :top_const_field,
|
2176
2126
|
body: [const],
|
2177
|
-
|
2178
|
-
|
2127
|
+
sl: beging[:sl],
|
2128
|
+
sc: beging[:sc]
|
2179
2129
|
)
|
2180
2130
|
end
|
2181
2131
|
|
@@ -2190,8 +2140,8 @@ class Prettier::Parser < Ripper
|
|
2190
2140
|
const.merge(
|
2191
2141
|
type: :top_const_ref,
|
2192
2142
|
body: [const],
|
2193
|
-
|
2194
|
-
|
2143
|
+
sl: beging[:sl],
|
2144
|
+
sc: beging[:sc]
|
2195
2145
|
)
|
2196
2146
|
end
|
2197
2147
|
|
@@ -2203,15 +2153,15 @@ class Prettier::Parser < Ripper
|
|
2203
2153
|
if oper == :not
|
2204
2154
|
node = find_scanner_event(:@kw, 'not')
|
2205
2155
|
|
2206
|
-
paren = source[node[:
|
2156
|
+
paren = source[node[:ec]...value[:sc]].include?('(')
|
2207
2157
|
ending = paren ? find_scanner_event(:@rparen) : value
|
2208
2158
|
|
2209
2159
|
node.merge!(
|
2210
2160
|
type: :unary,
|
2211
2161
|
oper: oper,
|
2212
2162
|
body: [value],
|
2213
|
-
|
2214
|
-
|
2163
|
+
el: ending[:el],
|
2164
|
+
ec: ending[:ec],
|
2215
2165
|
paren: paren
|
2216
2166
|
)
|
2217
2167
|
else
|
@@ -2221,7 +2171,7 @@ class Prettier::Parser < Ripper
|
|
2221
2171
|
# stack. So we need to explicitly disallow those operators.
|
2222
2172
|
index =
|
2223
2173
|
scanner_events.rindex do |scanner_event|
|
2224
|
-
scanner_event[:type] == :@op &&
|
2174
|
+
scanner_event[:type] == :@op && scanner_event[:sc] < value[:sc] &&
|
2225
2175
|
!%w[.. ...].include?(scanner_event[:body])
|
2226
2176
|
end
|
2227
2177
|
|
@@ -2230,8 +2180,8 @@ class Prettier::Parser < Ripper
|
|
2230
2180
|
type: :unary,
|
2231
2181
|
oper: oper[0],
|
2232
2182
|
body: [value],
|
2233
|
-
|
2234
|
-
|
2183
|
+
el: value[:el],
|
2184
|
+
ec: value[:ec]
|
2235
2185
|
)
|
2236
2186
|
end
|
2237
2187
|
end
|
@@ -2246,8 +2196,8 @@ class Prettier::Parser < Ripper
|
|
2246
2196
|
find_scanner_event(:@kw, 'undef').merge!(
|
2247
2197
|
type: :undef,
|
2248
2198
|
body: symbol_literals,
|
2249
|
-
|
2250
|
-
|
2199
|
+
el: last[:el],
|
2200
|
+
ec: last[:ec]
|
2251
2201
|
)
|
2252
2202
|
end
|
2253
2203
|
|
@@ -2259,15 +2209,15 @@ class Prettier::Parser < Ripper
|
|
2259
2209
|
beging = find_scanner_event(:@kw, 'unless')
|
2260
2210
|
ending = consequent || find_scanner_event(:@kw, 'end')
|
2261
2211
|
|
2262
|
-
stmts.bind(predicate[:
|
2212
|
+
stmts.bind(predicate[:ec], ending[:sc])
|
2263
2213
|
|
2264
2214
|
{
|
2265
2215
|
type: :unless,
|
2266
2216
|
body: [predicate, stmts, consequent],
|
2267
|
-
|
2268
|
-
|
2269
|
-
|
2270
|
-
|
2217
|
+
sl: beging[:sl],
|
2218
|
+
sc: beging[:sc],
|
2219
|
+
el: ending[:el],
|
2220
|
+
ec: ending[:ec]
|
2271
2221
|
}
|
2272
2222
|
end
|
2273
2223
|
|
@@ -2280,10 +2230,10 @@ class Prettier::Parser < Ripper
|
|
2280
2230
|
{
|
2281
2231
|
type: :unless_mod,
|
2282
2232
|
body: [predicate, statement],
|
2283
|
-
|
2284
|
-
|
2285
|
-
|
2286
|
-
|
2233
|
+
sl: statement[:sl],
|
2234
|
+
sc: statement[:sc],
|
2235
|
+
el: predicate[:el],
|
2236
|
+
ec: predicate[:ec]
|
2287
2237
|
}
|
2288
2238
|
end
|
2289
2239
|
|
@@ -2294,15 +2244,22 @@ class Prettier::Parser < Ripper
|
|
2294
2244
|
beging = find_scanner_event(:@kw, 'until')
|
2295
2245
|
ending = find_scanner_event(:@kw, 'end')
|
2296
2246
|
|
2297
|
-
|
2247
|
+
# Consume the do keyword if it exists so that it doesn't get confused for
|
2248
|
+
# some other block
|
2249
|
+
do_event = find_scanner_event(:@kw, 'do', consume: false)
|
2250
|
+
if do_event && do_event[:sc] > predicate[:ec] && do_event[:ec] < ending[:sc]
|
2251
|
+
scanner_events.delete(do_event)
|
2252
|
+
end
|
2253
|
+
|
2254
|
+
stmts.bind(predicate[:ec], ending[:sc])
|
2298
2255
|
|
2299
2256
|
{
|
2300
2257
|
type: :until,
|
2301
2258
|
body: [predicate, stmts],
|
2302
|
-
|
2303
|
-
|
2304
|
-
|
2305
|
-
|
2259
|
+
sl: beging[:sl],
|
2260
|
+
sc: beging[:sc],
|
2261
|
+
el: ending[:el],
|
2262
|
+
ec: ending[:ec]
|
2306
2263
|
}
|
2307
2264
|
end
|
2308
2265
|
|
@@ -2315,10 +2272,10 @@ class Prettier::Parser < Ripper
|
|
2315
2272
|
{
|
2316
2273
|
type: :until_mod,
|
2317
2274
|
body: [predicate, statement],
|
2318
|
-
|
2319
|
-
|
2320
|
-
|
2321
|
-
|
2275
|
+
sl: statement[:sl],
|
2276
|
+
sc: statement[:sc],
|
2277
|
+
el: predicate[:el],
|
2278
|
+
ec: predicate[:ec]
|
2322
2279
|
}
|
2323
2280
|
end
|
2324
2281
|
|
@@ -2329,16 +2286,16 @@ class Prettier::Parser < Ripper
|
|
2329
2286
|
def on_var_alias(left, right)
|
2330
2287
|
beging = find_scanner_event(:@kw, 'alias')
|
2331
2288
|
|
2332
|
-
paren = source[beging[:
|
2289
|
+
paren = source[beging[:ec]...left[:sc]].include?('(')
|
2333
2290
|
ending = paren ? find_scanner_event(:@rparen) : right
|
2334
2291
|
|
2335
2292
|
{
|
2336
2293
|
type: :var_alias,
|
2337
2294
|
body: [left, right],
|
2338
|
-
|
2339
|
-
|
2340
|
-
|
2341
|
-
|
2295
|
+
sl: beging[:sl],
|
2296
|
+
sc: beging[:sc],
|
2297
|
+
el: ending[:el],
|
2298
|
+
ec: ending[:ec]
|
2342
2299
|
}
|
2343
2300
|
end
|
2344
2301
|
|
@@ -2390,13 +2347,7 @@ class Prettier::Parser < Ripper
|
|
2390
2347
|
# block of code. It often will have comments attached to it, so it requires
|
2391
2348
|
# some special handling.
|
2392
2349
|
def on_void_stmt
|
2393
|
-
{
|
2394
|
-
type: :void_stmt,
|
2395
|
-
start: lineno,
|
2396
|
-
end: lineno,
|
2397
|
-
char_start: char_pos,
|
2398
|
-
char_end: char_pos
|
2399
|
-
}
|
2350
|
+
{ type: :void_stmt, sl: lineno, el: lineno, sc: char_pos, ec: char_pos }
|
2400
2351
|
end
|
2401
2352
|
|
2402
2353
|
# when is a parser event that represents another clause in a case chain.
|
@@ -2407,15 +2358,15 @@ class Prettier::Parser < Ripper
|
|
2407
2358
|
beging = find_scanner_event(:@kw, 'when')
|
2408
2359
|
ending = consequent || find_scanner_event(:@kw, 'end')
|
2409
2360
|
|
2410
|
-
stmts.bind(predicate[:
|
2361
|
+
stmts.bind(predicate[:ec], ending[:sc])
|
2411
2362
|
|
2412
2363
|
{
|
2413
2364
|
type: :when,
|
2414
2365
|
body: [predicate, stmts, consequent],
|
2415
|
-
|
2416
|
-
|
2417
|
-
|
2418
|
-
|
2366
|
+
sl: beging[:sl],
|
2367
|
+
sc: beging[:sc],
|
2368
|
+
el: ending[:el],
|
2369
|
+
ec: ending[:ec]
|
2419
2370
|
}
|
2420
2371
|
end
|
2421
2372
|
|
@@ -2426,15 +2377,22 @@ class Prettier::Parser < Ripper
|
|
2426
2377
|
beging = find_scanner_event(:@kw, 'while')
|
2427
2378
|
ending = find_scanner_event(:@kw, 'end')
|
2428
2379
|
|
2429
|
-
|
2380
|
+
# Consume the do keyword if it exists so that it doesn't get confused for
|
2381
|
+
# some other block
|
2382
|
+
do_event = find_scanner_event(:@kw, 'do', consume: false)
|
2383
|
+
if do_event && do_event[:sc] > predicate[:ec] && do_event[:ec] < ending[:sc]
|
2384
|
+
scanner_events.delete(do_event)
|
2385
|
+
end
|
2386
|
+
|
2387
|
+
stmts.bind(predicate[:ec], ending[:sc])
|
2430
2388
|
|
2431
2389
|
{
|
2432
2390
|
type: :while,
|
2433
2391
|
body: [predicate, stmts],
|
2434
|
-
|
2435
|
-
|
2436
|
-
|
2437
|
-
|
2392
|
+
sl: beging[:sl],
|
2393
|
+
sc: beging[:sc],
|
2394
|
+
el: ending[:el],
|
2395
|
+
ec: ending[:ec]
|
2438
2396
|
}
|
2439
2397
|
end
|
2440
2398
|
|
@@ -2447,10 +2405,10 @@ class Prettier::Parser < Ripper
|
|
2447
2405
|
{
|
2448
2406
|
type: :while_mod,
|
2449
2407
|
body: [predicate, statement],
|
2450
|
-
|
2451
|
-
|
2452
|
-
|
2453
|
-
|
2408
|
+
sl: statement[:sl],
|
2409
|
+
sc: statement[:sc],
|
2410
|
+
el: predicate[:el],
|
2411
|
+
ec: predicate[:ec]
|
2454
2412
|
}
|
2455
2413
|
end
|
2456
2414
|
|
@@ -2480,11 +2438,7 @@ class Prettier::Parser < Ripper
|
|
2480
2438
|
# location information from the first piece.
|
2481
2439
|
piece.merge(type: :word, body: [piece])
|
2482
2440
|
else
|
2483
|
-
word.merge!(
|
2484
|
-
body: word[:body] << piece,
|
2485
|
-
end: piece[:end],
|
2486
|
-
char_end: piece[:char_end]
|
2487
|
-
)
|
2441
|
+
word.merge!(body: word[:body] << piece, el: piece[:el], ec: piece[:ec])
|
2488
2442
|
end
|
2489
2443
|
end
|
2490
2444
|
|
@@ -2503,8 +2457,8 @@ class Prettier::Parser < Ripper
|
|
2503
2457
|
def on_words_add(words, word_add)
|
2504
2458
|
words.merge!(
|
2505
2459
|
body: words[:body] << word_add,
|
2506
|
-
|
2507
|
-
|
2460
|
+
el: word_add[:el],
|
2461
|
+
ec: word_add[:ec]
|
2508
2462
|
)
|
2509
2463
|
end
|
2510
2464
|
|
@@ -2537,8 +2491,8 @@ class Prettier::Parser < Ripper
|
|
2537
2491
|
def on_xstring_add(xstring, piece)
|
2538
2492
|
xstring.merge!(
|
2539
2493
|
body: xstring[:body] << piece,
|
2540
|
-
|
2541
|
-
|
2494
|
+
el: piece[:el],
|
2495
|
+
ec: piece[:ec]
|
2542
2496
|
)
|
2543
2497
|
end
|
2544
2498
|
|
@@ -2563,11 +2517,7 @@ class Prettier::Parser < Ripper
|
|
2563
2517
|
heredoc.merge!(body: xstring[:body])
|
2564
2518
|
else
|
2565
2519
|
ending = find_scanner_event(:@tstring_end)
|
2566
|
-
xstring.merge!(
|
2567
|
-
type: :xstring_literal,
|
2568
|
-
end: ending[:end],
|
2569
|
-
char_end: ending[:char_end]
|
2570
|
-
)
|
2520
|
+
xstring.merge!(type: :xstring_literal, el: ending[:el], ec: ending[:ec])
|
2571
2521
|
end
|
2572
2522
|
end
|
2573
2523
|
|
@@ -2578,8 +2528,8 @@ class Prettier::Parser < Ripper
|
|
2578
2528
|
find_scanner_event(:@kw, 'yield').merge!(
|
2579
2529
|
type: :yield,
|
2580
2530
|
body: [args_add_block],
|
2581
|
-
|
2582
|
-
|
2531
|
+
el: args_add_block[:el],
|
2532
|
+
ec: args_add_block[:ec]
|
2583
2533
|
)
|
2584
2534
|
end
|
2585
2535
|
|
@@ -2605,10 +2555,9 @@ end
|
|
2605
2555
|
# stdin and report back the AST over stdout.
|
2606
2556
|
|
2607
2557
|
if $0 == __FILE__
|
2608
|
-
|
2609
|
-
response = builder.parse
|
2558
|
+
response = Prettier::Parser.parse($stdin.read)
|
2610
2559
|
|
2611
|
-
if !response
|
2560
|
+
if !response
|
2612
2561
|
warn(
|
2613
2562
|
'@prettier/plugin-ruby encountered an error when attempting to parse ' \
|
2614
2563
|
'the ruby source. This usually means there was a syntax error in the ' \
|