Sha256: 14d1ba29b1bf31637209a18aee0458579d83c0b67cacedcf96f24e01dd20f358

Contents?: true

Size: 1.52 KB

Versions: 6

Compression:

Stored size: 1.52 KB

Contents

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

const findKeywordIndices = (children, keywords) => {
  const indices = [];

  children.forEach((child, index) => {
    if (child.type !== "silent_script") {
      return;
    }

    if (keywords.includes(child.value.keyword)) {
      indices.push(index);
    }
  });

  return indices;
};

// http://haml.info/docs/yardoc/file.REFERENCE.html#running-ruby--
const silentScript = (path, opts, print) => {
  const { children, value } = path.getValue();
  const parts = [`- ${value.text.trim()}`];

  if (children.length > 0) {
    const scripts = path.map(print, "children");

    if (value.keyword === "case") {
      const keywordIndices = findKeywordIndices(children, ["when", "else"]);

      parts.push(
        concat(
          scripts.map((script, index) => {
            const concated = concat([hardline, script]);

            return keywordIndices.includes(index) ? concated : indent(concated);
          })
        )
      );
    } else if (["if", "unless"].includes(value.keyword)) {
      const keywordIndices = findKeywordIndices(children, ["elsif", "else"]);

      parts.push(
        concat(
          scripts.map((script, index) => {
            const concated = concat([hardline, script]);

            return keywordIndices.includes(index) ? concated : indent(concated);
          })
        )
      );
    } else {
      parts.push(indent(concat([hardline, join(hardline, scripts)])));
    }
  }

  return group(concat(parts));
};

module.exports = silentScript;

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
prettier-0.20.0 src/haml/nodes/silentScript.js
prettier-0.19.1 src/haml/nodes/silentScript.js
prettier-0.19.0 src/haml/nodes/silentScript.js
prettier-0.18.2 src/haml/nodes/silentScript.js
prettier-0.18.1 src/haml/nodes/silentScript.js
prettier-0.18.0 src/haml/nodes/silentScript.js