dist/ruby/nodes/methods.js in prettier-2.0.0 vs dist/ruby/nodes/methods.js in prettier-2.1.0

- old
+ new

@@ -1,70 +1,50 @@ "use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); -exports.printAccessControl = exports.printSingleLineMethod = exports.printDef = void 0; +exports.printAccessControl = exports.printDefEndless = exports.printDef = void 0; const prettier_1 = __importDefault(require("../../prettier")); const utils_1 = require("../../utils"); const { group, hardline, indent, line } = prettier_1.default; const printDef = (path, opts, print) => { const node = path.getValue(); const declaration = ["def "]; - let paramsNode; - let bodystmtNode; - let nameDoc; - let paramsDoc; - let bodystmtDoc; - if (node.type === "def") { - paramsNode = node.body[1]; - bodystmtNode = node.body[2]; - nameDoc = path.call(print, "body", 0); - paramsDoc = path.call(print, "body", 1); - bodystmtDoc = path.call(print, "body", 2); + // In this case, we're printing a method that's defined as a singleton, so + // we need to include the target and the operator before the name. + if (node.type === "defs") { + declaration.push(path.call(print, "target"), path.call(print, "op")); } - else { - // In this case, we're printing a method that's defined as a singleton, so - // we need to include the target and the operator - declaration.push(path.call(print, "body", 0), path.call(print, "body", 1)); - paramsNode = node.body[3]; - bodystmtNode = node.body[4]; - nameDoc = path.call(print, "body", 2); - paramsDoc = path.call(print, "body", 3); - bodystmtDoc = path.call(print, "body", 4); - } - // In case there are no parens but there are arguments - const parens = paramsNode.type === "params" && paramsNode.body.some((type) => type); - declaration.push(nameDoc, parens ? "(" : "", paramsDoc, parens ? ")" : ""); - if ((0, utils_1.isEmptyBodyStmt)(bodystmtNode)) { + // In case there are no parens but there are parameters + const useParens = node.params.type === "params" && !(0, utils_1.isEmptyParams)(node.params); + declaration.push(path.call(print, "name"), useParens ? "(" : "", path.call(print, "params"), useParens ? ")" : ""); + if ((0, utils_1.isEmptyBodyStmt)(node.bodystmt)) { return group([...declaration, "; end"]); } return group([ group(declaration), - indent([hardline, bodystmtDoc]), + indent([hardline, path.call(print, "bodystmt")]), hardline, "end" ]); }; exports.printDef = printDef; -const printSingleLineMethod = (path, opts, print) => { - const parensNode = path.getValue().body[1]; +const printDefEndless = (path, opts, print) => { + const node = path.getValue(); let paramsDoc = ""; - if (parensNode) { - const paramsNode = parensNode.body[0]; - if (paramsNode.body.some((type) => type)) { - paramsDoc = path.call(print, "body", 1); - } + // If we have any kind of parameters, we're going to print the whole + // parentheses. If we don't, then we're just going to skip them entirely. + if (node.paren && !(0, utils_1.isEmptyParams)(node.paren.cnts)) { + paramsDoc = path.call(print, "paren"); } return group([ "def ", - path.call(print, "body", 0), + path.call(print, "name"), paramsDoc, " =", - indent(group([line, path.call(print, "body", 2)])) + indent(group([line, path.call(print, "stmt")])) ]); }; -exports.printSingleLineMethod = printSingleLineMethod; -const printAccessControl = (path, opts, print) => { - return path.call(print, "body", 0); -}; +exports.printDefEndless = printDefEndless; +const printAccessControl = (path, opts, print) => path.call(print, "value"); exports.printAccessControl = printAccessControl;