Sha256: 903bd494d5f3826cc9c088a70d3d4ffffc7cad0bd616590f3a580dd5fcc2f652

Contents?: true

Size: 1.63 KB

Versions: 15

Compression:

Stored size: 1.63 KB

Contents

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

module.exports = {
  class: (path, opts, print) => {
    const [_constant, superclass, statements] = path.getValue().body;

    const parts = ["class ", path.call(print, "body", 0)];
    if (superclass) {
      parts.push(" < ", path.call(print, "body", 1));
    }

    // If the body is empty, we can replace with a ;
    const stmts = statements.body[0].body;
    if (stmts.length === 1 && stmts[0].type === "void_stmt") {
      return group(concat([concat(parts), ifBreak(line, "; "), "end"]));
    }

    return group(
      concat([
        concat(parts),
        indent(concat([hardline, path.call(print, "body", 2)])),
        concat([hardline, "end"])
      ])
    );
  },
  class_name_error: (_path, _opts, _print) => {
    throw new Error("class/module name must be CONSTANT");
  },
  module: (path, opts, print) => {
    const declaration = group(concat(["module ", path.call(print, "body", 0)]));

    // If the body is empty, we can replace with a ;
    const stmts = path.getValue().body[1].body[0].body;
    if (stmts.length === 1 && stmts[0].type === "void_stmt") {
      return group(concat([declaration, ifBreak(line, "; "), "end"]));
    }

    return group(
      concat([
        declaration,
        indent(concat([hardline, path.call(print, "body", 1)])),
        concat([hardline, "end"])
      ])
    );
  },
  sclass: (path, opts, print) =>
    group(
      concat([
        concat(["class << ", path.call(print, "body", 0)]),
        indent(concat([hardline, path.call(print, "body", 1)])),
        concat([hardline, "end"])
      ])
    )
};

Version data entries

15 entries across 15 versions & 1 rubygems

Version Path
prettier-0.21.0 src/nodes/scopes.js
prettier-0.20.1 src/nodes/scopes.js
prettier-0.20.0 src/nodes/scopes.js
prettier-0.19.1 src/nodes/scopes.js
prettier-0.19.0 src/nodes/scopes.js
prettier-0.18.2 src/nodes/scopes.js
prettier-0.18.1 src/nodes/scopes.js
prettier-0.18.0 src/nodes/scopes.js
prettier-0.17.0 src/nodes/scopes.js
prettier-0.16.0 src/nodes/scopes.js
prettier-0.15.1 src/nodes/scopes.js
prettier-0.15.0 src/nodes/scopes.js
prettier-0.14.0 src/nodes/scopes.js
prettier-0.13.0 src/nodes/scopes.js
prettier-0.12.3 src/nodes/scopes.js