src/ruby/nodes/rescue.js in prettier-1.3.0 vs src/ruby/nodes/rescue.js in prettier-1.4.0
- old
+ new
@@ -24,54 +24,60 @@
indent(concat([hardline, path.call(print, "body", 1)]))
]);
}
function printRescue(path, opts, print) {
- const [exception, variable, _stmts, addition] = path.getValue().body;
const parts = ["rescue"];
- if (exception || variable) {
- if (exception) {
- if (Array.isArray(exception)) {
- // In this case, it's actually only the one exception (it's an array
- // of length 1).
- parts.push(" ", path.call(print, "body", 0, 0));
- } else {
- // Here we have multiple exceptions from which we're rescuing, so we
- // need to align them and join them together.
- const joiner = concat([",", line]);
- const exceptions = group(join(joiner, path.call(print, "body", 0)));
-
- parts.push(" ", align("rescue ".length, exceptions));
- }
- }
-
- if (variable) {
- parts.push(" => ", path.call(print, "body", 1));
- }
+ if (path.getValue().body[0]) {
+ parts.push(align("rescue ".length, path.call(print, "body", 0)));
} else {
// If you don't specify an error to rescue in a `begin/rescue` block, then
// implicitly you're rescuing from `StandardError`. In this case, we're
// just going to explicitly add it.
parts.push(" StandardError");
}
- const rescueBody = path.call(print, "body", 2);
+ const bodystmt = path.call(print, "body", 1);
- if (rescueBody.parts.length > 0) {
- parts.push(indent(concat([hardline, rescueBody])));
+ if (bodystmt.parts.length > 0) {
+ parts.push(indent(concat([hardline, bodystmt])));
}
// This is the next clause on the `begin` statement, either another
// `rescue`, and `ensure`, or an `else` clause.
- if (addition) {
- parts.push(concat([hardline, path.call(print, "body", 3)]));
+ if (path.getValue().body[2]) {
+ parts.push(concat([hardline, path.call(print, "body", 2)]));
}
return group(concat(parts));
}
+// This is a container node that we're adding into the AST that isn't present in
+// Ripper solely so that we have a nice place to attach inline comments.
+function printRescueEx(path, opts, print) {
+ const [exception, variable] = path.getValue().body;
+ const parts = [];
+
+ if (exception) {
+ let exceptionDoc = path.call(print, "body", 0);
+
+ if (Array.isArray(exceptionDoc)) {
+ const joiner = concat([",", line]);
+ exceptionDoc = group(join(joiner, exceptionDoc));
+ }
+
+ parts.push(" ", exceptionDoc);
+ }
+
+ if (variable) {
+ parts.push(" => ", path.call(print, "body", 1));
+ }
+
+ return group(concat(parts));
+}
+
function printRescueMod(path, opts, print) {
const [statementDoc, valueDoc] = path.map(print, "body");
return concat([
"begin",
@@ -87,8 +93,9 @@
module.exports = {
begin: printBegin,
ensure: printEnsure,
redo: literal("redo"),
rescue: printRescue,
+ rescue_ex: printRescueEx,
rescue_mod: printRescueMod,
retry: literal("retry")
};