Skip to content

Commit 01360d3

Browse files
shulaodafengmk2
andauthored
fix(migrate): emit a single --check when rewriting prettier scripts (#2044)
#### Problem When migrating a `package.json` script, prettier flags are rewritten to `vp fmt`, converting `--list-different` / `-l` / `-c` to `--check`. If the script also contained a literal `--check`, the result depended on the flag order: | input | before | after | | --- | --- | --- | | `prettier --check --list-different .` | `vp fmt --check .` | `vp fmt --check .` | | `prettier --list-different --check .` | `vp fmt --check --check .` ❌ | `vp fmt --check .` | | `prettier --check --check .` | `vp fmt --check --check .` ❌ | `vp fmt --check .` | `strip_flags_from_suffix` (in `script_rewrite.rs`) converts source flags to the target flag and dedups against a literal `dedup_flag`. But the dedup branch only set `conversion_emitted` — it did not drop the literal token or `break`, so a literal `--check` that came **after** a converted flag still fell through to `suffix.0.push(item)` and was emitted a second time. Only the reverse order (`--check` first) worked, because the literal was pushed once and the later conversion was suppressed. #### Fix Make the dedup branch symmetric — when the token is the literal `dedup_flag`: ```rust if val == conv.dedup_flag { // Drop a duplicate if the target flag was already emitted; // otherwise keep this one and mark it emitted. if conversion_emitted[ci] { converted = true; } else { conversion_emitted[ci] = true; } break; } ``` Now the target flag is emitted exactly once regardless of order. The added `break` is safe: prettier is the only config with a `flag_conversions` rule and it has exactly one (eslint's is empty), so nothing later in the loop is skipped. #### Test Extended `test_rewrite_prettier_list_different_to_check` with the two previously-broken orderings — `--list-different --check` and the double-literal `--check --check` — both asserting a single `vp fmt --check .`. They fail on the old code (duplicate `--check`) and pass on the fix. The full `vite_migration` suite stays green (293 passed). #### Reproduce ```bash cargo test -p vite_migration --lib \ prettier::tests::test_rewrite_prettier_list_different_to_check ``` To see the bug: revert the `dedup_flag` branch to `conversion_emitted[ci] = true;` (no `break`) and rerun — the test fails with `left: "vp fmt --check --check ."`. Co-authored-by: MK (fengmk2) <fengmk2@gmail.com>
1 parent 0fa9bf1 commit 01360d3

2 files changed

Lines changed: 17 additions & 1 deletion

File tree

crates/vite_migration/src/prettier.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,15 @@ mod tests {
225225
rewrite_prettier_script("prettier --check --list-different ."),
226226
"vp fmt --check ."
227227
);
228+
229+
// --list-different + --check (the other order) → still a single --check
230+
assert_eq!(
231+
rewrite_prettier_script("prettier --list-different --check ."),
232+
"vp fmt --check ."
233+
);
234+
235+
// two literal --check → still a single --check
236+
assert_eq!(rewrite_prettier_script("prettier --check --check ."), "vp fmt --check .");
228237
}
229238

230239
#[test]

crates/vite_migration/src/script_rewrite.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -497,7 +497,14 @@ fn strip_flags_from_suffix(
497497
break;
498498
}
499499
if val == conv.dedup_flag {
500-
conversion_emitted[ci] = true;
500+
// Drop a duplicate if the target flag was already emitted;
501+
// otherwise keep this one and mark it emitted.
502+
if conversion_emitted[ci] {
503+
converted = true;
504+
} else {
505+
conversion_emitted[ci] = true;
506+
}
507+
break;
501508
}
502509
}
503510
if converted {

0 commit comments

Comments
 (0)