Options
All
  • Public
  • Public/Protected
  • All
Menu

Index

Type aliases

ComputedDoc

Doc

Doc: undefined | string | ComputedDoc

Variables

breakParent

breakParent: Doc

Include this anywhere to force all parent groups to break. See group for more info. Example:

group(
  concat([
    " ",
    expr,
    " ",
    breakParent
  ])
)

cursor

cursor: Doc

This is a placeholder value where the cursor is in the original input in order to find where it would be printed.

hardline

hardline: Doc

Line break that is always behaves as if it did not fit, regardless if the group fits on one line or not.

line

line: Doc

Line break. If a group fits on one line it prints a single space. If group does not fit it prints line break and an indentetation.

lineSuffixBoundary

lineSuffixBoundary: Doc

In cases where you embed code inside of templates, comments shouldn't be able to leave the code part. lineSuffixBoundary is an explicit marker you can use to flush code in addition to newlines.

concat(["{", lineSuffix(" // comment"), lineSuffixBoundary, "}", hardline])

will output

{ // comment
}

and not

{} // comment

literalline

literalline: Doc

Line break that is always behaves as if it did not fit. Unlike other lines though it does not print indetantion after line break. Prettier uses this for template literals.

softline

softline: Doc

Line break. If a group fits on one line it prints nothing. If group does not fit it behaves same as line.

Functions

addAlignmentToDoc

  • addAlignmentToDoc(doc: Doc, size: number, tabWidth: number): Doc

align

  • align(n: number, contents: Doc): Doc
  • This is similar to indent but it increases the level of indentation for the given content by a given n fixed number. When using tabs, it's going to print spaces. You should prefer using indent whenever possible.

    align(tabWidth, concat(["hello", line, "world"]))
    

    Parameters

    • n: number
    • contents: Doc

    Returns Doc

concat

  • concat(parts: Array<Doc>): Doc
  • Combines an array of parts into a single one.

    concat(["hello", "world"])
    

    Parameters

    • parts: Array<Doc>

    Returns Doc

conditionalGroup

  • This should be used as last resort as it triggers an exponential complexity when nested. This will try to print the first option, if it fits use it, otherwise try next one and so on.

    Parameters

    Returns Doc

fill

  • fill(parts: Array<Doc>): Doc
  • This is an alternative type of group which behave like text layout: it's going to add a break whenever the next element doesn't fit in the line anymore. The difference with a typical group is that it's not going to break all the separators, just the ones that are at the end of lines.

    fill(["I", line, "love", line, "prettier"])
    

    Parameters

    • parts: Array<Doc>

    Returns Doc

group

  • Mark a group of fragments which the printer should try to print on one line. This tell the printer formatter when to break. Groups are usually nested, and the printer will try to fit everything on one line, but if it doesn't fit it will break the outermost group first and try again. It will continue breaking groups until everything fits (or there are no more groups to break).

    A document can force parent groups to break by including breakParent. A "hard" and "literal" line automatically include this so they always break parent groups. Breaks are propagated to all parent groups, so if a deeply nested expression has a hard break, everything with break. This only matters for "hard" breaks, i.e. newlines that are printed no matter what and can be statically analyzed.

    For example, an array will try to fit on one line:

    [1, "foo", { bar: 2 }]
    

    However, if any of the items inside the array have a hard break, the array will always break as well:

    [
      1,
      function() {
        return 2
      },
      3
    ]
    

    In prettier functions always break after the opening curly brace no matter what, so the array breaks as well for consistent formatting. See the prettier implementation of ArrayExpression for an example.

    group(
      concat([
        "[",
        indent(
          concat([
            line,
            join(
              concat([",", line]),
              path.map(print, "elements")
            )
          ])
        ),
        line,
        "]"
      ])
    )
    

    Parameters

    Returns Doc

ifBreak

  • ifBreak(breakContents: Doc, flatContents: Doc): Doc
  • Prints breakContents if the current group breaks and prints flatContents if it doesn't.

    ifBreak(";", " ")
    

    Parameters

    • breakContents: Doc
    • flatContents: Doc

    Returns Doc

indent

  • Increase the level of indentation by given n number for the given contents.

    indent("Hello")
    indent(concat(["hello", indent("world")]))
    

    Parameters

    • contents: Doc

    Returns Doc

join

  • join(seperator: Doc, parts: Array<Doc>): Doc

lineSuffix

  • lineSuffix(contents: Doc): Doc
  • This is used to implement trailing comments. In practice, it is not practical to find where the line ends and you don't want to accidentally print some code at the end of the comment. lineSuffix will buffer the output and flush it before any new line or lineSuffixBoundary.

    concat(["a", lineSuffix(" // comment"), ";", hardline])
    

    will output

    a; // comment
    

    Parameters

    • contents: Doc

    Returns Doc

Legend

  • Module
  • Object literal
  • Variable
  • Function
  • Function with type parameter
  • Index signature
  • Type alias
  • Enumeration
  • Enumeration member
  • Property
  • Method
  • Interface
  • Interface with type parameter
  • Constructor
  • Property
  • Method
  • Index signature
  • Class
  • Class with type parameter
  • Constructor
  • Property
  • Method
  • Accessor
  • Index signature
  • Inherited constructor
  • Inherited property
  • Inherited method
  • Inherited accessor
  • Protected property
  • Protected method
  • Protected accessor
  • Private property
  • Private method
  • Private accessor
  • Static property
  • Static method

Generated using TypeDoc