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

4 Pretty-Printing🔗ℹ

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

 (require javascript/print) package: javascript

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

(require pprint)

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

4.1 Formatting Terms🔗ℹ

procedure

(format-term term)  doc?

  term : Term/X?
Formats any JavaScript term as a doc for pretty-printing.

procedure

(format-source-element element)  doc?

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

procedure

(format-variable-initializer init)  doc?

  init : VariableInitializer?
Formats a variable initializer as a doc for pretty-printing.

procedure

(format-declaration decl)  doc?

  decl : Declaration?
Formats a declaration as a doc for pretty-printing.

procedure

(format-expression expr)  doc?

  expr : Expression/X?
Formats an expression as a doc for pretty-printing.

procedure

(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.

procedure

(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.

procedure

(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.

procedure

(format-substatement stmt)  doc?

  stmt : SubStatement/X?
Formats a statement that occurs as a substatement as a doc for pretty-printing.

procedure

(format-case-clause clause)  doc?

  clause : CaseClause?
Formats a case clause as a doc for pretty-printing.

procedure

(format-property property)  doc?

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

procedure

(format-identifier id)  doc?

  id : Identifier?
Formats an identifier as a doc for pretty-printing.

4.2 Configuration Parameters🔗ℹ

The number of spaces to indent.
(Currently ignored.)
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.

value

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

value

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

value

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

value

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

procedure

(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.