Commit 01360d3
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
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
225 | 225 | | |
226 | 226 | | |
227 | 227 | | |
| 228 | + | |
| 229 | + | |
| 230 | + | |
| 231 | + | |
| 232 | + | |
| 233 | + | |
| 234 | + | |
| 235 | + | |
| 236 | + | |
228 | 237 | | |
229 | 238 | | |
230 | 239 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
497 | 497 | | |
498 | 498 | | |
499 | 499 | | |
500 | | - | |
| 500 | + | |
| 501 | + | |
| 502 | + | |
| 503 | + | |
| 504 | + | |
| 505 | + | |
| 506 | + | |
| 507 | + | |
501 | 508 | | |
502 | 509 | | |
503 | 510 | | |
| |||
0 commit comments