Skip to content

Conversation

@Lakshith7748
Copy link

Description

This PR fixes a RangeError: Maximum call stack size exceeded when using many overlapping decorations.

The issue was caused by transformer-decorations creating deeply nested
span
elements for overlapping decorations. When the nesting depth exceeded the stack limit (e.g., > 2500 levels), hast-util-to-html would crash due to recursion.

This fix modifies transformer-decorations to flatten the decoration structure. Instead of wrapping the existing content in a new
span
for every decoration (which creates nesting), the transformer now applies the decoration to each child element individually when possible.

Linked Issues

Fixes #592

Additional context

  • Modified packages/core/src/transformer-decorations.ts to apply decorations to individual children instead of creating a wrapper.

  • Preserved the behavior of creating a wrapper for empty decorations (insertion points).

  • Updated snapshots in packages/core/test/decorations.test.ts to reflect the flattened structure.

  • Verified with a reproduction test case involving 5000 overlapping decorations.

@netlify
Copy link

netlify bot commented Dec 2, 2025

Deploy Preview for shiki-matsu ready!

Name Link
🔨 Latest commit b7f9009
🔍 Latest deploy log https://app.netlify.com/projects/shiki-matsu/deploys/692fd30e0008710008be23d1
😎 Deploy Preview https://deploy-preview-1182--shiki-matsu.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify project configuration.

@netlify
Copy link

netlify bot commented Dec 2, 2025

Deploy Preview for shiki-next ready!

Name Link
🔨 Latest commit b7f9009
🔍 Latest deploy log https://app.netlify.com/projects/shiki-next/deploys/692fd30e9014060008a33864
😎 Deploy Preview https://deploy-preview-1182--shiki-next.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify project configuration.

@codecov
Copy link

codecov bot commented Dec 2, 2025

Codecov Report

❌ Patch coverage is 43.33333% with 51 lines in your changes missing coverage. Please review.
✅ Project coverage is 94.48%. Comparing base (5ddea95) to head (be2fbbd).

Files with missing lines Patch % Lines
packages/core/src/transformer-decorations.ts 43.33% 51 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #1182      +/-   ##
==========================================
- Coverage   95.08%   94.48%   -0.60%     
==========================================
  Files          91       91              
  Lines        7866     7941      +75     
  Branches     1662     1672      +10     
==========================================
+ Hits         7479     7503      +24     
- Misses        381      432      +51     
  Partials        6        6              

☔ View full report in Codecov by Sentry.
📢 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.

@antfu
Copy link
Member

antfu commented Dec 3, 2025

I think we should provide an option for wrap/flatten style.

And I am not sure if we need to introduce the options to disable the interaction check. Maybe for flatten mode we could just skip it

Copy link
Member

@antfu antfu left a comment

Choose a reason for hiding this comment

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

The current approach is a breaking change, and we would not take it. Refer to the comment above.

- Add decorationsStyle option ('wrap' | 'flatten') to DecorationOptions
- 'wrap' (default): maintains current behavior with nested wrappers and intersection validation
- 'flatten': skips intersection check, allowing deeply nested decorations without stack overflow
- Addresses maintainer feedback to avoid breaking changes
- Add test case for flatten mode with nested decorations
@Lakshith7748
Copy link
Author

The current approach is a breaking change, and we would not take it. Refer to the comment above.

Thanks for the feedback! I've updated the PR to add a decorationsStyle option instead of making breaking changes.

  • 'wrap' (default): Current behavior with intersection validation
  • 'flatten': Skips intersection check, allows deeply nested decorations

@Lakshith7748 Lakshith7748 requested a review from antfu December 3, 2025 06:09
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.

Decorations: Allow intersections

2 participants