Skip to content

feat: format embedded languages in tagged templates in the Wasm plugin - #838

Merged
dsherret merged 2 commits into
mainfrom
tagged-template-wasm
Oct 31, 2026
Merged

dsherret merged 2 commits into
mainfrom
tagged-template-wasm

Conversation

@dsherret

Copy link
Copy Markdown
Member

Closes #9

The Wasm plugin now formats the text of tagged templates with the plugin the dprint host uses for the language's file extension. The Rust API could already do this with an external formatter, but the Wasm plugin passed none. For example, with the markup_fmt and malva plugins:

const page = html`
  <body>
    <header>HEADER</header>
    <main>${main}</main>
  </body>
`;

const Button = styled.button`
  color: red;
  padding: ${p => p.padding}px;
  &:hover {
    color: ${hoverColor};
  }
`;

Configuration

  • taggedTemplate.formatEmbedded (default true) turns it off.

  • taggedTemplate.embeddedLanguages maps a tag to the file extension of the language its text is formatted as, and replaces the defaults when it's specified:

    • css is formatted as less, because the placeholders of its expressions are Less variables (ex. @dpr1nt_00_d), which plain CSS doesn't allow in places like values;
    • gql and graphql as graphql;
    • html, sql, svg, and xml as themselves.

    The styled-components patterns (ex. styled.div... and styled(Button)...) use the css entry.

Both are in the schema.

How it works

  • The template's text is formatted with the host using the plugin's lineWidth, indentWidth, useTabs, and newLineKind: lf, so it lines up with the code when it's put back in the template.
  • The text is dedented and trimmed first so formatting it again gives the same text. CSS is wrapped in a rule (a{ ... }) so declarations are valid, and unwrapped after.
  • A template is left as is when the host says the text didn't change, which is also what it says when no plugin formats the extension.
  • A template on a single line stays on a single line when its formatted text does too (ex. html<b>text</b> in a conditional), instead of always being put on its own lines.
  • A critical error from the host (ex. it's shutting down) is returned as is instead of as a formatting error.

Breaking changes to the Rust API

  • The external formatter is now called with the file extension of the language (ex. "less" for css... by default) instead of the tag, and only for the tags in taggedTemplate.embeddedLanguages.
  • ExternalFormatter has a lifetime parameter, which lets the Wasm plugin's formatter borrow the host's callback. Code that writes &ExternalFormatter still compiles.

Rust 2024 edition formatting

The second commit fixes the rustfmt command in dprint.json, which didn't pass the edition, so it formatted with the 2015 edition and couldn't format files that use let chains (ex. generate.rs). It now passes --edition 2024 like cargo fmt does, and reformats the code with it, so dprint check and cargo fmt --check agree.

Testing

  • Specs for the new options, single-line templates, and the existing embedded language specs updated for single-line templates staying inline.
  • Unit tests for the dedenting, trimming, and CSS wrapping.
  • Formatted with the dprint CLI using the built Wasm plugin with the markup_fmt and malva plugins, and checked that formatting again doesn't change the output.

The Wasm plugin now formats the text of tagged templates (ex. html`...` and
css`...`) with the plugin the dprint host uses for the language's file extension,
which the Rust API could already do with an external formatter.

- `taggedTemplate.formatEmbedded` (default `true`) turns it off.
- `taggedTemplate.embeddedLanguages` maps a tag to the file extension of the
  language its text is formatted as, which replaces the defaults (`css` as `less`,
  `gql` and `graphql` as `graphql`, and `html`, `sql`, `svg`, and `xml` as
  themselves). The styled-components patterns use the `css` entry.

The text is dedented and trimmed before it's formatted, and CSS is wrapped in a
rule so declarations are valid. A template is left as is when the host says the
text didn't change, which is also what it says when no plugin formats the
extension. A template on a single line stays on a single line when its formatted
text does too.

BREAKING CHANGE: The external formatter of the Rust API is now called with the
file extension of the language (ex. `"less"` for css`...`) instead of the tag, and
`ExternalFormatter` has a lifetime parameter.

Closes #9
The rustfmt command in dprint.json didn't pass the edition, so it formatted with the
2015 edition and couldn't format files that use let chains (ex. generate.rs). It now
passes --edition 2024 like cargo fmt does, and this formats the code with it.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Format html tagged template text

1 participant