Fix: Comment optimization should not cause automatic-semi insert for single-line block #326
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.
Problem
Today the following piece of code is valid but it is parsed incorrectly:
Here's a link to the TypeScript Playground showing that the snippet above is valid JavaScript or TypeScript:
The output of
tree-sitter parse
is the following:This is because
name/*comment*/() {
was being parsed as having an automatic semicolon betweenname
and()
, but this isn't right.This was because there was an optimization that would cause automatic semicolons to be considered valid if a comment was started. This overlooked single line block comments.
Diff summary
Turns out the problem was really nuanced:
There are two callers of
scan_whitespace_and_comments
,This PR basically gives the ability for the first of these to be invoked (ie because the automatic semicolon scanner saw a comment) and not result in a decision on semicolon-legality in particularly the case where only a single-line-block-comment is seen.
It does this by
scan_whitespace_and_comments
to have an enum return value to indicate its three results (no-decision, reject, accept)scan_whitespace_and_comments
a consume:false mode where it stops as soon as it pops out of (a series of) block commentsAlso added tests to the corpus