On this page:
4.1 Formatting Terms
format-term
format-source-element
format-variable-initializer
format-declaration
format-expression
format-subexpression
format-statement
format-nested-substatement
format-substatement
format-case-clause
format-property
format-identifier
4.2 Configuration Parameters
current-indentation-width
collapse-lines?
collapse-simple-substatements?
4.3 Extending the Pretty-Printer
formatters/ Expression
formatters/ Statement
formatters/ Expression List
formatters/ Statement List
format-map
Version: 4.1.3.3

4 Pretty-Printing

This library provides facilities for pretty-printing JavaScript source. It can be required via:

 (require (planet dherman/javascript:9:0/print))

This library depends on the pprint PLaneT package, which can be required via:

  (require (planet dherman/pprint:4))

See the documentation for pprint for information on how to use it.

4.1 Formatting Terms

(format-term term)  doc?
  term : Term/X?

Formats any JavaScript term as a doc for pretty-printing.

(format-source-element element)  doc?
  element : SourceElement?

Formats a JavaScript source element as a doc for pretty-printing.

(format-variable-initializer init)  doc?
  init : VariableInitializer?

Formats a variable initializer as a doc for pretty-printing.

(format-declaration decl)  doc?
  decl : Declaration?

Formats a declaration as a doc for pretty-printing.

(format-expression expr)  doc?
  expr : Expression/X?

Formats an expression as a doc for pretty-printing.

(format-subexpression expr parent)  doc?
  expr : Expression?
  parent : Expression?

Formats an expression expr that occurs as an immediate subexpression of parent as a doc for pretty-printing. The parenthesization is determined based on the precedence of the two expressions.

(format-statement stmt)  doc?
  stmt : Statement/X?

Formats a statement as a doc for pretty-printing. Post-conditions: The statement output includes its own semicolon if appropriate, and statement output is not newline-terminated.

(format-nested-substatement stmt)  doc?
  stmt : SubStatement/X?

Formats a statement that occurs as an indented substatement as a doc for pretty-printing. Post-condition: The indentation level is returned to its previous level after the substatement.

(format-substatement stmt)  doc?
  stmt : SubStatement/X?

Formats a statement that occurs as a substatement as a doc for pretty-printing.

(format-case-clause clause)  doc?
  clause : CaseClause?

Formats a case clause as a doc for pretty-printing.

(format-property property)  doc?
  property : Property?

Formats an object property as a doc for pretty-printing.

(format-identifier id)  doc?
  id : Identifier?

Formats an identifier as a doc for pretty-printing.

4.2 Configuration Parameters

current-indentation-width : (parameter/c natural-number/c)

The number of spaces to indent.

collapse-lines? : (parameter/c boolean?)

(Currently ignored.)

collapse-simple-substatements? : (parameter/c boolean?)

Non-block substatements stay on same line?

4.3 Extending the Pretty-Printer

As described in Extending the Language, it is possible to extend the language with custom forms. The pretty-printer can be extended with extra matchers that recognize just their form types and produce pretty-printed docs. Matchers should simple fail to match (raising the same error as (match) would for anything other than the forms they recognize.

formatters/Expression : (parameter/c (listof (any -> doc?)))
formatters/Statement : (parameter/c (listof (any -> doc?)))
formatters/ExpressionList : (parameter/c (listof (any -> doc?)))
formatters/StatementList : (parameter/c (listof (any -> doc?)))

(format-map f elts param)  c
  f : (a -> c)
  elts : (or (listof a) b)
  param : (parameter/c (b -> c))

Applies a formatter to an extensible list of terms (such as an ExpressionList/X or StatementList/X). If the formatter fails to match, the current value of the param parameter is used to format the elements instead.