Skip to content

Commit 5cb22f0

Browse files
committed
fix(migrate): preserve unmigrated Prettier projects
1 parent 1064bde commit 5cb22f0

9 files changed

Lines changed: 70 additions & 14 deletions

File tree

docs/guide/migrate-rules.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -192,5 +192,6 @@ lint autofix preserves these imports.
192192
After updating the manifests and package-manager configuration, migration
193193
reinstalls dependencies once to refresh the lockfile. If installation fails,
194194
migration reports the error and exits with a nonzero status. After a successful
195-
migration, it runs `vp fmt`. A formatter failure is reported as a warning
196-
so the migration result and manual formatting command remain available.
195+
migration, it runs `vp fmt` unless the project still uses Prettier. A formatter
196+
failure is reported as a warning so the migration result and manual formatting
197+
command remain available.

packages/cli/snap-tests-global/migration-framework-shim-astro-vue/snap.txt

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,17 @@
11
> vp migrate --no-interactive --no-hooks # migration should add both Vue and Astro shims
2+
3+
Formatting code...
4+
5+
Code formatted
26
◇ Migrated . to Vite+
37
• Node <semver> pnpm <semver>
48
✓ Dependencies installed in <variable>ms
59
• 1 config update applied
610
• TypeScript shim added for framework component files
711

812
> cat src/env.d.ts # check both shims were written
9-
declare module '*.vue' {
10-
import type { DefineComponent } from 'vue';
13+
declare module "*.vue" {
14+
import type { DefineComponent } from "vue";
1115
const component: DefineComponent<{}, {}, unknown>;
1216
export default component;
1317
}

packages/cli/snap-tests-global/migration-framework-shim-astro/snap.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
11
> vp migrate --no-interactive --no-hooks # migration should add Astro shim when astro dependency is detected
2+
3+
Formatting code...
4+
5+
Code formatted
26
◇ Migrated . to Vite+
37
• Node <semver> pnpm <semver>
48
✓ Dependencies installed in <variable>ms
Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,17 @@
11
> vp migrate --no-interactive --no-hooks # migration should add Vue shim when vue dependency is detected
2+
3+
Formatting code...
4+
5+
Code formatted
26
◇ Migrated . to Vite+
37
• Node <semver> pnpm <semver>
48
✓ Dependencies installed in <variable>ms
59
• 1 config update applied
610
• TypeScript shim added for framework component files
711

812
> cat src/env.d.ts # check Vue shim was written
9-
declare module '*.vue' {
10-
import type { DefineComponent } from 'vue';
13+
declare module "*.vue" {
14+
import type { DefineComponent } from "vue";
1115
const component: DefineComponent<{}, {}, unknown>;
1216
export default component;
1317
}

packages/cli/snap-tests-global/migration-standalone-npm/snap.txt

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
11
> vp migrate --no-interactive --no-hooks # migration should work with npm, add overrides, and update lockfile
2+
3+
Formatting code...
4+
5+
Code formatted
26
◇ Migrated . to Vite+
37
• Node <semver> npm <semver>
48
✓ Dependencies installed in <variable>ms
@@ -11,10 +15,10 @@
1115
"vite": "npm:@voidzero-dev/vite-plus-core@<semver>",
1216
"vite-plus": "<semver>"
1317
},
14-
"packageManager": "npm@<semver>",
1518
"overrides": {
1619
"vite": "npm:@voidzero-dev/vite-plus-core@<semver>"
17-
}
20+
},
21+
"packageManager": "npm@<semver>"
1822
}
1923

2024
> node -e "const lock = require('./package-lock.json'); const vite = lock.packages['node_modules/vite']; if (vite && (vite.name === '@voidzero-dev/vite-plus-core' || vite.resolved?.includes('/@voidzero-dev/vite-plus-core/'))) console.log('lockfile has vite override'); else { console.error('vite override not found in lockfile'); process.exit(1); }" # verify lockfile updated with override

packages/cli/snap-tests-global/migration-standalone-pnpm/snap.txt

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
11
> vp migrate --no-interactive --no-hooks --package-manager pnpm # migration should work with pnpm, write overrides and peerDependencyRules to pnpm-workspace.yaml
2+
3+
Formatting code...
4+
5+
Code formatted
26
◇ Migrated . to Vite+
37
• Node <semver> pnpm <semver>
48
✓ Dependencies installed in <variable>ms
@@ -19,9 +23,9 @@ catalog:
1923
vite: npm:@voidzero-dev/vite-plus-core@<semver>
2024
vite-plus: <semver>
2125
overrides:
22-
vite: 'catalog:'
26+
vite: "catalog:"
2327
peerDependencyRules:
2428
allowAny:
2529
- vite
2630
allowedVersions:
27-
vite: '*'
31+
vite: "*"

packages/cli/src/migration/__tests__/format.spec.ts

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { describe, expect, it, vi } from 'vitest';
22

3-
import { formatMigratedProject } from '../format.ts';
3+
import { canFormatWithOxfmt, formatMigratedProject } from '../format.ts';
44
import { createMigrationReport } from '../report.ts';
55

66
describe('formatMigratedProject', () => {
@@ -45,3 +45,17 @@ describe('formatMigratedProject', () => {
4545
]);
4646
});
4747
});
48+
49+
describe('canFormatWithOxfmt', () => {
50+
it('formats projects that do not use Prettier', () => {
51+
expect(canFormatWithOxfmt(false, false)).toBe(true);
52+
});
53+
54+
it('formats projects after Prettier was migrated', () => {
55+
expect(canFormatWithOxfmt(true, true)).toBe(true);
56+
});
57+
58+
it('does not reformat projects that still use Prettier', () => {
59+
expect(canFormatWithOxfmt(true, false)).toBe(false);
60+
});
61+
});

packages/cli/src/migration/bin.ts

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ import {
4646
import type { PackageDependencies } from '../utils/types.ts';
4747
import { detectWorkspace } from '../utils/workspace.ts';
4848
import { checkRolldownCompatibility } from './compat-runner.ts';
49-
import { formatMigratedProject } from './format.ts';
49+
import { canFormatWithOxfmt, formatMigratedProject } from './format.ts';
5050
import {
5151
addFrameworkShim,
5252
checkVitestVersion,
@@ -388,6 +388,7 @@ interface MigrationPlan extends MigrationSetupPlan {
388388
packageManager: PackageManager;
389389
yarnPnpConverted: boolean;
390390
migratePrettier: boolean;
391+
hasPrettierDependency: boolean;
391392
prettierConfigFile?: string;
392393
fixBaseUrl: boolean;
393394
migrateNodeVersionFile: boolean;
@@ -724,6 +725,7 @@ async function collectMigrationPlan(
724725
yarnPnpConverted,
725726
...setupPlan,
726727
migratePrettier,
728+
hasPrettierDependency: prettierProject.hasDependency,
727729
prettierConfigFile: prettierProject.configFile,
728730
fixBaseUrl,
729731
migrateNodeVersionFile,
@@ -1159,7 +1161,10 @@ async function executeMigrationPlan(
11591161
workspaceInfo.rootDir,
11601162
report,
11611163
);
1162-
if (finalInstallSummary.status === 'installed') {
1164+
if (
1165+
finalInstallSummary.status === 'installed' &&
1166+
canFormatWithOxfmt(plan.hasPrettierDependency, plan.migratePrettier)
1167+
) {
11631168
await formatMigratedProject(workspaceInfo.rootDir, interactive, report);
11641169
}
11651170
return {
@@ -1530,7 +1535,12 @@ async function main() {
15301535
}
15311536
}
15321537

1533-
if (didMigrate && finalInstallOk && canFormatMigratedProject) {
1538+
if (
1539+
didMigrate &&
1540+
finalInstallOk &&
1541+
canFormatMigratedProject &&
1542+
canFormatWithOxfmt(prettierProject.hasDependency, prettierMigrated)
1543+
) {
15341544
clearMigrationProgress();
15351545
await formatMigratedProject(workspaceInfoOptional.rootDir, options.interactive, report);
15361546
}

packages/cli/src/migration/format.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,17 @@ type FormatRunner = (
1111
const FORMAT_FAILURE_MESSAGE =
1212
'Automatic formatting failed. Run `vp fmt` manually after migration.';
1313

14+
/**
15+
* Do not apply Oxfmt to a project that still uses Prettier. Their formatting
16+
* rules can conflict, especially when Prettier is enforced through ESLint.
17+
*/
18+
export function canFormatWithOxfmt(
19+
hasPrettierDependency: boolean,
20+
prettierMigrated: boolean,
21+
): boolean {
22+
return !hasPrettierDependency || prettierMigrated;
23+
}
24+
1425
/**
1526
* Format a successfully migrated project without turning a formatter problem
1627
* into an unhandled migration failure. The formatter already prints its

0 commit comments

Comments
 (0)