Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix faulty regex for syntax highlighter #1723

Merged
merged 3 commits into from
Oct 2, 2024

Conversation

notmeegoreng
Copy link
Contributor

@notmeegoreng notmeegoreng commented Sep 14, 2024

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

  • the end of a single numeral literal at the start of the line o,
  • or the latest occurrence of the character doubled (| => ||, & => &&),
  • otherwise the start of the line,

it highlights the entire line up to the | and one character after in pink, the colour used for new and with.

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:

return /\.{3}|--+|\+\++|\^|(==|!=)[^=]|[$%&*+\-~\/^]=+|[^&]*&[^&]/
}
return /\.{3}|--+|\+\++|\^|(==|!=)[^=]|[$%&*+\-~\/^]=+|[^&]*&[^&]|[^\|]*\|[^\|]/

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

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • Documentation update
  • Code quality improvements

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

  • I have tested this code

@coveralls
Copy link

Pull Request Test Coverage Report for Build 10862938302

Details

  • 1 of 2 (50.0%) changed or added relevant lines in 1 file are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage remained the same at 81.61%

Changes Missing Coverage Covered Lines Changed/Added Lines %
src/editors/ace/modes/source.ts 1 2 50.0%
Totals Coverage Status
Change from base Build 10841419701: 0.0%
Covered Lines: 10983
Relevant Lines: 13081

💛 - Coveralls

Copy link
Member

@s-kybound s-kybound left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me, tested the regex online.

@s-kybound s-kybound merged commit 883ffe7 into source-academy:master Oct 2, 2024
2 checks passed
s-kybound added a commit that referenced this pull request Oct 2, 2024
@notmeegoreng notmeegoreng deleted the patch-1 branch October 4, 2024 03:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants