Sha256: d6534ac36114b4292572ddc77ebfd5a5c53d04370eb000e6221cd0e3835b9099

Contents?: true

Size: 1.24 KB

Versions: 8

Compression:

Stored size: 1.24 KB

Contents

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

// The `BEGIN` and `END` keywords are used to hook into the Ruby process. Any
// `BEGIN` blocks are executed right when the process starts up, and the `END`
// blocks are executed right before exiting.
//
//     BEGIN {
//       # content goes here
//     }
//
//     END {
//       # content goes here
//     }
//
// Interesting side note, you don't use `do...end` blocks with these hooks. Both
// nodes contain one child which is a `stmts` node.
function printHook(name) {
  return function printHookWithName(path, opts, print) {
    const stmtsNode = path.getValue().body[1];
    const printedStmts = path.call(print, "body", 1);

    const parts = [
      name,
      " ",
      path.call(print, "body", 0),
      indent(concat([line, printedStmts])),
      concat([line, "}"])
    ];

    // If there are no statements but there are comments, then we want to skip
    // printing the newline so that we don't end up with multiple spaces.
    if (isEmptyStmts(stmtsNode) && stmtsNode.comments) {
      parts[1] = indent(printedStmts);
    }

    return group(concat(parts));
  };
}

module.exports = {
  BEGIN: printHook("BEGIN"),
  END: printHook("END")
};

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
prettier-1.2.2 src/nodes/hooks.js
prettier-1.2.1 src/nodes/hooks.js
prettier-1.2.0 src/nodes/hooks.js
prettier-1.1.0 src/nodes/hooks.js
prettier-1.0.1 src/nodes/hooks.js
prettier-1.0.0 src/nodes/hooks.js
prettier-1.0.0.pre.rc2 src/nodes/hooks.js
prettier-1.0.0.pre.rc1 src/nodes/hooks.js