Files
oxc/apps/oxfmt/src-js/libs/prettier-plugin-oxfmt/index.ts
T
Yuji Sugiura 296623d868 feat(oxfmt)!: format parser:html files by oxc_formatter_htmlish (#28273)
Fixes #28263 

- Before this PR: `.html` and also html-in-{js,md,etc} are formatted by
Prettier.
  - Because of that, we had to run JS, which became an overhead.
- After this PR: All of them are formatted natively by
`oxc_formatter_htmlish`.
  - No overhead.

At the same time, since we are fixing Prettier bugs and inconsistent
behavior, please refer to DIVERGENCES.md for the differences.
2026-10-30 16:29:13 +09:00

53 lines
1.5 KiB
TypeScript

/**
* Prettier plugin that uses `oxc_formatter` for (j|t)s-in-xxx part.
*
* When Prettier formats Vue/Angular (which can embed JS/TS code inside) files,
* it calls the `embed()` function for each block.
*
* By default, it uses the `babel` or `typescript` parser and `estree` printer.
* Therefore, by overriding these internally, we can use `oxc_formatter` instead.
* e.g. Now it's possible to apply our builtin sort-imports for JS/TS code inside Vue `<script>`.
*/
import { textToDoc } from "./text-to-doc";
import type { Parser, Printer, Doc, SupportOptions } from "prettier";
// NOTE: Custom options must be declared here,
// or Prettier's normalization drops them before they reach `textToDoc()`.
export const options: SupportOptions = {
_oxfmtPluginOptionsJson: {
category: "JavaScript",
type: "string",
default: "{}",
description: "Bundled JSON string for oxfmt-plugin options",
},
_oxfmtVueScriptLang: {
category: "JavaScript",
type: "string",
default: "",
description:
'Effective `lang` of Vue SFC `<script>` blocks when it needs disambiguation (currently only "tsx"), scanned by the host',
},
};
const oxfmtParser: Parser<Doc> = {
parse: textToDoc,
astFormat: "OXFMT",
// Not used but required
locStart: () => -1,
locEnd: () => -1,
};
export const parsers: Record<string, Parser> = {
// Override default JS/TS parsers
babel: oxfmtParser,
"babel-ts": oxfmtParser,
typescript: oxfmtParser,
};
export const printers: Record<string, Printer<Doc>> = {
OXFMT: {
print: ({ node }) => node,
},
};