Skip to content

fix: enhance handling of MPX directive comments in Stylus formatting#105

Merged
wangshunnn merged 1 commit into
mainfrom
fix-stylus-comments-formatter
Mar 13, 2026
Merged

fix: enhance handling of MPX directive comments in Stylus formatting#105
wangshunnn merged 1 commit into
mainfrom
fix-stylus-comments-formatter

Conversation

@wangshunnn

@wangshunnn wangshunnn commented Mar 13, 2026

Copy link
Copy Markdown
Collaborator

fix #99

This pull request enhances the handling of MPX directive comments within Stylus files by ensuring that these comments are preserved in their original positions during formatting. The main improvement is the introduction of logic to temporarily replace MPX directive comments with placeholders before formatting, and then restore them afterward, preventing the formatter from relocating these comments.

Preservation of MPX directive comments during formatting:

  • Added a regular expression mpxDirectiveCommentRegex to accurately match MPX directive comments such as /* @mpx-if (...) */ and similar variants.
  • Implemented logic to extract MPX directive comments, replace them with unique single-line placeholders before formatting, and restore them after formatting to maintain their original indentation and position.

Summary by CodeRabbit

  • Bug Fixes
    • Fixed Stylus formatter to properly preserve MPX directive comments in style blocks, maintaining correct indentation and alignment during formatting operations.

@coderabbitai

coderabbitai Bot commented Mar 13, 2026

Copy link
Copy Markdown
📝 Walkthrough

Walkthrough

Adds pre-processing and post-processing steps to Stylus formatting to preserve MPX directive comments. Introduces regex pattern to identify comments like /* @mpx-if (...) */, replaces them with placeholders before formatting, and restores the original comments afterward to prevent unwanted line breaks in conditional compilation directives.

Changes

Cohort / File(s) Summary
Stylus Formatting Comment Preservation
packages/language-service/src/plugins/mpx-sfc-style-stylus.ts
Added regex-based pre- and post-processing logic to preserve MPX directive comments during Stylus formatting. Comments are temporarily replaced with placeholders before formatter execution, then restored to their original form, preventing unwanted line breaks that would break conditional compilation.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~8 minutes

Possibly related PRs

Poem

🐰 Comments wrapped in stylus dreams,
Now preserved through formatting schemes,
No more breaks in directives bright,
Conditional logic stays just right! ✨

🚥 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%. 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 specifically describes the main change: enhancing handling of MPX directive comments in Stylus formatting, which is the primary focus of the code changes.
Linked Issues check ✅ Passed The code changes implement the core requirement from issue #99: preserving MPX directive comments in their original positions during Stylus formatting by using placeholder replacement and restoration logic.
Out of Scope Changes check ✅ Passed All changes are directly related to the objective of preserving MPX directive comments in Stylus formatting; no unrelated or out-of-scope modifications are present.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
  • 📝 Generate docstrings (stacked PR)
  • 📝 Generate docstrings (commit on current branch)
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch fix-stylus-comments-formatter
📝 Coding Plan
  • Generate coding plan for human review comments

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 and usage tips.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@packages/language-service/src/plugins/mpx-sfc-style-stylus.ts`:
- Around line 95-97: The placeholder "// __MPX_DIRECTIVE_${index}__" can collide
with user comments; modify the placeholder generation in this module (the code
that creates the placeholder variable and calls directiveComments.set) to use a
collision-proof token—e.g. append a random/unique suffix (crypto.randomUUID() or
a monotonic unique id combined with a hidden sentinel/unicode private-use
sequence) or otherwise encode metadata (index + uuid) so placeholders are
globally unique and unguessable; update all places that create or restore
placeholders (the placeholder variable, directiveComments.set(...) and the
corresponding restoration logic around lines referencing the same placeholder)
to use the new unique token format so restoration matches only generated
placeholders.

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 25da8fed-d092-433c-b216-f836d01e79d5

📥 Commits

Reviewing files that changed from the base of the PR and between 656bd53 and edd7a2d.

📒 Files selected for processing (1)
  • packages/language-service/src/plugins/mpx-sfc-style-stylus.ts

Comment thread packages/language-service/src/plugins/mpx-sfc-style-stylus.ts
@wangshunnn wangshunnn merged commit 4ba7a6a into main Mar 13, 2026
2 checks passed
@wangshunnn wangshunnn deleted the fix-stylus-comments-formatter branch March 13, 2026 08:33
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.

MPX插件格式化stylus样式的时候有问题

1 participant