Skip to content
Tooling

Formatter#

Text
velt fmt [<file|dir>...] [--check]
velt fmt (--workspace | -p <glob>... | --affected [<base>]) [--fail-fast] [--check]

velt fmt formats Velt sources in place, with one style and no options, like gofmt. Without paths it formats the package's package.vlt and the .vlt, .ts and .tsx files of its src/, or every .vlt file under the current directory outside a package (a TypeScript project's files are left alone). A directory given as a path contributes its .vlt files, and the .ts and .tsx files (not .d.ts) under a package's src/ and tests/; elsewhere they may belong to a TypeScript project, so name them as files to format them. target/, node_modules/ and hidden directories are skipped.

  • --check writes nothing, lists the files that are not formatted, and exits with 1 if there are any. Use it in CI.

  • A file that does not parse is reported and left alone (exit code 1).

  • The style is close to Prettier's: 2-space indentation, a 100-column line width, double quotes (single quotes are kept when the text contains "), semicolons, trailing commas in lists that break over several lines, at most one blank line between statements. Template literals are printed verbatim. Formatting is idempotent and never changes the program.

  • A method chain that does not fit puts each .name(...) on its own line; an index or ! stays on the line of the call before it (.split(" ")[0], .find(f)!). Unlike Prettier, this holds for a computed index too (.split(" ")[i]): a line starting with [ continues the line before it, which the parser warns about.

    ts
    const word = line
      .trim()
      .toUpperCase()
      .split(" ")[i]
      .toLowerCase()
      .padStart(10, "x");
  • In a workspace, velt fmt at the root formats every member; --workspace, -p and --affected select members anywhere.

  • JSX is laid out like Prettier does it: an element that does not fit goes over several lines (in parentheses after =, return and =>), text is reflowed as a paragraph, and in an HTML element or a fragment a space next to a tag at a line break is written {" "} (it would otherwise be lost). A conditional with an element in it puts each element branch in parentheses when it breaks. Unlike Prettier, a component's children are kept as written (it receives them as its children prop, so Save and Save{" "} differ: spaces next to its tags stay on their line), runs of several spaces in text are kept, branches other than elements are not given parentheses they did not have, and <fbt> gets no special layout.

  • Comments stay next to the code they were written beside: a comment above a statement, member or field stays above it, and one at the end of a line stays at the end of that line. An object type with a comment inside is printed one field per line, like an interface body.

  • The language server formats documents on request (Editors).

Edit this page on GitHub