How to use the prettier.doc function in prettier

To help you get started, we’ve selected a few prettier examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github trivago / prettier-plugin-twig-melody / src / print / BlockStatement.js View on Github external
const prettier = require("prettier");
const { concat, hardline, group } = prettier.doc.builders;
const { Node } = require("melody-types");
const { EXPRESSION_NEEDED, printChildBlock } = require("../util");

const p = (node, path, print) => {
    node[EXPRESSION_NEEDED] = false;
    const hasChildren = Array.isArray(node.body);

    if (hasChildren) {
        const blockName = path.call(print, "name");
        const opener = concat([
            node.trimLeft ? "{%-" : "{%",
            " block ",
            blockName,
            node.trimRightBlock ? " -%}" : " %}"
        ]);
        const parts = [opener];
github trivago / prettier-plugin-twig-melody / src / print / ConditionalExpression.js View on Github external
const prettier = require("prettier");
const { concat, line, indent, group } = prettier.doc.builders;
const {
    EXPRESSION_NEEDED,
    STRING_NEEDS_QUOTES,
    wrapExpressionIfNeeded
} = require("../util");

const p = (node, path, print) => {
    node[EXPRESSION_NEEDED] = false;
    node[STRING_NEEDS_QUOTES] = true;

    const rest = [line, "?"];
    if (node.consequent) {
        rest.push(concat([" ", path.call(print, "consequent")]));
    }
    if (node.alternate) {
        rest.push(line, ": ", path.call(print, "alternate"));
github trivago / prettier-plugin-twig-melody / src / print / UnaryExpression.js View on Github external
const prettier = require("prettier");
const { concat, group } = prettier.doc.builders;
const {
    EXPRESSION_NEEDED,
    STRING_NEEDS_QUOTES,
    wrapExpressionIfNeeded
} = require("../util");

const p = (node, path, print) => {
    node[EXPRESSION_NEEDED] = false;
    node[STRING_NEEDS_QUOTES] = true;
    const parts = [node.operator, path.call(print, "argument")];
    wrapExpressionIfNeeded(path, parts, node);
    return group(concat(parts));
};

module.exports = {
    printUnaryExpression: p
github trivago / prettier-plugin-twig-melody / src / print / Attribute.js View on Github external
const prettier = require("prettier");
const { concat } = prettier.doc.builders;
const { EXPRESSION_NEEDED, STRING_NEEDS_QUOTES } = require("../util");
const { Node } = require("melody-types");

const mayCorrectWhitespace = attrName =>
    ["id", "class", "type"].indexOf(attrName) > -1;

const sanitizeWhitespace = s => s.replace(/\s+/g, " ").trim();

const printConcatenatedString = (valueNode, path, print, ...initialPath) => {
    const printedFragments = [];
    let currentNode = valueNode;
    const currentPath = initialPath;
    while (Node.isBinaryConcatExpression(currentNode)) {
        printedFragments.unshift(path.call(print, ...currentPath, "right"));
        currentPath.push("left");
        currentNode = currentNode.left;
github trivago / prettier-plugin-twig-melody / src / print / MacroDeclarationStatement.js View on Github external
const prettier = require("prettier");
const {
    group,
    join,
    concat,
    line,
    softline,
    hardline,
    indent
} = prettier.doc.builders;

const printOpener = (node, path, print) => {
    const parts = [
        node.trimLeft ? "{%-" : "{%",
        " macro ",
        path.call(print, "name"),
        "("
    ];
    const mappedArguments = path.map(print, "arguments");
    const joinedArguments = join(concat([",", line]), mappedArguments);
    parts.push(indent(concat([softline, joinedArguments])));
    parts.push(")", line, node.trimRightMacro ? "-%}" : "%}");
    return group(concat(parts));
};

const p = (node, path, print) => {
github prettier / plugin-ruby / src / nodes / defined.js View on Github external
const { concat, group, indent, softline } = require("prettier").doc.builders;

const defined = (path, options, print) => group(concat([
  "defined?(",
  indent(concat([softline, path.call(print, "body", 0)])),
  concat([softline, ")"])
]));

module.exports = {
  defined
};
github jhipster / prettier-java / packages / prettier-plugin-java / src / printers / printer-utils.js View on Github external
"use strict";
const _ = require("lodash");
const { join, concat, group } = require("./prettier-builder");
const { printTokenWithComments } = require("./comments");
const { indent, hardline } = require("prettier").doc.builders;

const orderedModifiers = [
  "Public",
  "Protected",
  "Private",
  "Abstract",
  "Default",
  "Static",
  "Final",
  "Transient",
  "Volatile",
  "Synchronized",
  "Native",
  "Strictfp"
];
github prettier / plugin-php / src / index.js View on Github external
"use strict";

const parse = require("./parser");
const print = require("./printer");
const clean = require("./clean");
const options = require("./options");
const comments = require("./comments");
const { join, hardline } = require("prettier").doc.builders;
const { hasPragma, insertPragma } = require("./pragma");

function createLanguage(linguistData, { extend, override }) {
  const language = {};

  for (const key in linguistData) {
    const newKey = key === "languageId" ? "linguistLanguageId" : key;
    language[newKey] = linguistData[key];
  }

  if (extend) {
    for (const key in extend) {
      language[key] = (language[key] || []).concat(extend[key]);
    }
  }
github benjie / prettier-plugin-pg / src / print.js View on Github external
if (text.indexOf(escape) < 0) {
      return escape;
    }
  }
  throw new Error("Could not find an acceptable function escape sequence");
}

const {
  concat,
  join,
  hardline,
  line,
  softline,
  group,
  indent,
} = require("prettier").doc.builders;

const commaLine = concat([",", line]);

function whitelistKeys(obj, keys) {
  const extraKey = Object.keys(obj).find(key => keys.indexOf(key) < 0);
  if (extraKey) {
    throw new Error(
      `Do not understand key '${extraKey}' in ${JSON.stringify(obj)}`
    );
  }
}

function isContextNode(node) {
  const allKeys = Object.keys(node);
  if (allKeys.length === 1) {
    if (allKeys[0].substr(-4) === "Stmt") {
github prettier / plugin-python / src / printer / index.js View on Github external
"use strict";

const util = require("../_util-from-prettier");
const tokens = require("./tokens");

const docBuilders = require("prettier").doc.builders;

const concat = docBuilders.concat;
const join = docBuilders.join;
const hardline = docBuilders.hardline;
const line = docBuilders.line;
const softline = docBuilders.softline;
const literalline = docBuilders.literalline;
const group = docBuilders.group;
const indent = docBuilders.indent;
const ifBreak = docBuilders.ifBreak;

const escapedLine = concat([ifBreak(" \\"), line]);
const escapedSoftline = concat([ifBreak(" \\"), softline]);

function indentConcat(docs) {
  return indent(concat(docs));