This repository was archived by the owner on Dec 15, 2022. It is now read-only.
Highlight invalid/illegal JSON syntax in tree-sitter grammar #78
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.
Requirements
Description of the Change
This PR adds simple highlighting of invalid/illegal markup in tree-sitter-parsed JSON files. This is similar to how the first-mate JSON grammar highlighted syntax errors in JSON files.
Current first-mate:

Current tree-sitter:

Proposed tree-sitter:

Alternate Designs
The first-mate grammar tokenizes different kinds of invalid syntax (e.g. missing comma vs. unterminated strings). I chose not to go this deep with my changes for the tree-sitter grammar here, since
'ERROR': 'invalid.illegal'covers the majority of cases pretty clearly.I could've also used a scope other than
invalid.illegal, though since I am not distinguishing between different types of errors per the above, I felt no need to qualify the scope any further.Benefits
It would be much more obvious when your JSON file contains invalid/illegal syntax, such as a missing comma or extraneous text.
Possible Drawbacks
Some people may find it distracting or difficult to read, especially if you are writing a JSON file by hand. This may be fixable with some modifications to my new grammar rules (either to scope the selectors more specifically, or to use regex). But the principle remains the same: since JSON and its ecosystem of parsers demand such strict syntax, I think the tradeoff is worth it to more easily catch syntax errors on the spot.
Applicable Issues
N/A