fix: allow leading and trailing comments in mustache tag #11866
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.
Fixes #7456
This PR allows having leading and trailing comments in mustache expressions. This can be useful when disabling eslint for expression in expression, like following
This PR does not change that having only comments in mustache does not work, such as
{/* comment */}
as often used in React.trailingComments are attached to parent node instead when
parent.end === node.end
, so that checking for end position of 'js expression + comment' can be done by checking for end position of the root node's last trailing comment.Further
If this PR is accepted, the
print(ast)
function of esrap (and perhaps code-red as well) needs to be changed. An assumption is made that there can only be at most 1 trailingComments, but this PR changes it to potentially have multiple comments. So it causes a minor bug where the 2nd+ trailing comments are not printed when printing js from ast.https://github.com/Rich-Harris/esrap/blob/2b9561c679ed38cc216e7d481a323bfef387a022/src/handlers.js#L59-L61
If needed, I can create a PR for that as well.