feat: getLeadingComments/getTrailingComments#90
Merged
Rich-Harris merged 14 commits intomainfrom Nov 25, 2025
Merged
Conversation
🦋 Changeset detectedLatest commit: 47caf1c The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
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 |
commit: |
6 tasks
manuel3108
approved these changes
Nov 25, 2025
Member
manuel3108
left a comment
There was a problem hiding this comment.
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() |
Member
Author
There was a problem hiding this comment.
Ah whoops. AFK, on the subway, but can update later
Co-authored-by: Manuel <30698007+manuel3108@users.noreply.github.com>
…js/esrap into feat/additional-comments-function
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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 —getLeadingCommentsandgetTrailingComments. Both receive aTSESTree.Nodeand return either aBaseComment[]orundefined.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
WeakMapon 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
positionproperty — we can just the same types that are commonly found in the (TS)ESTree ecosystem.