Skip to content

💡: Support edition of TOML config files while preserving comments and formatting #8815

Description

@DecimalTurn

Problem

As discussed in #7563, not being able to preserve comments when programmatically editing TOML files can be a problem.

Solution

I'd suggest using the library I'm currently maintanining (https://github.com/DecimalTurn/toml-patch), to allow for edition of TOML files while preserving comments and formatting.

Pros:

  • Simple API for patching: directly pass the original string and the edited JS Object.
    eg.:
    const existing = `
    # This is a TOML document
    
    title = "TOML example"
    owner.name = "Bob"
    `;
    const patched = TOML.patch(existing, {
      title: 'TOML example',
      owner: {
        name: 'Tim'
      }
    });
  • Supports TOML v1.1
  • Additional formatting options

Cons:

  • Less performant than smol-toml: ~3x slower for parsing and ~10x slower for stringifying. However for regular config file, the difference is almost imperceptible.

  • Larger package size than smol-toml: +54 kb compared to smol-toml

    Metric smol-toml @decimalturn/toml-patch Difference
    Minified ~12.8 kB ~67.2 kB +54.4 kB
    Min + Gzipped ~5.3 kB ~20.8 kB +15.5 kB
    Dependencies 0 0

Alternatives

Other packages I'm aware of that also support comments and formatting preservation:

  • https://github.com/ota-meshi/toml-eslint-parser
    This implementation requires to manipulate TOML documents by interfacing directly with the AST. It's powerful, but a bit more involved than decimalturn/toml-patch's API
  • shopify/toml-patch
    This package is a web-assembly wrapper around the toml-edit rust crate. It's not necessarly faster than native JS in this case lilkely due to the web-assembly inital overhead. Also, the API requires the use of strings to define edition paths (eg. ['package', 'version']) which is not ideal.

Additional Context

If that is something of interest, I can also help make the PR.

Code of Conduct

  • I agree to follow this project's Code of Conduct

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions