Report unterminated construct errors at opening token #3827
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.
👋 We're working on integrating Prism into Sorbet and I would like to propose an improvement to how unterminated construct errors are reported to end users.
This PR adds an
expect1_openingfunction that expects a token and attaches the error to the opening token location rather than the current position. This is useful for errors about missing closing tokens, where it could be useful to point to the line with the opening token rather than the end of the file.For example:
This would previously produce three identical errors at the end of the file. After this commit, they would be reported at the opening token location:
This way, end users can see which particular def node is considered unclosed, rather than a seeing a collection of errors all pointing to the end of the file. However, I understand this might be considered less technically correct and potentially less useful in certain situations. In the single-line test examples it might seem preferable to show the error at end-of-input, since that's where the closing token should be, but I think in most practical cases that won't be the case.
Alternative
An alternative approach might be to add "hints" to errors so the error location remains end-of-input but an additional hint location could point to the opening token. Clients could choose whether to surface the original error location, the hint location, or both. What do you think?
Tests
As part of this, I added a handful of tests capturing the original behavior which should make it easier to see the effect this proposed change has.