tombi-wasm-lib

tombi-wasm-lib provides Tombi's formatter and linter for browsers and other JavaScript environments.

Install

pnpm add tombi-wasm-lib

Format TOML

Initialize the WASM module before calling format:

import init, { format, type TombiWasmError } from "tombi-wasm-lib";

await init();

const source = `
[package]
name = "example"
`;

try {
  const { formatted, diagnostics } = await format(source, "Cargo.toml");

  if (formatted !== undefined) {
    console.log(formatted);
  }

  for (const diagnostic of diagnostics) {
    if (diagnostic.level === "error") {
      console.error(diagnostic.message);
    } else {
      console.warn(diagnostic.message);
    }
  }
} catch (error) {
  console.error((error as TombiWasmError).message);
}

format resolves to an object containing the formatted TOML when formatting succeeds and an array of diagnostics. The diagnostics array is empty when none were reported. When diagnostics prevent formatting, formatted is omitted. Configuration and execution errors reject the Promise as TombiWasmError.

Lint TOML

Initialize the WASM module before calling lint:

import init, { lint, type TombiWasmError } from "tombi-wasm-lib";

await init();

const source = `
[package]
name = "example"
`;

try {
  const { diagnostics } = await lint(source, "Cargo.toml");

  if (diagnostics.length > 0) {
    for (const diagnostic of diagnostics) {
      const message = `${diagnostic.message} (${diagnostic.range.start.line}:${diagnostic.range.start.column})`;

      if (diagnostic.level === "error") {
        console.error(message);
      } else {
        console.warn(message);
      }
    }
  } else {
    console.log("No diagnostics");
  }
} catch (error) {
  // Configuration and execution errors reject the Promise.
  console.error((error as TombiWasmError).message);
}

lint resolves to an object containing a diagnostics array. The array is empty when no diagnostics were reported. Configuration and execution errors reject the Promise as TombiWasmError.

Both functions accept an optional options object as the third argument. config replaces the automatically discovered configuration and accepts an object or string:

const config = {
  content: `
toml-version = "v1.1.0"

[format.rules]
indent-width = 4
`,
  path: "/workspace/tombi.toml",
};

await format(source, "Cargo.toml", { config });
await lint(source, "Cargo.toml", { config });

// The configuration content can be passed as a string shorthand. It is
// treated as the content of a virtual `tombi.toml`.
await format(source, "Cargo.toml", { config: config.content });

When config is omitted, Tombi uses automatic discovery and falls back to the default configuration if no configuration file is found.