mirror of
https://github.com/oxc-project/oxc.git
synced 2026-11-04 10:29:58 +00:00
feat(oxfmt): support overrides[].parser -> associations[].language migration (#27475)
`oxfmt --migrate prettier` can handle `parser` migration.
This commit is contained in:
1 parent
a495e8241e
commit
ce4e8dcbda
3 files changed
+87
-10
No files matched your search
@@ -5,6 +5,7 @@ import { readdir, readFile } from "node:fs/promises";
|
||||
import { createRequire } from "node:module";
|
||||
import { pathToFileURL } from "node:url";
|
||||
import { hasOxfmtrcFile, createBlankOxfmtrcFile, saveOxfmtrcFile, exitWithError } from "./shared";
|
||||
import type { Language } from "../../config.generated";
|
||||
|
||||
/**
|
||||
* Run the `--migrate prettier` command to migrate Prettier's config in the cwd to `.oxfmtrc.json` file.
|
||||
@@ -74,23 +75,35 @@ export async function runMigratePrettier() {
|
||||
|
||||
if (Array.isArray(overrides)) {
|
||||
const oxfmtOverrides = [];
|
||||
const associations = [];
|
||||
for (const { files, excludeFiles, options = {} } of overrides) {
|
||||
const migrated = {
|
||||
...migrateOptions(options, false),
|
||||
...migratePluginOptions(options, enabledPlugins, false),
|
||||
};
|
||||
// e.g. `{ files: "*.svg", options: { parser: "html" } }`
|
||||
if (Object.keys(migrated).length === 0) continue;
|
||||
oxfmtOverrides.push({
|
||||
const globs = {
|
||||
files: [files].flat(),
|
||||
...(excludeFiles !== undefined && { excludeFiles: [excludeFiles].flat() }),
|
||||
options: migrated,
|
||||
});
|
||||
};
|
||||
// e.g. `{ files: "*.svg", options: { parser: "html" } }`
|
||||
const { parser, ...rest } = options;
|
||||
const language = PARSER_TO_LANGUAGE.get(parser);
|
||||
if (language) {
|
||||
associations.push({ ...globs, language });
|
||||
} else if (parser !== undefined) {
|
||||
warnings.push(`overrides: "parser": "${parser}" is not supported, skipping...`);
|
||||
}
|
||||
const migrated = {
|
||||
...migrateOptions(rest, false),
|
||||
...migratePluginOptions(rest, enabledPlugins, false),
|
||||
};
|
||||
if (Object.keys(migrated).length === 0) continue;
|
||||
oxfmtOverrides.push({ ...globs, options: migrated });
|
||||
}
|
||||
if (oxfmtOverrides.length > 0) {
|
||||
oxfmtrc.overrides = oxfmtOverrides;
|
||||
console.log(` - Migrated "overrides"`);
|
||||
}
|
||||
if (associations.length > 0) {
|
||||
oxfmtrc.associations = associations;
|
||||
console.log(` - Migrated "parser" in "overrides" to "associations"`);
|
||||
}
|
||||
}
|
||||
|
||||
// Migrate `ignorePatterns` from `.prettierignore`
|
||||
@@ -279,6 +292,39 @@ function migrateOptions(
|
||||
return result;
|
||||
}
|
||||
|
||||
// Prettier's `parser` (built-in and known plugins) to Oxfmt's `associations[].language`.
|
||||
// Not listed (e.g. `flow`, `hermes`, `json-stringify`, `lwc`, `mjml`) are not supported.
|
||||
const PARSER_TO_LANGUAGE = new Map<unknown, Language>([
|
||||
["babel", "javascript"],
|
||||
["acorn", "javascript"],
|
||||
["espree", "javascript"],
|
||||
["meriyah", "javascript"],
|
||||
// `@prettier/plugin-oxc`, `@prettier/plugin-yuku`
|
||||
["oxc", "javascript"],
|
||||
["yuku", "javascript"],
|
||||
["typescript", "typescript"],
|
||||
["babel-ts", "typescript"],
|
||||
["oxc-ts", "typescript"],
|
||||
["yuku-ts", "typescript"],
|
||||
["json", "json"],
|
||||
["jsonc", "jsonc"],
|
||||
["json5", "json5"],
|
||||
["css", "css"],
|
||||
["scss", "scss"],
|
||||
["less", "less"],
|
||||
["graphql", "graphql"],
|
||||
["yaml", "yaml"],
|
||||
["markdown", "markdown"],
|
||||
["remark", "markdown"],
|
||||
["mdx", "mdx"],
|
||||
["html", "html"],
|
||||
["angular", "angular-html"],
|
||||
["vue", "vue"],
|
||||
["glimmer", "handlebars"],
|
||||
["svelte", "svelte"],
|
||||
["astro", "astro"],
|
||||
]);
|
||||
|
||||
// Plugin options: only enable when the corresponding Prettier plugin is used.
|
||||
// Empty object means "enabled with defaults"; Tailwind, Svelte and Astro are disabled by default.
|
||||
// In overrides, namespaces are deep-merged with top-level, so only set ones are needed.
|
||||
|
||||
@@ -67,6 +67,7 @@ oxfmt --migrate prettier
|
||||
Found Prettier config at: <cwd>/prettier.config.mjs
|
||||
- Migrated "prettier-plugin-tailwindcss" options to "sortTailwindcss"
|
||||
- Migrated "overrides"
|
||||
- Migrated "parser" in "overrides" to "associations"
|
||||
Created `.oxfmtrc.json`.
|
||||
It may not be formatted yet, run `oxfmt` to format it.
|
||||
```
|
||||
@@ -76,7 +77,7 @@ It may not be formatted yet, run `oxfmt` to format it.
|
||||
|
||||
Please review:
|
||||
- "printWidth" is not set in Prettier config, defaulting to 80 (Oxfmt default: 100)
|
||||
- overrides: "parser" is not supported, skipping...
|
||||
- overrides: "parser": "flow" is not supported, skipping...
|
||||
- overrides: "plugins" is not supported, skipping...
|
||||
- `prettier.config.mjs` was evaluated and migrated as static values, any logic in it is not preserved,
|
||||
port it to `oxfmt.config.ts` manually if needed
|
||||
@@ -96,6 +97,17 @@ Please review:
|
||||
]
|
||||
},
|
||||
"overrides": [
|
||||
{
|
||||
"files": [
|
||||
"src/**/*.html"
|
||||
],
|
||||
"excludeFiles": [
|
||||
"src/index.html"
|
||||
],
|
||||
"options": {
|
||||
"tabWidth": 4
|
||||
}
|
||||
},
|
||||
{
|
||||
"files": [
|
||||
"*.md"
|
||||
@@ -113,6 +125,23 @@ Please review:
|
||||
}
|
||||
}
|
||||
],
|
||||
"associations": [
|
||||
{
|
||||
"files": [
|
||||
"*.svg"
|
||||
],
|
||||
"language": "html"
|
||||
},
|
||||
{
|
||||
"files": [
|
||||
"src/**/*.html"
|
||||
],
|
||||
"excludeFiles": [
|
||||
"src/index.html"
|
||||
],
|
||||
"language": "angular-html"
|
||||
}
|
||||
],
|
||||
"ignorePatterns": []
|
||||
}
|
||||
|
||||
|
||||
@@ -4,6 +4,8 @@ export default {
|
||||
tailwindFunctions: ["clsx"],
|
||||
overrides: [
|
||||
{ files: "*.svg", options: { parser: "html" } },
|
||||
{ files: "*.js.flow", options: { parser: "flow" } },
|
||||
{ files: "src/**/*.html", excludeFiles: "src/index.html", options: { parser: "angular", tabWidth: 4 } },
|
||||
{
|
||||
files: ["*.md"],
|
||||
excludeFiles: "CHANGELOG.md",
|
||||
|
||||
Reference in new issue
Block a user