dist/ruby/nodes/flow.js in prettier-2.0.0 vs dist/ruby/nodes/flow.js in prettier-2.1.0
- old
+ new
@@ -3,64 +3,57 @@
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.printYield0 = exports.printYield = exports.printNext = exports.printBreak = void 0;
const prettier_1 = __importDefault(require("../../prettier"));
-const utils_1 = require("../../utils");
const { join } = prettier_1.default;
-function nodeDive(node, steps) {
- let current = node;
- steps.forEach((step) => {
- current = current[step];
- });
- return current;
-}
+// If there are parentheses around these nodes, then we can't skip printing them
+// without changing the semantic meaning of the statement.
const unskippableParens = [
"if_mod",
"rescue_mod",
"unless_mod",
"until_mod",
"while_mod"
];
-function maybeHandleParens(path, print, keyword, steps) {
- const node = nodeDive(path.getValue(), steps);
- if (node.type !== "paren") {
- return null;
- }
- const stmts = node.body[0].body;
- if (stmts.length === 1 && !unskippableParens.includes(stmts[0].type)) {
- return [`${keyword} `, path.call(print, ...steps, "body", 0)];
- }
- return [keyword, path.call(print, ...steps)];
+function printFlowControl(keyword) {
+ return function printFlowControlWithKeyword(path, opts, print) {
+ const node = path.getValue();
+ // If we don't have any arguments to the keyword, then it's always going to
+ // come in as an args node, in which case we can just print the keyword.
+ if (node.args.type === "args") {
+ return keyword;
+ }
+ // Special handling if we've got parentheses on this call to the keyword. If
+ // we do and we can skip them, then we will. If we don't, then we'll print
+ // them right after the keyword with no space.
+ const paren = node.args.args.type === "args" && node.args.args.parts[0];
+ if (paren && paren.type === "paren") {
+ const stmts = paren.cnts.body;
+ // Here we're checking if we can skip past the parentheses entirely.
+ if (stmts.length === 1 && !unskippableParens.includes(stmts[0].type)) {
+ return [
+ `${keyword} `,
+ path.call(print, "args", "args", "parts", 0, "cnts")
+ ];
+ }
+ // Here we know we can't, so just printing out the parentheses as normal.
+ return [keyword, path.call(print, "args", "args", "parts", 0)];
+ }
+ // If we didn't hit the super special handling, then we're just going to
+ // print out the arguments to the keyword like normal.
+ return [`${keyword} `, join(", ", path.call(print, "args"))];
+ };
}
-const printBreak = (path, opts, print) => {
- const content = path.getValue().body[0];
- if (content.body.length === 0) {
- return "break";
- }
- const steps = ["body", 0, "body", 0, "body", 0];
- return (maybeHandleParens(path, print, "break", steps) || [
- "break ",
- join(", ", path.call(print, "body", 0))
- ]);
-};
-exports.printBreak = printBreak;
-const printNext = (path, opts, print) => {
- const args = path.getValue().body[0].body[0];
- if (!args) {
- return "next";
- }
- const steps = ["body", 0, "body", 0, "body", 0];
- return (maybeHandleParens(path, print, "next", steps) || [
- "next ",
- join(", ", path.call(print, "body", 0))
- ]);
-};
-exports.printNext = printNext;
+exports.printBreak = printFlowControl("break");
+exports.printNext = printFlowControl("next");
const printYield = (path, opts, print) => {
- if (path.getValue().body[0].type === "paren") {
- return ["yield", path.call(print, "body", 0)];
+ const node = path.getValue();
+ const argsDoc = path.call(print, "args");
+ if (node.args.type === "paren") {
+ return ["yield", argsDoc];
}
- return ["yield ", join(", ", path.call(print, "body", 0))];
+ return ["yield ", join(", ", argsDoc)];
};
exports.printYield = printYield;
-exports.printYield0 = (0, utils_1.literal)("yield");
+const printYield0 = (path) => path.getValue().value;
+exports.printYield0 = printYield0;