dist/ruby/nodes/commands.js in prettier-2.0.0 vs dist/ruby/nodes/commands.js in prettier-2.1.0
- old
+ new
@@ -8,32 +8,40 @@
const utils_1 = require("../../utils");
const { align, group, ifBreak, indent, join, line, softline } = prettier_1.default;
function throwBadDoc(doc) {
throw new Error(`Unknown doc ${doc}`);
}
+function reduceDocLength(sum, doc) {
+ // If we've hit a line, then we're going to start the counting back at 0 since
+ // it will be starting from the beginning of a line.
+ if (typeof doc === "object" && !Array.isArray(doc) && doc.type === "line") {
+ return 0;
+ }
+ return sum + docBreakLength(doc);
+}
// Loop through the already created doc nodes and determine the overall length
// so that we can properly align the command arguments.
-function docLength(doc) {
+function docBreakLength(doc) {
if (Array.isArray(doc)) {
- return doc.reduce((sum, child) => sum + docLength(child), 0);
+ return doc.reduce(reduceDocLength, 0);
}
if (typeof doc === "string") {
return doc.length;
}
switch (doc.type) {
case "concat":
case "fill":
- return doc.parts.reduce((sum, child) => sum + docLength(child), 0);
+ return doc.parts.reduce(reduceDocLength, 0);
case "align":
case "group":
case "indent":
case "line-suffix":
- return docLength(doc.contents);
+ return docBreakLength(doc.contents);
case "if-break":
- return docLength(doc.flatContents);
+ return docBreakLength(doc.breakContents);
case "line":
- return doc.soft ? 0 : 1;
+ return 0;
case "break-parent":
case "cursor":
case "indent-if-break":
case "label":
case "line-suffix-boundary":
@@ -42,14 +50,14 @@
default:
throwBadDoc(doc);
}
}
function hasDef(node) {
- return (node.body[1].type === "args_add_block" &&
- node.body[1].body[0].type === "args" &&
- node.body[1].body[0].body[0] &&
- ["def", "defs"].includes(node.body[1].body[0].body[0].type));
+ return (node.args.type === "args_add_block" &&
+ node.args.args.type === "args" &&
+ node.args.args.parts[0] &&
+ ["def", "defs"].includes(node.args.args.parts[0].type));
}
// Very special handling case for rspec matchers. In general with rspec matchers
// you expect to see something like:
//
// expect(foo).to receive(:bar).with(
@@ -61,32 +69,38 @@
// )
//
// In this case the arguments are aligned to the left side as opposed to being
// aligned with the `receive` call.
function skipArgsAlign(node) {
- return ["to", "not_to", "to_not"].includes(node.body[2].body);
+ return ["to", "not_to", "to_not"].includes(node.message.value);
}
// If there is a ternary argument to a command and it's going to get broken
// into multiple lines, then we're going to have to use parentheses around the
// command in order to make sure operator precedence doesn't get messed up.
function hasTernaryArg(node) {
- return node.body[0].body.some((child) => child.type === "ifop");
+ var _a;
+ switch (node.type) {
+ case "args":
+ return node.parts.some((child) => child.type === "ifop");
+ case "args_add_block":
+ return hasTernaryArg(node.args) || ((_a = node.block) === null || _a === void 0 ? void 0 : _a.type) === "ifop";
+ }
}
const printCommand = (path, opts, print) => {
const node = path.getValue();
- const command = path.call(print, "body", 0);
- const joinedArgs = join([",", line], path.call(print, "body", 1));
- const hasTernary = hasTernaryArg(node.body[1]);
+ const command = path.call(print, "message");
+ const joinedArgs = join([",", line], path.call(print, "args"));
+ const hasTernary = hasTernaryArg(node.args);
let breakArgs;
if (hasTernary) {
breakArgs = indent([softline, joinedArgs]);
}
else if (hasDef(node)) {
breakArgs = joinedArgs;
}
else {
- breakArgs = align(docLength(command) + 1, joinedArgs);
+ breakArgs = align(docBreakLength(command) + 1, joinedArgs);
}
return group(ifBreak([
command,
hasTernary ? "(" : " ",
breakArgs,
@@ -95,29 +109,29 @@
};
exports.printCommand = printCommand;
const printCommandCall = (path, opts, print) => {
const node = path.getValue();
const parts = [
- path.call(print, "body", 0),
+ path.call(print, "receiver"),
(0, utils_1.makeCall)(path, opts, print),
- path.call(print, "body", 2)
+ path.call(print, "message")
];
- if (!node.body[3]) {
+ if (!node.args) {
return parts;
}
- const argDocs = join([",", line], path.call(print, "body", 3));
+ const argDocs = join([",", line], path.call(print, "args"));
let breakDoc;
- if (hasTernaryArg(node.body[3])) {
+ if (hasTernaryArg(node.args)) {
breakDoc = parts.concat("(", indent([softline, argDocs]), softline, ")");
parts.push(" ");
}
else if (skipArgsAlign(node)) {
parts.push(" ");
breakDoc = parts.concat(argDocs);
}
else {
parts.push(" ");
- breakDoc = parts.concat(align(docLength(parts), argDocs));
+ breakDoc = parts.concat(align(docBreakLength(parts), argDocs));
}
const joinedDoc = parts.concat(argDocs);
return group(ifBreak(breakDoc, joinedDoc));
};
exports.printCommandCall = printCommandCall;