GithubHelp home page GithubHelp logo

isabella232 / prettier-printer Goto Github PK

View Code? Open in Web Editor NEW

This project forked from prettier/prettier-printer

0.0 0.0 0.0 241 KB

Library for building and pretty printing text documents

Home Page: https://prettier.github.io/prettier-printer/

License: MIT License

JavaScript 59.50% TypeScript 40.50%

prettier-printer's Introduction

prettier-printer

This is pretty printer library implemented as described by Wadler in "A prettier printer" paper. The library is based on a single way to concatenate documents, which is associative and has a left and right unit.

Overview

This is very rough documentation of the formatting commands you can use to build a printed version of something. This will be improved over time.

The printer should use the basic formatting abstractions provided to construct a document. Parts of the API only exist to be compatible with recast's previous API to ease migration, but over time we can clean it up.

The following commands are available:

concat

Combine an array into a single string.

group

Mark a group of items which the printer should try to fit on one line. This is the basic command to tell the printer 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 (see below). 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
]

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

conditionalGroup

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

conditionalGroup([a, b, c])

fill

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"])

ifBreak

Prints something if the current group breaks and something else if it doesn't.

ifBreak(";", " ")

breakParent

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

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

join

Join an array of items with a separator.

line

Specify a line break. If an expression fits on one line, the line break will be replaced with a space. Line breaks always indent the next line with the current level of indentation.

softline

Specify a line break. The difference from line is that if the expression fits on one line, it will be replaced with nothing.

hardline

Specify a line break that is always included in the output, no matter if the expression fits on one line or not.

literalline

Specify a line break that is always included in the output, and don't indent the next line. This is used for template literals.

lineSuffix

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.

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

will output

a; // comment

lineSuffixBoundary

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

indent

Increase the level of indentation.

align

This is similar to indent but it increases the level of indentation by a fixed number. When using tabs, it's going to print spaces. You should prefer using indent whenever possible.

cursor

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

Example

For an example, here's the implementation of the ArrayExpression node type:

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

This is a group with opening and closing brackets, and possibly indented contents. Because it's a group it will always be broken up if any of the sub-expressions are broken.

prettier-printer's People

Contributors

gozala avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.