fix: preserve inner comment when adding a property to an empty object#165
fix: preserve inner comment when adding a property to an empty object#165naveentehrpariya wants to merge 1 commit into
Conversation
Babel attaches comments inside an otherwise-empty object literal
(e.g. `{ /* keep me */ }`) as `innerComments`. recast's printer reads
comments from `node.comments` and ignores `innerComments`, so once the
object is modified and reprinted the comment was silently dropped.
When inserting the first property into such an object, move the inner
comments onto the new property as leading comments so they survive code
generation.
Fixes unjs#33
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: defaults Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (2)
📝 WalkthroughWalkthrough
ChangesInner comment preservation on empty object literals
Estimated code review effort🎯 2 (Simple) | ⏱️ ~8 minutes Poem
🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
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. Comment |
Problem
Comments placed inside an otherwise-empty object literal are lost as soon as a property is added to that object.
Actual:
Expected — the inner comment is preserved:
Cause
@babel/parserattaches comments that sit inside an empty object asinnerCommentson theObjectExpressionnode. recast's printer only looks atnode.comments(entries flaggedleading/trailing) and ignoresinnerComments. While the node is untouched recast reuses the original source verbatim, so the comment survives; once a property is added the node is reprinted and the comment is dropped.Fix
When inserting the first property into an object that still carries
innerComments, move those comments onto the new property as leading comments so recast emits them. Objects that already have properties, and objects without inner comments, are unaffected.Both block (
/* */) and line (//) inner comments are covered by the added tests.Fixes #33
Summary by CodeRabbit
Bug Fixes
Tests