Sha256: 135a46be230349b8921dd46bbeef8640ad53ba40cdb5f9aafe7c699e907ec8cf

Contents?: true

Size: 1.68 KB

Versions: 1

Compression:

Stored size: 1.68 KB

Contents

const {
  concat,
  group,
  ifBreak,
  indent,
  line,
  join,
  softline
} = require("../prettier");
const { literal } = require("../utils");

const printReturn = (path, opts, print) => {
  let args = path.getValue().body[0].body[0];
  let steps = ["body", 0, "body", 0];

  if (!args) {
    return "return";
  }

  // If the body of the return contains parens, then just skip directly to the
  // content of the parens so that we can skip printing parens if we don't
  // want them.
  if (args.body[0] && args.body[0].type === "paren") {
    args = args.body[0].body[0];
    steps = steps.concat("body", 0, "body", 0);
  }

  // If we're returning an array literal that isn't a special array, then we
  // want to grab the arguments so that we can print them out as if they were
  // normal return arguments.
  if (
    args.body[0] &&
    args.body[0].type === "array" &&
    ["args", "args_add_star"].includes(args.body[0].body[0].type)
  ) {
    steps = steps.concat("body", 0, "body", 0);
  }

  // Now that we've established which actual node is the arguments to return,
  // we grab it out of the path by diving down the steps that we've set up.
  const parts = path.call.apply(path, [print].concat(steps));

  // If we got the value straight out of the parens, then `parts` would only
  // be a singular doc as opposed to an array.
  const value = Array.isArray(parts) ? join(concat([",", line]), parts) : parts;

  return group(
    concat([
      "return",
      ifBreak(parts.length > 1 ? " [" : "(", " "),
      indent(concat([softline, value])),
      concat([softline, ifBreak(parts.length > 1 ? "]" : ")", "")])
    ])
  );
};

module.exports = {
  return: printReturn,
  return0: literal("return")
};

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
prettier-0.16.0 src/nodes/return.js