prettier 0.12.2
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 +7 -0
- data/CHANGELOG.md +368 -0
- data/CODE_OF_CONDUCT.md +76 -0
- data/CONTRIBUTING.md +143 -0
- data/LICENSE +21 -0
- data/README.md +151 -0
- data/bin/console +7 -0
- data/exe/rbprettier +8 -0
- data/lib/prettier.rb +18 -0
- data/lib/prettier/rake/task.rb +57 -0
- data/node_modules/prettier/bin-prettier.js +44620 -0
- data/node_modules/prettier/index.js +42565 -0
- data/node_modules/prettier/third-party.js +5326 -0
- data/package.json +39 -0
- data/src/builders.js +9 -0
- data/src/escapePattern.js +45 -0
- data/src/nodes.js +594 -0
- data/src/nodes/alias.js +32 -0
- data/src/nodes/arrays.js +162 -0
- data/src/nodes/blocks.js +66 -0
- data/src/nodes/calls.js +28 -0
- data/src/nodes/case.js +57 -0
- data/src/nodes/commands.js +70 -0
- data/src/nodes/conditionals.js +154 -0
- data/src/nodes/hashes.js +134 -0
- data/src/nodes/hooks.js +15 -0
- data/src/nodes/lambdas.js +59 -0
- data/src/nodes/loops.js +46 -0
- data/src/nodes/methods.js +42 -0
- data/src/nodes/params.js +75 -0
- data/src/nodes/regexp.js +18 -0
- data/src/nodes/rescue.js +77 -0
- data/src/nodes/strings.js +143 -0
- data/src/parse.js +16 -0
- data/src/print.js +23 -0
- data/src/ripper.rb +542 -0
- data/src/ruby.js +127 -0
- data/src/toProc.js +82 -0
- data/src/utils.js +150 -0
- metadata +109 -0
data/package.json
ADDED
@@ -0,0 +1,39 @@
|
|
1
|
+
{
|
2
|
+
"name": "@prettier/plugin-ruby",
|
3
|
+
"version": "0.12.2",
|
4
|
+
"description": "prettier plugin for the Ruby programming language",
|
5
|
+
"main": "src/ruby.js",
|
6
|
+
"scripts": {
|
7
|
+
"lint": "eslint --cache .",
|
8
|
+
"print": "prettier --plugin=.",
|
9
|
+
"rubocop": "run() { prettier --plugin=. $@ | bundle exec rubocop --stdin $1; }; run",
|
10
|
+
"test": "jest"
|
11
|
+
},
|
12
|
+
"repository": {
|
13
|
+
"type": "git",
|
14
|
+
"url": "git+https://github.com/prettier/plugin-ruby.git"
|
15
|
+
},
|
16
|
+
"author": "Kevin Deisz",
|
17
|
+
"license": "MIT",
|
18
|
+
"bugs": {
|
19
|
+
"url": "https://github.com/prettier/plugin-ruby/issues"
|
20
|
+
},
|
21
|
+
"homepage": "https://github.com/prettier/plugin-ruby#readme",
|
22
|
+
"dependencies": {
|
23
|
+
"prettier": "^1.16.4"
|
24
|
+
},
|
25
|
+
"devDependencies": {
|
26
|
+
"all-contributors-cli": "^6.1.2",
|
27
|
+
"eslint": "^5.15.0",
|
28
|
+
"eslint-config-airbnb-base": "^13.1.0",
|
29
|
+
"eslint-config-prettier": "^4.1.0",
|
30
|
+
"eslint-plugin-import": "^2.16.0",
|
31
|
+
"jest": "^24.0.0"
|
32
|
+
},
|
33
|
+
"jest": {
|
34
|
+
"setupFilesAfterEnv": [
|
35
|
+
"./test/js/setupTests.js"
|
36
|
+
],
|
37
|
+
"testRegex": ".test.js$"
|
38
|
+
}
|
39
|
+
}
|
data/src/builders.js
ADDED
@@ -0,0 +1,9 @@
|
|
1
|
+
// If `RBPRETTIER` is set, then this is being run from the `Prettier::run` ruby
|
2
|
+
// method. In that case, we need to pull `prettier` from the node_modules
|
3
|
+
// directly, as it's been shipped with the gem.
|
4
|
+
const source = process.env.RBPRETTIER ? "../node_modules/prettier" : "prettier";
|
5
|
+
|
6
|
+
// eslint-disable-next-line import/no-dynamic-require
|
7
|
+
const { builders, utils } = require(source).doc;
|
8
|
+
|
9
|
+
module.exports = Object.assign({}, builders, utils);
|
@@ -0,0 +1,45 @@
|
|
1
|
+
/**
|
2
|
+
* String literal syntax documentation from Ruby source (2.7.0-dev)
|
3
|
+
*
|
4
|
+
* Double-quote strings allow escaped characters such as <tt>\n</tt> for
|
5
|
+
* newline, <tt>\t</tt> for tab, etc. The full list of supported escape
|
6
|
+
* sequences are as follows:
|
7
|
+
*
|
8
|
+
* \a bell, ASCII 07h (BEL)
|
9
|
+
* \b backspace, ASCII 08h (BS)
|
10
|
+
* \t horizontal tab, ASCII 09h (TAB)
|
11
|
+
* \n newline (line feed), ASCII 0Ah (LF)
|
12
|
+
* \v vertical tab, ASCII 0Bh (VT)
|
13
|
+
* \f form feed, ASCII 0Ch (FF)
|
14
|
+
* \r carriage return, ASCII 0Dh (CR)
|
15
|
+
* \e escape, ASCII 1Bh (ESC)
|
16
|
+
* \s space, ASCII 20h (SPC)
|
17
|
+
* \\ backslash, \
|
18
|
+
* \nnn octal bit pattern, where nnn is 1-3 octal digits ([0-7])
|
19
|
+
* \xnn hexadecimal bit pattern, where nn is 1-2 hexadecimal digits ([0-9a-fA-F])
|
20
|
+
* \unnnn Unicode character, where nnnn is exactly 4 hexadecimal digits ([0-9a-fA-F])
|
21
|
+
* \u{nnnn ...} Unicode character(s), where each nnnn is 1-6 hexadecimal digits ([0-9a-fA-F])
|
22
|
+
* \cx or \C-x control character, where x is an ASCII printable character
|
23
|
+
* \M-x meta character, where x is an ASCII printable character
|
24
|
+
* \M-\C-x meta control character, where x is an ASCII printable character
|
25
|
+
* \M-\cx same as above
|
26
|
+
* \c\M-x same as above
|
27
|
+
* \c? or \C-? delete, ASCII 7Fh (DEL)
|
28
|
+
*
|
29
|
+
* In addition to disabling interpolation, single-quoted strings also disable all
|
30
|
+
* escape sequences except for the single-quote (<tt>\'</tt>) and backslash
|
31
|
+
* (<tt>\\\\</tt>).
|
32
|
+
*/
|
33
|
+
const patterns = [
|
34
|
+
"[abtnvfres\\\\]", // simple
|
35
|
+
"[0-7]{1,3}", // octal bits
|
36
|
+
"x[0-9a-fA-F]{1,2}", // hex bit
|
37
|
+
"u[0-9a-fA-F]{4}", // unicode char
|
38
|
+
"u\\{[0-9a-fA-F ]+\\}", // unicode chars
|
39
|
+
"c[ -~]|C\\-[ -~]", // control
|
40
|
+
"M\\-[ -~]", // meta
|
41
|
+
"M\\-\\\\C\\-[ -~]|M\\-\\\\c[ -~]|c\\\\M\\-[ -~]", // meta control
|
42
|
+
"c\\?|C\\-\\?" // delete
|
43
|
+
];
|
44
|
+
|
45
|
+
module.exports = new RegExp(`\\\\(${patterns.join("|")})`);
|
data/src/nodes.js
ADDED
@@ -0,0 +1,594 @@
|
|
1
|
+
const {
|
2
|
+
align,
|
3
|
+
breakParent,
|
4
|
+
concat,
|
5
|
+
dedent,
|
6
|
+
group,
|
7
|
+
hardline,
|
8
|
+
ifBreak,
|
9
|
+
indent,
|
10
|
+
join,
|
11
|
+
line,
|
12
|
+
literalline,
|
13
|
+
markAsRoot,
|
14
|
+
removeLines,
|
15
|
+
softline,
|
16
|
+
trim
|
17
|
+
} = require("./builders");
|
18
|
+
|
19
|
+
const toProc = require("./toProc");
|
20
|
+
const {
|
21
|
+
concatBody,
|
22
|
+
empty,
|
23
|
+
first,
|
24
|
+
literal,
|
25
|
+
makeArgs,
|
26
|
+
makeCall,
|
27
|
+
makeList,
|
28
|
+
prefix,
|
29
|
+
skipAssignIndent
|
30
|
+
} = require("./utils");
|
31
|
+
|
32
|
+
const nodes = {
|
33
|
+
"@int": (path, _opts, _print) => {
|
34
|
+
const { body } = path.getValue();
|
35
|
+
|
36
|
+
// If the number is octal and does not contain the optional "o" character
|
37
|
+
// after the leading 0, add it in.
|
38
|
+
if (/^0[0-9]/.test(body)) {
|
39
|
+
return `0o${body.slice(1)}`;
|
40
|
+
}
|
41
|
+
|
42
|
+
// If the number is a decimal number, is sufficiently large, and is not
|
43
|
+
// already formatted with underscores, then add them in in between the
|
44
|
+
// numbers every three characters starting from the right.
|
45
|
+
if (!body.startsWith("0") && body.length >= 4 && !body.includes("_")) {
|
46
|
+
return ` ${body}`
|
47
|
+
.slice((body.length + 2) % 3)
|
48
|
+
.match(/.{3}/g)
|
49
|
+
.join("_")
|
50
|
+
.trim();
|
51
|
+
}
|
52
|
+
|
53
|
+
return body;
|
54
|
+
},
|
55
|
+
"@__end__": (path, _opts, _print) => {
|
56
|
+
const { body } = path.getValue();
|
57
|
+
return concat([trim, "__END__", literalline, body]);
|
58
|
+
},
|
59
|
+
access_ctrl: first,
|
60
|
+
arg_paren: (path, opts, print) => {
|
61
|
+
if (path.getValue().body[0] === null) {
|
62
|
+
return "";
|
63
|
+
}
|
64
|
+
|
65
|
+
const { addTrailingCommas } = opts;
|
66
|
+
const { args, heredocs } = makeArgs(path, opts, print, 0);
|
67
|
+
|
68
|
+
const argsNode = path.getValue().body[0];
|
69
|
+
const hasBlock = argsNode.type === "args_add_block" && argsNode.body[1];
|
70
|
+
|
71
|
+
if (heredocs.length > 1) {
|
72
|
+
return concat(["(", join(", ", args), ")"].concat(heredocs));
|
73
|
+
}
|
74
|
+
|
75
|
+
const parenDoc = group(
|
76
|
+
concat([
|
77
|
+
"(",
|
78
|
+
indent(
|
79
|
+
concat([
|
80
|
+
softline,
|
81
|
+
join(concat([",", line]), args),
|
82
|
+
addTrailingCommas && !hasBlock ? ifBreak(",", "") : ""
|
83
|
+
])
|
84
|
+
),
|
85
|
+
concat([softline, ")"])
|
86
|
+
])
|
87
|
+
);
|
88
|
+
|
89
|
+
if (heredocs.length === 1) {
|
90
|
+
return group(concat([parenDoc].concat(heredocs)));
|
91
|
+
}
|
92
|
+
|
93
|
+
return parenDoc;
|
94
|
+
},
|
95
|
+
args: (path, opts, print) => {
|
96
|
+
const args = path.map(print, "body");
|
97
|
+
let blockNode = null;
|
98
|
+
|
99
|
+
[1, 2, 3].find(parent => {
|
100
|
+
const parentNode = path.getParentNode(parent);
|
101
|
+
blockNode =
|
102
|
+
parentNode &&
|
103
|
+
parentNode.type === "method_add_block" &&
|
104
|
+
parentNode.body[1];
|
105
|
+
return blockNode;
|
106
|
+
});
|
107
|
+
|
108
|
+
const proc = blockNode && toProc(blockNode);
|
109
|
+
if (proc) {
|
110
|
+
args.push(proc);
|
111
|
+
}
|
112
|
+
|
113
|
+
return args;
|
114
|
+
},
|
115
|
+
args_add_block: (path, opts, print) => {
|
116
|
+
const parts = path.call(print, "body", 0);
|
117
|
+
|
118
|
+
if (path.getValue().body[1]) {
|
119
|
+
parts.push(concat(["&", path.call(print, "body", 1)]));
|
120
|
+
}
|
121
|
+
|
122
|
+
return parts;
|
123
|
+
},
|
124
|
+
args_add_star: (path, opts, print) => {
|
125
|
+
const printed = path.map(print, "body");
|
126
|
+
const parts = printed[0]
|
127
|
+
.concat([concat(["*", printed[1]])])
|
128
|
+
.concat(printed.slice(2));
|
129
|
+
|
130
|
+
return parts;
|
131
|
+
},
|
132
|
+
assoc_splat: prefix("**"),
|
133
|
+
assign: (path, opts, print) => {
|
134
|
+
const [printedTarget, printedValue] = path.map(print, "body");
|
135
|
+
let adjustedValue = printedValue;
|
136
|
+
|
137
|
+
if (
|
138
|
+
["mrhs_add_star", "mrhs_new_from_args"].includes(
|
139
|
+
path.getValue().body[1].type
|
140
|
+
)
|
141
|
+
) {
|
142
|
+
adjustedValue = group(join(concat([",", line]), printedValue));
|
143
|
+
}
|
144
|
+
|
145
|
+
if (skipAssignIndent(path.getValue().body[1])) {
|
146
|
+
return group(concat([printedTarget, " = ", adjustedValue]));
|
147
|
+
}
|
148
|
+
|
149
|
+
return group(
|
150
|
+
concat([printedTarget, " =", indent(concat([line, adjustedValue]))])
|
151
|
+
);
|
152
|
+
},
|
153
|
+
assign_error: (_path, _opts, _print) => {
|
154
|
+
throw new Error("Can't set variable");
|
155
|
+
},
|
156
|
+
binary: (path, opts, print) => {
|
157
|
+
const operator = path.getValue().body[1];
|
158
|
+
const useNoSpace = operator === "**";
|
159
|
+
|
160
|
+
return group(
|
161
|
+
concat([
|
162
|
+
concat([path.call(print, "body", 0), useNoSpace ? "" : " "]),
|
163
|
+
operator,
|
164
|
+
indent(
|
165
|
+
concat([useNoSpace ? softline : line, path.call(print, "body", 2)])
|
166
|
+
)
|
167
|
+
])
|
168
|
+
);
|
169
|
+
},
|
170
|
+
block_var: (path, opts, print) => {
|
171
|
+
const parts = ["|", removeLines(path.call(print, "body", 0))];
|
172
|
+
|
173
|
+
// The second part of this node is a list of optional block-local variables
|
174
|
+
if (path.getValue().body[1]) {
|
175
|
+
parts.push("; ", join(", ", path.map(print, "body", 1)));
|
176
|
+
}
|
177
|
+
|
178
|
+
parts.push("| ");
|
179
|
+
return concat(parts);
|
180
|
+
},
|
181
|
+
blockarg: (path, opts, print) => concat(["&", path.call(print, "body", 0)]),
|
182
|
+
bodystmt: (path, opts, print) => {
|
183
|
+
const [_statements, rescue, elseClause, ensure] = path.getValue().body;
|
184
|
+
const parts = [path.call(print, "body", 0)];
|
185
|
+
|
186
|
+
if (rescue) {
|
187
|
+
parts.push(dedent(concat([hardline, path.call(print, "body", 1)])));
|
188
|
+
}
|
189
|
+
|
190
|
+
if (elseClause) {
|
191
|
+
// Before Ruby 2.6, this piece of bodystmt was an explicit "else" node
|
192
|
+
const stmts =
|
193
|
+
elseClause.type === "else"
|
194
|
+
? path.call(print, "body", 2, "body", 0)
|
195
|
+
: path.call(print, "body", 2);
|
196
|
+
|
197
|
+
parts.push(concat([dedent(concat([hardline, "else"])), hardline, stmts]));
|
198
|
+
}
|
199
|
+
|
200
|
+
if (ensure) {
|
201
|
+
parts.push(dedent(concat([hardline, path.call(print, "body", 3)])));
|
202
|
+
}
|
203
|
+
|
204
|
+
return group(concat(parts));
|
205
|
+
},
|
206
|
+
break: (path, opts, print) => {
|
207
|
+
const content = path.getValue().body[0];
|
208
|
+
|
209
|
+
if (content.body.length === 0) {
|
210
|
+
return "break";
|
211
|
+
}
|
212
|
+
|
213
|
+
if (content.body[0].body[0].type === "paren") {
|
214
|
+
return concat([
|
215
|
+
"break ",
|
216
|
+
path.call(print, "body", 0, "body", 0, "body", 0, "body", 0)
|
217
|
+
]);
|
218
|
+
}
|
219
|
+
|
220
|
+
return concat(["break ", join(", ", path.call(print, "body", 0))]);
|
221
|
+
},
|
222
|
+
class: (path, opts, print) => {
|
223
|
+
const [_constant, superclass, statements] = path.getValue().body;
|
224
|
+
|
225
|
+
const parts = ["class ", path.call(print, "body", 0)];
|
226
|
+
if (superclass) {
|
227
|
+
parts.push(" < ", path.call(print, "body", 1));
|
228
|
+
}
|
229
|
+
|
230
|
+
// If the body is empty, we can replace with a ;
|
231
|
+
const stmts = statements.body[0].body;
|
232
|
+
if (stmts.length === 1 && stmts[0].type === "void_stmt") {
|
233
|
+
return group(concat([concat(parts), ifBreak(line, "; "), "end"]));
|
234
|
+
}
|
235
|
+
|
236
|
+
return group(
|
237
|
+
concat([
|
238
|
+
concat(parts),
|
239
|
+
indent(concat([hardline, path.call(print, "body", 2)])),
|
240
|
+
concat([hardline, "end"])
|
241
|
+
])
|
242
|
+
);
|
243
|
+
},
|
244
|
+
class_name_error: (_path, _opts, _print) => {
|
245
|
+
throw new Error("class/module name must be CONSTANT");
|
246
|
+
},
|
247
|
+
const_path_field: (path, opts, print) => join("::", path.map(print, "body")),
|
248
|
+
const_path_ref: (path, opts, print) => join("::", path.map(print, "body")),
|
249
|
+
const_ref: first,
|
250
|
+
defined: (path, opts, print) =>
|
251
|
+
group(
|
252
|
+
concat([
|
253
|
+
"defined?(",
|
254
|
+
indent(concat([softline, path.call(print, "body", 0)])),
|
255
|
+
concat([softline, ")"])
|
256
|
+
])
|
257
|
+
),
|
258
|
+
dot2: (path, opts, print) =>
|
259
|
+
concat([
|
260
|
+
path.call(print, "body", 0),
|
261
|
+
"..",
|
262
|
+
path.getValue().body[1] ? path.call(print, "body", 1) : ""
|
263
|
+
]),
|
264
|
+
dot3: (path, opts, print) =>
|
265
|
+
concat([
|
266
|
+
path.call(print, "body", 0),
|
267
|
+
"...",
|
268
|
+
path.getValue().body[1] ? path.call(print, "body", 1) : ""
|
269
|
+
]),
|
270
|
+
dyna_symbol: (path, opts, print) => {
|
271
|
+
const { quote } = path.getValue().body[0];
|
272
|
+
|
273
|
+
return concat([":", quote, concat(path.call(print, "body", 0)), quote]);
|
274
|
+
},
|
275
|
+
else: (path, opts, print) => {
|
276
|
+
const stmts = path.getValue().body[0];
|
277
|
+
|
278
|
+
return concat([
|
279
|
+
stmts.body.length === 1 && stmts.body[0].type === "command"
|
280
|
+
? breakParent
|
281
|
+
: "",
|
282
|
+
"else",
|
283
|
+
indent(concat([softline, path.call(print, "body", 0)]))
|
284
|
+
]);
|
285
|
+
},
|
286
|
+
embdoc: (path, _opts, _print) => concat([trim, path.getValue().body]),
|
287
|
+
excessed_comma: empty,
|
288
|
+
fcall: concatBody,
|
289
|
+
field: (path, opts, print) =>
|
290
|
+
group(
|
291
|
+
concat([
|
292
|
+
path.call(print, "body", 0),
|
293
|
+
concat([makeCall(path, opts, print), path.call(print, "body", 2)])
|
294
|
+
])
|
295
|
+
),
|
296
|
+
massign: (path, opts, print) => {
|
297
|
+
let right = path.call(print, "body", 1);
|
298
|
+
|
299
|
+
if (
|
300
|
+
["mrhs_add_star", "mrhs_new_from_args"].includes(
|
301
|
+
path.getValue().body[1].type
|
302
|
+
)
|
303
|
+
) {
|
304
|
+
right = group(join(concat([",", line]), right));
|
305
|
+
}
|
306
|
+
|
307
|
+
return group(
|
308
|
+
concat([
|
309
|
+
group(join(concat([",", line]), path.call(print, "body", 0))),
|
310
|
+
" =",
|
311
|
+
indent(concat([line, right]))
|
312
|
+
])
|
313
|
+
);
|
314
|
+
},
|
315
|
+
method_add_arg: (path, opts, print) => {
|
316
|
+
const [method, args] = path.map(print, "body");
|
317
|
+
const argNode = path.getValue().body[1];
|
318
|
+
|
319
|
+
// This case will ONLY be hit if we can successfully turn the block into a
|
320
|
+
// to_proc call. In that case, we just explicitly add the parens around it.
|
321
|
+
if (argNode.type === "args" && args.length > 0) {
|
322
|
+
return concat([method, "("].concat(args).concat(")"));
|
323
|
+
}
|
324
|
+
|
325
|
+
return concat([method, args]);
|
326
|
+
},
|
327
|
+
method_add_block: (path, opts, print) => {
|
328
|
+
const [method, block] = path.getValue().body;
|
329
|
+
const proc = toProc(block);
|
330
|
+
|
331
|
+
if (proc && method.type === "call") {
|
332
|
+
return group(
|
333
|
+
concat([
|
334
|
+
path.call(print, "body", 0),
|
335
|
+
"(",
|
336
|
+
indent(concat([softline, proc])),
|
337
|
+
concat([softline, ")"])
|
338
|
+
])
|
339
|
+
);
|
340
|
+
}
|
341
|
+
if (proc) {
|
342
|
+
return path.call(print, "body", 0);
|
343
|
+
}
|
344
|
+
return concat(path.map(print, "body"));
|
345
|
+
},
|
346
|
+
methref: (path, opts, print) => join(".:", path.map(print, "body")),
|
347
|
+
mlhs: makeList,
|
348
|
+
mlhs_add_post: (path, opts, print) =>
|
349
|
+
path.call(print, "body", 0).concat(path.call(print, "body", 1)),
|
350
|
+
mlhs_add_star: (path, opts, print) =>
|
351
|
+
path
|
352
|
+
.call(print, "body", 0)
|
353
|
+
.concat([
|
354
|
+
path.getValue().body[1]
|
355
|
+
? concat(["*", path.call(print, "body", 1)])
|
356
|
+
: "*"
|
357
|
+
]),
|
358
|
+
mlhs_paren: (path, opts, print) => {
|
359
|
+
if (["massign", "mlhs_paren"].includes(path.getParentNode().type)) {
|
360
|
+
// If we're nested in brackets as part of the left hand side of an
|
361
|
+
// assignment, i.e., (a, b, c) = 1, 2, 3
|
362
|
+
// ignore the current node and just go straight to the content
|
363
|
+
return path.call(print, "body", 0);
|
364
|
+
}
|
365
|
+
|
366
|
+
return group(
|
367
|
+
concat([
|
368
|
+
"(",
|
369
|
+
indent(
|
370
|
+
concat([
|
371
|
+
softline,
|
372
|
+
join(concat([",", line]), path.call(print, "body", 0))
|
373
|
+
])
|
374
|
+
),
|
375
|
+
concat([softline, ")"])
|
376
|
+
])
|
377
|
+
);
|
378
|
+
},
|
379
|
+
mrhs: makeList,
|
380
|
+
mrhs_add_star: (path, opts, print) =>
|
381
|
+
path
|
382
|
+
.call(print, "body", 0)
|
383
|
+
.concat([concat(["*", path.call(print, "body", 1)])]),
|
384
|
+
mrhs_new_from_args: (path, opts, print) => {
|
385
|
+
const parts = path.call(print, "body", 0);
|
386
|
+
|
387
|
+
if (path.getValue().body.length > 1) {
|
388
|
+
parts.push(path.call(print, "body", 1));
|
389
|
+
}
|
390
|
+
|
391
|
+
return parts;
|
392
|
+
},
|
393
|
+
module: (path, opts, print) => {
|
394
|
+
const declaration = group(concat(["module ", path.call(print, "body", 0)]));
|
395
|
+
|
396
|
+
// If the body is empty, we can replace with a ;
|
397
|
+
const stmts = path.getValue().body[1].body[0].body;
|
398
|
+
if (stmts.length === 1 && stmts[0].type === "void_stmt") {
|
399
|
+
return group(concat([declaration, ifBreak(line, "; "), "end"]));
|
400
|
+
}
|
401
|
+
|
402
|
+
return group(
|
403
|
+
concat([
|
404
|
+
declaration,
|
405
|
+
indent(concat([hardline, path.call(print, "body", 1)])),
|
406
|
+
concat([hardline, "end"])
|
407
|
+
])
|
408
|
+
);
|
409
|
+
},
|
410
|
+
next: (path, opts, print) => {
|
411
|
+
const args = path.getValue().body[0].body[0];
|
412
|
+
|
413
|
+
if (!args) {
|
414
|
+
return "next";
|
415
|
+
}
|
416
|
+
|
417
|
+
if (args.body[0].type === "paren") {
|
418
|
+
// Ignoring the parens node and just going straight to the content
|
419
|
+
return concat([
|
420
|
+
"next ",
|
421
|
+
path.call(print, "body", 0, "body", 0, "body", 0, "body", 0)
|
422
|
+
]);
|
423
|
+
}
|
424
|
+
|
425
|
+
return concat(["next ", join(", ", path.call(print, "body", 0))]);
|
426
|
+
},
|
427
|
+
number_arg: first,
|
428
|
+
opassign: (path, opts, print) =>
|
429
|
+
group(
|
430
|
+
concat([
|
431
|
+
path.call(print, "body", 0),
|
432
|
+
" ",
|
433
|
+
path.call(print, "body", 1),
|
434
|
+
indent(concat([line, path.call(print, "body", 2)]))
|
435
|
+
])
|
436
|
+
),
|
437
|
+
paren: (path, opts, print) => {
|
438
|
+
if (!path.getValue().body[0]) {
|
439
|
+
return "()";
|
440
|
+
}
|
441
|
+
|
442
|
+
let content = path.call(print, "body", 0);
|
443
|
+
|
444
|
+
if (
|
445
|
+
["args", "args_add_star", "args_add_block"].includes(
|
446
|
+
path.getValue().body[0].type
|
447
|
+
)
|
448
|
+
) {
|
449
|
+
content = join(concat([",", line]), content);
|
450
|
+
}
|
451
|
+
|
452
|
+
return group(
|
453
|
+
concat([
|
454
|
+
"(",
|
455
|
+
indent(concat([softline, content])),
|
456
|
+
concat([softline, ")"])
|
457
|
+
])
|
458
|
+
);
|
459
|
+
},
|
460
|
+
program: (path, opts, print) =>
|
461
|
+
markAsRoot(
|
462
|
+
concat([join(literalline, path.map(print, "body")), literalline])
|
463
|
+
),
|
464
|
+
return: (path, opts, print) => {
|
465
|
+
const args = path.getValue().body[0].body[0];
|
466
|
+
|
467
|
+
if (!args) {
|
468
|
+
return "return";
|
469
|
+
}
|
470
|
+
|
471
|
+
if (args.body[0] && args.body[0].type === "paren") {
|
472
|
+
// Ignoring the parens node and just going straight to the content
|
473
|
+
return concat([
|
474
|
+
"return ",
|
475
|
+
path.call(print, "body", 0, "body", 0, "body", 0, "body", 0)
|
476
|
+
]);
|
477
|
+
}
|
478
|
+
|
479
|
+
return concat(["return ", join(", ", path.call(print, "body", 0))]);
|
480
|
+
},
|
481
|
+
return0: literal("return"),
|
482
|
+
sclass: (path, opts, print) =>
|
483
|
+
group(
|
484
|
+
concat([
|
485
|
+
concat(["class << ", path.call(print, "body", 0)]),
|
486
|
+
indent(concat([hardline, path.call(print, "body", 1)])),
|
487
|
+
concat([hardline, "end"])
|
488
|
+
])
|
489
|
+
),
|
490
|
+
stmts: (path, opts, print) => {
|
491
|
+
const stmts = path.getValue().body;
|
492
|
+
const parts = [];
|
493
|
+
let lineNo = null;
|
494
|
+
|
495
|
+
stmts.forEach((stmt, index) => {
|
496
|
+
if (stmt.type === "void_stmt") {
|
497
|
+
return;
|
498
|
+
}
|
499
|
+
|
500
|
+
const printed = path.call(print, "body", index);
|
501
|
+
|
502
|
+
if (lineNo === null) {
|
503
|
+
parts.push(printed);
|
504
|
+
} else if (
|
505
|
+
stmt.start - lineNo > 1 ||
|
506
|
+
[stmt.type, stmts[index - 1].type].includes("access_ctrl")
|
507
|
+
) {
|
508
|
+
parts.push(hardline, hardline, printed);
|
509
|
+
} else if (
|
510
|
+
stmt.start !== lineNo ||
|
511
|
+
path.getParentNode().type !== "string_embexpr"
|
512
|
+
) {
|
513
|
+
parts.push(hardline, printed);
|
514
|
+
} else {
|
515
|
+
parts.push("; ", printed);
|
516
|
+
}
|
517
|
+
|
518
|
+
lineNo = stmt.end;
|
519
|
+
});
|
520
|
+
|
521
|
+
return concat(parts);
|
522
|
+
},
|
523
|
+
super: (path, opts, print) => {
|
524
|
+
const args = path.getValue().body[0];
|
525
|
+
|
526
|
+
if (args.type === "arg_paren") {
|
527
|
+
// In case there are explicitly no arguments but they are using parens,
|
528
|
+
// we assume they are attempting to override the initializer and pass no
|
529
|
+
// arguments up.
|
530
|
+
if (args.body[0] === null) {
|
531
|
+
return "super()";
|
532
|
+
}
|
533
|
+
|
534
|
+
return concat(["super", path.call(print, "body", 0)]);
|
535
|
+
}
|
536
|
+
|
537
|
+
return concat(["super ", join(", ", path.call(print, "body", 0))]);
|
538
|
+
},
|
539
|
+
symbol: prefix(":"),
|
540
|
+
symbol_literal: concatBody,
|
541
|
+
top_const_field: prefix("::"),
|
542
|
+
top_const_ref: prefix("::"),
|
543
|
+
unary: (path, opts, print) => {
|
544
|
+
const operator = path.getValue().body[0];
|
545
|
+
|
546
|
+
return concat([
|
547
|
+
operator === "not" ? "not " : operator[0],
|
548
|
+
path.call(print, "body", 1)
|
549
|
+
]);
|
550
|
+
},
|
551
|
+
undef: (path, opts, print) =>
|
552
|
+
group(
|
553
|
+
concat([
|
554
|
+
"undef ",
|
555
|
+
align(
|
556
|
+
"undef ".length,
|
557
|
+
join(concat([",", line]), path.map(print, "body", 0))
|
558
|
+
)
|
559
|
+
])
|
560
|
+
),
|
561
|
+
var_field: concatBody,
|
562
|
+
var_ref: first,
|
563
|
+
vcall: first,
|
564
|
+
yield: (path, opts, print) => {
|
565
|
+
if (path.getValue().body[0].type === "paren") {
|
566
|
+
return concat(["yield", path.call(print, "body", 0)]);
|
567
|
+
}
|
568
|
+
|
569
|
+
return concat(["yield ", join(", ", path.call(print, "body", 0))]);
|
570
|
+
},
|
571
|
+
yield0: literal("yield"),
|
572
|
+
zsuper: literal("super")
|
573
|
+
};
|
574
|
+
|
575
|
+
module.exports = Object.assign(
|
576
|
+
{},
|
577
|
+
require("./nodes/alias"),
|
578
|
+
require("./nodes/arrays"),
|
579
|
+
require("./nodes/blocks"),
|
580
|
+
require("./nodes/calls"),
|
581
|
+
require("./nodes/case"),
|
582
|
+
require("./nodes/commands"),
|
583
|
+
require("./nodes/conditionals"),
|
584
|
+
require("./nodes/hashes"),
|
585
|
+
require("./nodes/hooks"),
|
586
|
+
require("./nodes/lambdas"),
|
587
|
+
require("./nodes/loops"),
|
588
|
+
require("./nodes/methods"),
|
589
|
+
require("./nodes/params"),
|
590
|
+
require("./nodes/regexp"),
|
591
|
+
require("./nodes/rescue"),
|
592
|
+
require("./nodes/strings"),
|
593
|
+
nodes
|
594
|
+
);
|