Sha256: e1cc90d2ea1bb6885c5bd346d35fa34e7f3a2549008098b5bcf80bba481a22c4

Contents?: true

Size: 1.71 KB

Versions: 1

Compression:

Stored size: 1.71 KB

Contents

const { concat, group, hardline, indent, line } = require("../prettier");
const { first } = require("../utils");

function printMethod(offset) {
  return function printMethodWithOffset(path, opts, print) {
    const [_name, params, body] = path.getValue().body.slice(offset);
    const declaration = ["def "];

    // In this case, we're printing a method that's defined as a singleton, so
    // we need to include the target and the operator
    if (offset > 0) {
      declaration.push(
        path.call(print, "body", 0),
        path.call(print, "body", 1)
      );
    }

    // In case there are no parens but there are arguments
    const parens =
      params.type === "params" && params.body.some((paramType) => paramType);

    declaration.push(
      path.call(print, "body", offset),
      parens ? "(" : "",
      path.call(print, "body", offset + 1),
      parens ? ")" : ""
    );

    // If the body is empty, we can replace with a ;
    const stmts = body.body[0].body;

    if (
      !body.body.slice(1).some((node) => node) &&
      stmts.length === 1 &&
      stmts[0].type === "void_stmt" &&
      !stmts[0].comments
    ) {
      return group(concat(declaration.concat(["; end"])));
    }

    return group(
      concat([
        group(concat(declaration)),
        indent(concat([hardline, path.call(print, "body", offset + 2)])),
        group(concat([hardline, "end"]))
      ])
    );
  };
}

function printSingleLineMethod(path, opts, print) {
  const [nameDoc, stmtDoc] = path.map(print, "body");

  return group(
    concat(["def ", nameDoc, " =", indent(group(concat([line, stmtDoc])))])
  );
}

module.exports = {
  access_ctrl: first,
  def: printMethod(0),
  defs: printMethod(2),
  defsl: printSingleLineMethod
};

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
prettier-1.2.0 src/nodes/methods.js