Skip to content

Add support for @prettier/plugin-oxc parsers#398

Open
roryabraham wants to merge 1 commit into
trivago:mainfrom
roryabraham:add-oxc-parser-support
Open

Add support for @prettier/plugin-oxc parsers#398
roryabraham wants to merge 1 commit into
trivago:mainfrom
roryabraham:add-oxc-parser-support

Conversation

@roryabraham
Copy link
Copy Markdown

Problem

@prettier/plugin-oxc registers oxc and oxc-ts parsers that override Prettier's built-in babel and typescript parsers for JS/TS/JSX/TSX files. Since this plugin only attaches its import-sorting preprocess hook to the babel, typescript, flow, vue, svelte, and ember-template-tag parsers, import sorting is silently skipped when @prettier/plugin-oxc is active.

This is because Prettier auto-detects the parser from the file extension, and when OXC is loaded it maps .ts/.tsx/.js/.jsx to oxc-ts/oxc instead of typescript/babel. The OXC parsers have no preprocess hook, so the sort-imports preprocessing never runs.

Solution

This PR adds oxc and oxc-ts parser entries that compose OXC's parser with the sort-imports defaultPreprocessor. The import of @prettier/plugin-oxc is wrapped in a try/catch so it remains fully optional — if the OXC plugin is not installed, nothing changes.

Changes:

  • src/index.ts: Dynamically import @prettier/plugin-oxc (with try/catch fallback) and add oxc/oxc-ts parser entries with preprocess: defaultPreprocessor
  • package.json: Add @prettier/plugin-oxc as an optional peerDependency (>= 0.1.0) and as a devDependency for testing

How it works

The preprocess hook operates on source text using @babel/parser to extract and reorder imports, then returns the modified source text. This is independent of the downstream parser (OXC), which receives the already-sorted source. OXC's parse, astFormat, and other properties are preserved via object spread.

Testing

Verified manually:

  1. Created a .ts file with deliberately out-of-order imports
  2. Ran prettier with both @prettier/plugin-oxc and this patched plugin
  3. Imports were correctly sorted according to importOrder config
  4. Without the patch, imports were left unsorted

@prettier/plugin-oxc registers `oxc` and `oxc-ts` parsers that override
the built-in `babel` and `typescript` parsers for JS/TS files. Since
this plugin only attaches its import-sorting preprocess hook to
`babel`/`typescript`/`flow`/`vue` parsers, import sorting is silently
skipped when OXC is active.

This adds `oxc` and `oxc-ts` parser entries that compose OXC's parser
with the sort-imports `defaultPreprocessor`. The import is wrapped in
try/catch so `@prettier/plugin-oxc` remains an optional dependency.
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.

1 participant