Add support for @prettier/plugin-oxc parsers#398
Open
roryabraham wants to merge 1 commit into
Open
Conversation
@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.
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Problem
@prettier/plugin-oxcregistersoxcandoxc-tsparsers that override Prettier's built-inbabelandtypescriptparsers for JS/TS/JSX/TSX files. Since this plugin only attaches its import-sortingpreprocesshook to thebabel,typescript,flow,vue,svelte, andember-template-tagparsers, import sorting is silently skipped when@prettier/plugin-oxcis active.This is because Prettier auto-detects the parser from the file extension, and when OXC is loaded it maps
.ts/.tsx/.js/.jsxtooxc-ts/oxcinstead oftypescript/babel. The OXC parsers have nopreprocesshook, so the sort-imports preprocessing never runs.Solution
This PR adds
oxcandoxc-tsparser entries that compose OXC's parser with the sort-importsdefaultPreprocessor. The import of@prettier/plugin-oxcis 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 addoxc/oxc-tsparser entries withpreprocess: defaultPreprocessorpackage.json: Add@prettier/plugin-oxcas an optionalpeerDependency(>= 0.1.0) and as adevDependencyfor testingHow it works
The
preprocesshook operates on source text using@babel/parserto 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'sparse,astFormat, and other properties are preserved via object spread.Testing
Verified manually:
.tsfile with deliberately out-of-order imports@prettier/plugin-oxcand this patched pluginimportOrderconfig