Skip to content

Conversation

@Rich-Harris
Copy link
Member

This is an alternative to #79 — rather than passing in a WeakMap, which is arguably a bit of an unusual API, this adds two new function options — getLeadingComments and getTrailingComments. Both receive a TSESTree.Node and return either a BaseComment[] or undefined.

I find function-based APIs tend to age a bit better, because they can be hammered into whatever shape you need that day. Obviously you can still use a WeakMap on the consumer side if you prefer (just pass a function that reads from the map), but it's no longer required.

Another reason I like this approach is that we don't need to add a new type with a position property — we can just the same types that are commonly found in the (TS)ESTree ecosystem.

@changeset-bot
Copy link

changeset-bot bot commented Nov 24, 2025

🦋 Changeset detected

Latest commit: 47caf1c

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
esrap Minor

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@pkg-pr-new
Copy link

pkg-pr-new bot commented Nov 24, 2025

npm i https://pkg.pr.new/sveltejs/esrap@90

commit: 47caf1c

Copy link
Member

@manuel3108 manuel3108 left a comment

Choose a reason for hiding this comment

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

Honestly, I don't really mind. The other pr was probably too tailored around the needs for sv, but we can still totally do this with this api and just abstract it away. I don't see any notable differences to #79 so I'm happy if we wan't to proceed with this.

README.md Outdated
Comment on lines 60 to 63
// a WeakMap of AST nodes to additional comments to insert at specific nodes
// useful for programmatically adding comments during code transformation,
// especially for nodes that were added programmatically
additionalComments: new WeakMap()
Copy link
Member

Choose a reason for hiding this comment

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

We should fix this

Copy link
Member Author

Choose a reason for hiding this comment

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

Ah whoops. AFK, on the subway, but can update later

Rich-Harris and others added 3 commits November 25, 2025 13:25
Co-authored-by: Manuel <30698007+manuel3108@users.noreply.github.com>
…js/esrap into feat/additional-comments-function
@Rich-Harris Rich-Harris merged commit da2f84d into main Nov 25, 2025
9 checks passed
@Rich-Harris Rich-Harris deleted the feat/additional-comments-function branch November 25, 2025 19:13
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.

4 participants