Skip to content

fix: preserve tab indentation when modifying modules - #179

Open
xianjianlf2 wants to merge 1 commit into
unjs:mainfrom
xianjianlf2:fix/preserve-tab-indentation
Open

xianjianlf2 wants to merge 1 commit into
unjs:mainfrom
xianjianlf2:fix/preserve-tab-indentation

Conversation

@xianjianlf2

@xianjianlf2 xianjianlf2 commented Sep 12, 2026

Copy link
Copy Markdown

Fixes #34.

Adding a Vite plugin to a config indented with one tab currently changes plugins to four tabs and emits a mixture of tabs and spaces for the plugin options. The issue's reproducer now keeps one tab for plugins and two tabs for the nested option.

There are two causes:

  • Recast parses with its default tab width, while generateCode() prints using the detected width. Pass the detected width to parseModule() too, preserving an explicit parser override.
  • The vendored Recast line concatenation turns appended indentation into literal spaces, even when the prefix contains no code. Keep that leading indentation in the line metadata so the printer can render it as tabs. Inline spacing and the source-map offset calculation retain their existing behavior.

The regression exercises addVitePlugin() with both detected indentation and an explicit tab width of 4. Both cases fail on the unchanged base.

Validation:

  • pnpm lint passes on the clean source tree.
  • pnpm exec vitest run --coverage: 85 tests pass.
  • pnpm build and pnpm run test:build --run: build succeeds; 85 tests pass against the built output.
  • pnpm exec tsc --noEmit --skipLibCheck passes. Unmodified pnpm typecheck reports the same 7 errors in dependency declarations on both the base and this branch.

AI assistance: OpenAI Codex assisted with diagnosis, implementation, and testing.

Summary by CodeRabbit

  • Bug Fixes

    • Fixed formatting so tab-indented source code preserves its existing tab width when modules are parsed, modified, and regenerated.
    • Improved output when adding Vite plugins, including cases with automatic or explicitly configured tab widths.
  • Tests

    • Added coverage confirming tab indentation remains intact across supported tab-width configurations.

@coderabbitai

coderabbitai Bot commented Sep 12, 2026

Copy link
Copy Markdown

Review Change StackReview Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Advanced

Run ID: 58cf3a6e-5ace-42f5-bec6-b020edeec19a

📥 Commits

Reviewing files that changed from the base of the PR and between 1804bf7 and 3e12683.

📒 Files selected for processing (3)
  • src/code.ts
  • test/format.test.ts
  • vendor/recast/lib/lines.ts

Included review availability: Your plan provides up to 8 included reviews per hour; 7 remain after this review.


📝 Walkthrough

Walkthrough

The parser now uses the configured or detected tab width. Recast line joining preserves indentation metadata. A regression test verifies tab indentation when adding a Vite plugin.

Changes

Tab indentation preservation

Layer / File(s) Summary
Parser and line-joining changes
src/code.ts, vendor/recast/lib/lines.ts
parseModule passes the configured or detected tabWidth. appendLines preserves incoming indentation when the previous line is empty.
Vite plugin indentation regression coverage
test/format.test.ts
The test covers tab-indented plugin arrays with undefined and explicit tabWidth values. It verifies that generated code retains tabs.

Priority: ⬇️ Low

Estimated code review effort: 2 (Simple) | ~10 minutes

Change: Bug fix · Severity of issue fixed: Low

Suggested reviewers: antfu

Merge Risk: ⚪ Minimal · up to 3e126

The indentation fix is covered by regression tests and no merge-blocking risk remains.

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 2 functions across 3 files. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly and concisely describes the main change: preserving tab indentation when modifying modules.
Linked Issues check ✅ Passed Issue #34 requires preservation of one-tab indentation for plugins, two-tab indentation for nested options, and one-tab indentation for the closing plugin configuration. parseModule now passes the…
Out of Scope Changes check ✅ Passed All reviewed changes support issue #34. The parser option change implements tab-width preservation. The vendored Recast change preserves renderable indentation metadata. The test covers the reported `…
  • Fix all pre-merge checks with AI
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@codecov

codecov Bot commented Sep 13, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 86.50%. Comparing base (46f6cee) to head (3e12683).
⚠️ Report is 46 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #179      +/-   ##
==========================================
- Coverage   93.54%   86.50%   -7.04%     
==========================================
  Files          25       27       +2     
  Lines        1797      882     -915     
  Branches      335      278      -57     
==========================================
- Hits         1681      763     -918     
+ Misses        115      102      -13     
- Partials        1       17      +16     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Tab style is not kept

2 participants