diff --git a/apps/oxfmt/src-js/cli/migration/migrate-prettier.ts b/apps/oxfmt/src-js/cli/migration/migrate-prettier.ts index 4290a2289392a..668acc980a818 100644 --- a/apps/oxfmt/src-js/cli/migration/migrate-prettier.ts +++ b/apps/oxfmt/src-js/cli/migration/migrate-prettier.ts @@ -59,6 +59,8 @@ export async function runMigratePrettier() { // NOTE: Some options unsupported by Oxfmt may still be valid when invoking Prettier. // However, to avoid inconsistency, we do not enable options that affect Oxfmt. const oxfmtrc = await createBlankOxfmtrcFile(cwd); + + let hasSortPackageJsonPlugin = false; for (const [key, value] of Object.entries(prettierConfig ?? {})) { // Handle plugins - check for prettier-plugin-tailwindcss and warn about others if (key === "plugins" && Array.isArray(value)) { @@ -66,6 +68,8 @@ export async function runMigratePrettier() { if (plugin === "prettier-plugin-tailwindcss") { // Migrate `prettier-plugin-tailwindcss` options migrateTailwindOptions(prettierConfig!, oxfmtrc); + } else if (plugin === "prettier-plugin-packagejson") { + hasSortPackageJsonPlugin = true; } else if (typeof plugin === "string") { console.error(` - plugins: "${plugin}" is not supported, skipping...`); } else { @@ -103,6 +107,14 @@ export async function runMigratePrettier() { ); oxfmtrc.printWidth = 80; } + // `experimentalSortPackageJson` is enabled by default in Oxfmt, but Prettier does not have this. + // Only enable if `prettier-plugin-packagejson` is used. + if (hasSortPackageJsonPlugin) { + oxfmtrc.experimentalSortPackageJson = {}; + console.error(` - Migrated "prettier-plugin-packagejson" to "experimentalSortPackageJson"`); + } else { + oxfmtrc.experimentalSortPackageJson = false; + } // `embeddedLanguageFormatting` is not fully supported for JS-in-XXX yet. if (oxfmtrc.embeddedLanguageFormatting !== "off") { console.error(` - "embeddedLanguageFormatting" in JS/TS files is not fully supported yet`); diff --git a/apps/oxfmt/test/cli/migrate_prettier/migrate_prettier.test.ts b/apps/oxfmt/test/cli/migrate_prettier/migrate_prettier.test.ts index 12edac074b773..9f7b03ed1d6b9 100644 --- a/apps/oxfmt/test/cli/migrate_prettier/migrate_prettier.test.ts +++ b/apps/oxfmt/test/cli/migrate_prettier/migrate_prettier.test.ts @@ -213,4 +213,53 @@ node_modules await fs.rm(tempDir, { recursive: true, force: true }); } }); + + it("should disable experimentalSortPackageJson by default", async () => { + const tempDir = await fs.mkdtemp(join(tmpdir(), "oxfmt-migrate-test")); + + try { + // Create prettier config without package.json sorting plugin + await fs.writeFile( + join(tempDir, ".prettierrc"), + JSON.stringify({ + semi: false, + }), + ); + + const result = await runCli(tempDir, ["--migrate", "prettier"]); + expect(result.exitCode).toBe(0); + + const content = await fs.readFile(join(tempDir, ".oxfmtrc.json"), "utf8"); + const oxfmtrc = JSON.parse(content); + + // Prettier does not have package.json sorting by default + expect(oxfmtrc.experimentalSortPackageJson).toBe(false); + } finally { + await fs.rm(tempDir, { recursive: true, force: true }); + } + }); + + it("should enable experimentalSortPackageJson when prettier-plugin-packagejson is used", async () => { + const tempDir = await fs.mkdtemp(join(tmpdir(), "oxfmt-migrate-test")); + + try { + // Create prettier config with package.json sorting plugin + await fs.writeFile( + join(tempDir, ".prettierrc"), + JSON.stringify({ + plugins: ["prettier-plugin-packagejson"], + }), + ); + + const result = await runCli(tempDir, ["--migrate", "prettier"]); + expect(result.exitCode).toBe(0); + + const content = await fs.readFile(join(tempDir, ".oxfmtrc.json"), "utf8"); + const oxfmtrc = JSON.parse(content); + + expect(oxfmtrc.experimentalSortPackageJson).toBeTruthy(); + } finally { + await fs.rm(tempDir, { recursive: true, force: true }); + } + }); });