Sha256: 01a3cb08d495d4ec6e653112a89d5dfa834542d9fa0c84b1c7c7a671c95b6395

Contents?: true

Size: 1.73 KB

Versions: 13

Compression:

Stored size: 1.73 KB

Contents

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

function printCase(path, opts, print) {
  const statement = ["case"];

  // You don't need to explicitly have something to test against in a case
  // statement (without it it effectively becomes an if/elsif chain).
  if (path.getValue().body[0]) {
    statement.push(" ", path.call(print, "body", 0));
  }

  return concat(
    statement.concat([hardline, path.call(print, "body", 1), hardline, "end"])
  );
}

function printWhen(path, opts, print) {
  const [_preds, _stmts, addition] = path.getValue().body;

  // The `fill` builder command expects an array of docs alternating with
  // line breaks. This is so it can loop through and determine where to break.
  const preds = fill(
    path.call(print, "body", 0).reduce((accum, pred, index) => {
      if (index === 0) {
        return [pred];
      }

      // Pull off the last element and make it concat with a comma so that
      // we can maintain alternating lines and docs.
      return accum
        .slice(0, -1)
        .concat([concat([accum[accum.length - 1], ","]), line, pred]);
    }, null)
  );

  const stmts = path.call(print, "body", 1);
  const parts = [concat(["when ", align("when ".length, preds)])];

  // It's possible in a when to just have empty void statements, in which case
  // we would skip adding the body.
  if (!stmts.parts.every((part) => !part)) {
    parts.push(indent(concat([hardline, stmts])));
  }

  // This is the next clause on the case statement, either another `when` or
  // an `else` clause.
  if (addition) {
    parts.push(hardline, path.call(print, "body", 2));
  }

  return group(concat(parts));
}

module.exports = {
  case: printCase,
  when: printWhen
};

Version data entries

13 entries across 13 versions & 1 rubygems

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