dist/ruby/nodes/loops.js in prettier-2.0.0 vs dist/ruby/nodes/loops.js in prettier-2.1.0
- old
+ new
@@ -1,30 +1,31 @@
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
-exports.printUntilModifer = exports.printUntil = exports.printWhileModifier = exports.printWhile = exports.printFor = void 0;
+exports.printUntil = exports.printWhile = exports.printFor = void 0;
const prettier_1 = __importDefault(require("../../prettier"));
const utils_1 = require("../../utils");
const { align, breakParent, group, hardline, ifBreak, indent, join, softline } = prettier_1.default;
-function printLoop(keyword, modifier) {
+function isModifier(node) {
+ return node.type === "while_mod" || node.type === "until_mod";
+}
+function printLoop(keyword) {
return function printLoopWithOptions(path, { rubyModifier }, print) {
- const [, stmts] = path.getValue().body;
+ const node = path.getValue();
+ const predicateDoc = path.call(print, "pred");
// If the only statement inside this while loop is a void statement, then we
// can shorten to just displaying the predicate and then a semicolon.
- if ((0, utils_1.isEmptyStmts)(stmts)) {
- return group([
- group([keyword, " ", path.call(print, "body", 0)]),
- hardline,
- "end"
- ]);
+ if (!isModifier(node) && (0, utils_1.isEmptyStmts)(node.stmts)) {
+ return group([group([keyword, " ", predicateDoc]), hardline, "end"]);
}
+ const statementDoc = path.call(print, isModifier(node) ? "stmt" : "stmts");
const inlineLoop = (0, utils_1.inlineEnsureParens)(path, [
- path.call(print, "body", 1),
+ statementDoc,
` ${keyword} `,
- path.call(print, "body", 0)
+ predicateDoc
]);
// If we're in the modifier form and we're modifying a `begin`, then this is
// a special case where we need to explicitly use the modifier form because
// otherwise the semantic meaning changes. This looks like:
//
@@ -32,42 +33,43 @@
// foo
// end while bar
//
// The above is effectively a `do...while` loop (which we don't have in
// ruby).
- if (modifier && path.getValue().body[1].type === "begin") {
+ if (isModifier(node) && node.stmt.type === "begin") {
return inlineLoop;
}
const blockLoop = [
- [`${keyword} `, align(keyword.length + 1, path.call(print, "body", 0))],
- indent([softline, path.call(print, "body", 1)]),
+ [`${keyword} `, align(keyword.length + 1, predicateDoc)],
+ indent([softline, statementDoc]),
softline,
"end"
];
// If we're disallowing inline loops or if the predicate of the loop
// contains an assignment (in which case we can't know for certain that that
// assignment doesn't impact the statements inside the loop) then we can't
// use the modifier form and we must use the block form.
- if (!rubyModifier || (0, utils_1.containsAssignment)(path.getValue().body[0])) {
+ if (!rubyModifier || (0, utils_1.containsAssignment)(node.pred)) {
return [breakParent, blockLoop];
}
return group(ifBreak(blockLoop, inlineLoop));
};
}
const printFor = (path, opts, print) => {
- const [varDoc, rangeDoc, stmtsDoc] = path.map(print, "body");
- const varsDoc = path.getValue().body[0].type === "mlhs" ? join(", ", varDoc) : varDoc;
+ const node = path.getValue();
+ let indexDoc = path.call(print, "index");
+ if (node.index.type === "mlhs") {
+ indexDoc = join(", ", indexDoc);
+ }
return group([
"for ",
- varsDoc,
+ indexDoc,
" in ",
- rangeDoc,
- indent([hardline, stmtsDoc]),
+ path.call(print, "collection"),
+ indent([hardline, path.call(print, "stmts")]),
hardline,
"end"
]);
};
exports.printFor = printFor;
-exports.printWhile = printLoop("while", false);
-exports.printWhileModifier = printLoop("while", true);
-exports.printUntil = printLoop("until", false);
-exports.printUntilModifer = printLoop("until", true);
+exports.printWhile = printLoop("while");
+exports.printUntil = printLoop("until");