Sha256: 742daa3b78b31bd7c8baaed5cb073b0eabc4c3acebc10f02fa2536025daf6988

Contents?: true

Size: 1.73 KB

Versions: 13

Compression:

Stored size: 1.73 KB

Contents

const { concat, join } = require("../../prettier");
const { literal } = require("../../utils");

const nodeDive = (node, steps) => {
  let current = node;

  steps.forEach((step) => {
    current = current[step];
  });

  return current;
};

const unskippableParens = [
  "if_mod",
  "rescue_mod",
  "unless_mod",
  "until_mod",
  "while_mod"
];

const 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 concat([
      `${keyword} `,
      path.call.apply(path, [print].concat(steps).concat("body", 0))
    ]);
  }

  return concat([keyword, path.call.apply(path, [print].concat(steps))]);
};

module.exports = {
  break: (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) ||
      concat(["break ", join(", ", path.call(print, "body", 0))])
    );
  },
  next: (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) ||
      concat(["next ", join(", ", path.call(print, "body", 0))])
    );
  },
  yield: (path, opts, print) => {
    if (path.getValue().body[0].type === "paren") {
      return concat(["yield", path.call(print, "body", 0)]);
    }

    return concat(["yield ", join(", ", path.call(print, "body", 0))]);
  },
  yield0: literal("yield")
};

Version data entries

13 entries across 13 versions & 1 rubygems

Version Path
prettier-1.6.1 src/ruby/nodes/flow.js
prettier-1.6.0 src/ruby/nodes/flow.js
prettier-1.5.5 src/ruby/nodes/flow.js
prettier-1.5.4 src/ruby/nodes/flow.js
prettier-1.5.3 src/ruby/nodes/flow.js
prettier-1.5.2 src/ruby/nodes/flow.js
prettier-1.5.1 src/ruby/nodes/flow.js
prettier-1.5.0 src/ruby/nodes/flow.js
prettier-1.4.0 src/ruby/nodes/flow.js
prettier-1.3.0 src/ruby/nodes/flow.js
prettier-1.2.5 src/ruby/nodes/flow.js
prettier-1.2.4 src/ruby/nodes/flow.js
prettier-1.2.3 src/ruby/nodes/flow.js