npm.io
3.0.2 • Published 1 week ago

prettier-plugin-toml

Licence
MIT
Version
3.0.2
Deps
3
Size
73 kB
Vulns
0
Weekly
0
Stars
356

prettier-plugin-toml npm bundle size npm bundle size

An opinionated toml formatter plugin for Prettier

Prettier is an opinionated code formatter. It enforces a consistent style by parsing your code and re-printing, taking various rules into account.

This plugin adds support for toml through tombi.

Notice

This plugin is still under development, its printer just wraps tombi's default printer. Of course it should just work, but may not match prettier's format sometimes.

Requirements

prettier-plugin-toml is an evergreen module. This module requires an LTS Node version (v18.0.0+).

Install

Using npm:

# npm
npm i -D prettier prettier-plugin-toml

# yarn
yarn add -D prettier prettier-plugin-toml

Usage

Once installed, Prettier plugins must be added to .prettierrc:

{
  "plugins": ["prettier-plugin-toml"]
}

Then:

# npx
npx prettier --write foo.toml

# yarn
yarn prettier --write foo.toml

Configuration

Besides the options below, this plugin reads tombi's own configuration, following its search priority:

  1. Project — for each directory from the formatted file's directory up to the filesystem root: .tombi.toml, tombi.toml, .config/tombi.toml, then [tool.tombi] in pyproject.toml.
  2. User — $XDG_CONFIG_HOME/tombi/config.toml, ~/.config/tombi/config.toml, plus the platform specific ~/Library/Application Support/tombi/config.toml (macOS) or %APPDATA%\tombi\config.toml (Windows).
  3. System — /etc/tombi/config.toml.

The rules are resolved in order, each step overriding the previous one:

  1. Tombi's and Prettier's defaults.
  2. The discovered configuration's [format.rules].
  3. Prettier options that are explicitly set — a value equal to Prettier's default (for example printWidth: 80) does not count as explicit.
  4. The discovered configuration's per-file [[overrides]], applied by Tombi last.
Notes
  • Tombi's schema lookup is always disabled, so formatting stays offline and deterministic.
  • Discovered configuration files are cached per directory and polled (best effort), so edits to them apply without restarting the process. A newly created project configuration applies on the next run.

Parser Options

prettier's own core options are inherited and mapped to their tombi counterparts where they exist:

  • printWidth → line-width
  • tabWidth → indent-width
  • useTabs → indent-style
  • singleQuote → string-quote-style
  • bracketSpacing → inline-table-brace-space-width

endOfLine is handled by prettier itself. All of tombi's other format rules are exposed as toml options and can be used to override the inherited values:

interface PrettierOptions {
  // The TOML version to use when parsing and formatting.
  tomlVersion: 'v1.0.0' | 'v1.1.0-preview' | 'v1.1.0' // default `v1.0.0`
  // The number of spaces inside the brackets of a single line array.
  arrayBracketSpaceWidth: number // default `0`
  // The number of spaces after the comma in a single line array.
  arrayCommaSpaceWidth: number // default `1`
  // The style used to format comments.
  commentStyle: 'normalize' | 'preserve' // default `normalize`
  // The delimiter between date and time.
  dateTimeDelimiter: 'preserve' | 'space' | 'T' // default `T`
  // The blank lines limit between groups.
  groupBlankLinesLimit: number // default `1`
  // Whether to indent sub-tables.
  indentSubTables: boolean // default `false`
  // Whether to indent table key-value pairs.
  indentTableKeyValuePairs: boolean // default `false`
  // The number of spaces inside the braces of a single line inline table,
  // defaults to `bracketSpacing` (`1` or `0`).
  inlineTableBraceSpaceWidth: number // default `bracketSpacing`
  // The number of spaces after the comma in a single line inline table.
  inlineTableCommaSpaceWidth: number // default `1`
  // Whether to align the equals sign in key-value pairs.
  keyValueEqualsSignAlignment: boolean // default `false`
  // The preferred quote character for keys, defaults to `stringQuoteStyle`.
  keyQuoteStyle: 'double' | 'preserve' | 'single' // default `undefined`
  // The preferred quote character for strings, defaults to `singleQuote`.
  stringQuoteStyle: 'double' | 'preserve' | 'single' // default `singleQuote`
  // Whether to align the trailing comments in key-value pairs.
  trailingCommentAlignment: boolean // default `false`
  // The number of spaces around the equals sign in a key-value pair.
  keyValueEqualsSignSpaceWidth: number // default `1`
  // The number of blank lines between tables.
  tableBlankLines: number // default `1`
  // The number of spaces before a trailing comment.
  trailingCommentSpaceWidth: number // default `2`
}

Sponsors and Backers

Sponsors and Backers

Sponsors
1stG RxTS UnTS
1stG Open Collective sponsors RxTS Open Collective sponsors UnTS Open Collective sponsors
Backers
1stG RxTS UnTS
1stG Open Collective backers RxTS Open Collective backers UnTS Open Collective backers

Changelog

Detailed changes for each release are documented in CHANGELOG.md.

License

MIT Ray@mk1.io

Keywords