# `OXC.Format`
[🔗](https://github.com/elixir-volt/oxc_ex/blob/v0.10.0/lib/oxc/format.ex#L1)

Format JavaScript/TypeScript source code with oxfmt.

Prettier-compatible formatter built on OXC, ~30× faster than Prettier.
Supports JS, JSX, TS, and TSX.

## Examples

    {:ok, formatted} = OXC.Format.run("const x=1;let   y =  2;", "test.js")
    # "const x = 1;
let y = 2;
"

    {:ok, formatted} = OXC.Format.run("const x=1", "test.js", semi: false)
    # "const x = 1
"

# `option`

```elixir
@type option() ::
  {:print_width, pos_integer()}
  | {:tab_width, pos_integer()}
  | {:use_tabs, boolean()}
  | {:semi, boolean()}
  | {:single_quote, boolean()}
  | {:jsx_single_quote, boolean()}
  | {:trailing_comma, :all | :none}
  | {:bracket_spacing, boolean()}
  | {:bracket_same_line, boolean()}
  | {:arrow_parens, :always | :avoid}
  | {:end_of_line, :lf | :crlf | :cr}
  | {:quote_props, :as_needed | :consistent | :preserve}
  | {:single_attribute_per_line, boolean()}
  | {:object_wrap, :preserve | :collapse}
  | {:experimental_operator_position, :start | :end}
  | {:experimental_ternaries, boolean()}
  | {:embedded_language_formatting, :auto | :off}
  | {:sort_imports, boolean() | sort_imports_opts()}
  | {:sort_tailwindcss, boolean() | sort_tailwindcss_opts()}
```

# `sort_imports_opts`

```elixir
@type sort_imports_opts() :: %{
  optional(:ignore_case) =&gt; boolean(),
  optional(:sort_side_effects) =&gt; boolean(),
  optional(:order) =&gt; :asc | :desc,
  optional(:newlines_between) =&gt; boolean(),
  optional(:partition_by_newline) =&gt; boolean(),
  optional(:partition_by_comment) =&gt; boolean(),
  optional(:internal_pattern) =&gt; [String.t()]
}
```

# `sort_tailwindcss_opts`

```elixir
@type sort_tailwindcss_opts() :: %{
  optional(:config) =&gt; String.t(),
  optional(:stylesheet) =&gt; String.t(),
  optional(:functions) =&gt; [String.t()],
  optional(:attributes) =&gt; [String.t()],
  optional(:preserve_whitespace) =&gt; boolean(),
  optional(:preserve_duplicates) =&gt; boolean()
}
```

# `run`

```elixir
@spec run(String.t(), String.t(), [option()]) ::
  {:ok, String.t()} | {:error, [String.t()]}
```

Format source code.

## Options

  * `:print_width` — line width (default: 80)
  * `:tab_width` — spaces per indentation level (default: 2)
  * `:use_tabs` — indent with tabs instead of spaces (default: false)
  * `:semi` — print semicolons (default: true)
  * `:single_quote` — use single quotes (default: false)
  * `:jsx_single_quote` — use single quotes in JSX (default: false)
  * `:trailing_comma` — `:all` or `:none` (default: `:all`)
  * `:bracket_spacing` — spaces inside object braces (default: true)
  * `:bracket_same_line` — put `>` on the same line (default: false)
  * `:arrow_parens` — `:always` or `:avoid` (default: `:always`)
  * `:end_of_line` — `:lf`, `:crlf`, or `:cr` (default: `:lf`)
  * `:quote_props` — `:as_needed`, `:consistent`, or `:preserve` (default: `:as_needed`)
  * `:single_attribute_per_line` — force one attribute per line in JSX (default: false)
  * `:object_wrap` — `:preserve` or `:collapse` (default: `:preserve`)
  * `:experimental_operator_position` — `:start` or `:end` (default: `:end`)
  * `:experimental_ternaries` — use curious ternaries (default: false)
  * `:embedded_language_formatting` — `:auto` or `:off` (default: `:auto`)
  * `:sort_imports` — `true` for defaults, or a map with sub-options:
    * `:ignore_case` — case-insensitive sorting (default: true)
    * `:sort_side_effects` — sort side-effect imports (default: false)
    * `:order` — `:asc` or `:desc` (default: `:asc`)
    * `:newlines_between` — blank lines between groups (default: true)
    * `:partition_by_newline` — partition by existing newlines (default: false)
    * `:partition_by_comment` — partition by comments (default: false)
    * `:internal_pattern` — prefixes for internal imports (default: `["~/", "@/"]`)
  * `:sort_tailwindcss` — `true` for defaults, or a map with sub-options:
    * `:config` — path to Tailwind v3 config
    * `:stylesheet` — path to Tailwind v4 stylesheet
    * `:functions` — custom function names containing classes
    * `:attributes` — additional attributes to sort
    * `:preserve_whitespace` — preserve whitespace around classes (default: false)
    * `:preserve_duplicates` — preserve duplicate classes (default: false)

## Examples

    iex> {:ok, code} = OXC.Format.run("const   x=1", "test.js")
    iex> code
    "const x = 1;\n"

    iex> {:ok, code} = OXC.Format.run("const x=1", "test.js", semi: false)
    iex> code
    "const x = 1\n"

    iex> {:ok, code} = OXC.Format.run("const x = {a: 1, b: 2}", "test.js", print_width: 20)
    iex> String.contains?(code, "\n")
    true

# `run!`

```elixir
@spec run!(String.t(), String.t(), [option()]) :: String.t()
```

Like `run/3` but raises on errors.

## Examples

    iex> OXC.Format.run!("const   x=1", "test.js")
    "const x = 1;\n"

---

*Consult [api-reference.md](api-reference.md) for complete listing*
