Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions apps/oxfmt/src-js/cli/migration/migrate-prettier.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,13 +59,17 @@ 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)) {
for (const plugin of (value as Options["plugins"])!) {
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 {
Expand Down Expand Up @@ -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`);
Expand Down
49 changes: 49 additions & 0 deletions apps/oxfmt/test/cli/migrate_prettier/migrate_prettier.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 });
}
});
});
Loading