Fix faulty regex for syntax highlighter #1723
Merged
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.
Description
When a single | or single & (not || or &&) is used, not as the final character in the line nor in a multiline comment (but a single line comment still has the bug), the syntax highlighting fails.
Starting from
it highlights the entire line up to the | and one character after in pink, the colour used for
new
andwith
.Characters past one after the single | or & would be highlighted normally even if they are part of a comment.
We can observe this on the playground here: https://share.sourceacademy.nus.edu.sg/syntaxhighlightingbug
I identified the issue as a faulty regex here:
js-slang/src/editors/ace/modes/source.ts
Lines 116 to 118 in f6ea571
The cases for '|' and '&' capture other parts of the string in the attempt to exclude '||' and '&&'.
This PR has a fix for this, using lookahead and lookbehind assertions.
This PR also includes a few lines in the test for this file to cover the case of '|'.
Type of change
How to test
I expanded the existing tests for this file to cover this change.
https://github.com/notmeegoreng/js-slang/blob/patch-1/src/__tests__/mode.ts#L82
Checklist