-
-
Notifications
You must be signed in to change notification settings - Fork 862
feat(formatter,oxfmt): Support js-in-vue (partially) #19514
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
graphite-app
merged 1 commit into
main
from
02-18-feat_formatter_oxfmt_support_js-in-vue_partially_
Feb 24, 2026
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
66 changes: 43 additions & 23 deletions
66
apps/oxfmt/src-js/libs/prettier-plugin-oxfmt/text-to-doc.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,34 +1,54 @@ | ||
| import { doc } from "prettier"; | ||
| import { jsTextToDoc } from "../../index"; | ||
| import type { Parser, Doc } from "prettier"; | ||
|
|
||
| const { hardline, join } = doc.builders; | ||
| const LINE_BREAK_RE = /\r?\n/; | ||
|
|
||
| export const textToDoc: Parser<Doc>["parse"] = async (embeddedSourceText, textToDocOptions) => { | ||
| // NOTE: For (j|t)s-in-xxx, default `parser` is either `babel` or `typescript` | ||
| // In case of ts-in-md, `filepath` is overridden to distinguish TSX or TS | ||
| // We need to infer `SourceType::from_path(fileName)` for `oxc_formatter`. | ||
| // `_oxfmtPluginOptionsJson` is a JSON string bundled by Rust (`oxfmtrc::finalize_external_options`), | ||
| // containing format options + parent filepath for the Rust-side `oxc_formatter`. | ||
| const { parser, parentParser, filepath, _oxfmtPluginOptionsJson } = textToDocOptions; | ||
| const fileName = | ||
| parser === "typescript" | ||
| ? filepath.endsWith(".tsx") | ||
| ? "dummy.tsx" // tsx-in-md | ||
| : "dummy.ts" // ts-in-md / ts-in-xxx | ||
| : "dummy.jsx"; // Otherwise, always enable JSX for js-in-xxx, it's safe | ||
|
|
||
| const { doc: formattedText, errors } = await jsTextToDoc( | ||
| fileName, | ||
|
|
||
| // For (j|t)s-in-xxx, default `parser` is either `babel`, `babel-ts` or `typescript` | ||
| // We need to infer `SourceType::from_extension(ext)` for `oxc_formatter`. | ||
| // - JS: always enable JSX for js-in-xxx, it's safe | ||
| // - TS: `typescript` (ts-in-vue|markdown|mdx) or `babel-ts` (ts-in-vue(script generic="...")) | ||
| // - In case of ts-in-md, `filepath` is overridden as `dummy.tx(x)` to distinguish TSX or TS | ||
| // - NOTE: tsx-in-vue is not supported since there is no signal from Prettier to detect it | ||
| // - Prettier is using `maybeJSXRe.test(sourceText)` to detect, but it's slow! | ||
| const isTS = parser === "typescript" || parser === "babel-ts"; | ||
| const embeddedSourceExt = isTS ? (filepath?.endsWith(".tsx") ? "tsx" : "ts") : "jsx"; | ||
|
|
||
| // Detect context from Prettier's internal flags | ||
| const parentContext = detectParentContext(parentParser!, textToDocOptions); | ||
|
|
||
| const doc = await jsTextToDoc( | ||
| embeddedSourceExt, | ||
| embeddedSourceText, | ||
| _oxfmtPluginOptionsJson as string, | ||
| [parentParser].join(":"), | ||
| parentContext, | ||
| ); | ||
|
|
||
| if (0 < errors.length) throw new Error(errors[0].message); | ||
| if (doc === null) { | ||
| throw new Error("`oxfmt::textToDoc()` failed. Use `OXC_LOG` env var to see Rust-side logs."); | ||
| } | ||
|
|
||
| // NOTE: This is required for the parent ((j|t)s-in-xxx) printer | ||
| // to handle line breaks correctly, | ||
| // not only for `options.vueIndentScriptAndStyle` but also for basic printing. | ||
| // TODO: Will be handled in Rust, convert our IR to Prettier's Doc directly. | ||
| return join(hardline, formattedText.split(LINE_BREAK_RE)); | ||
| // SAFETY: Rust side returns Prettier's `Doc` JSON | ||
| return JSON.parse(doc) as Doc; | ||
leaysgur marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| }; | ||
|
|
||
| /** | ||
| * Detects Vue fragment mode from Prettier's internal flags. | ||
| * | ||
| * When Prettier formats Vue SFC templates, it calls textToDoc with special flags: | ||
| * - `__isVueForBindingLeft`: v-for left-hand side (e.g., `(item, index)` in `v-for="(item, index) in items"`) | ||
| * - `__isVueBindings`: v-slot bindings (e.g., `{ item }` in `#default="{ item }"`) | ||
| * - `__isEmbeddedTypescriptGenericParameters`: `<script generic="...">` type parameters | ||
| */ | ||
| function detectParentContext(parentParser: string, options: Record<string, unknown>): string { | ||
| if (parentParser === "vue") { | ||
| if ("__isVueForBindingLeft" in options) return "vue-for-binding-left"; | ||
| if ("__isVueBindings" in options) return "vue-bindings"; | ||
| if ("__isEmbeddedTypescriptGenericParameters" in options) return "vue-script-generic"; | ||
| return "vue-script"; | ||
| } | ||
|
|
||
| return parentParser; | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.