Skip to content

Commit

Permalink
Fix faulty regex for syntax highlighter (#1723)
Browse files Browse the repository at this point in the history
* Fix faulty regex

* fix regex properly, removing extra *s

* add tests

---------

Co-authored-by: notmeegoreng <[email protected]>
  • Loading branch information
notmeegoreng and notmeegoreng authored Oct 2, 2024
1 parent f6ea571 commit 883ffe7
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 3 deletions.
11 changes: 10 additions & 1 deletion src/__tests__/mode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ test('constants are not correctly loaded', () => {
})

test('operator syntax type error', () => {
const code = 'const num = 3; \nnum++; \nnum--; \nnum += 1;'
const code = 'const num = 3; \nnum++; \nnum--; \nnum += 1; \n5 + num |2;'

setSession(Chapter.SOURCE_1, defaultVariant, defaultExternal, code)

Expand All @@ -91,6 +91,15 @@ test('operator syntax type error', () => {

const token3 = session.getTokenAt(3, 5)
expect(expectedBool(token3, CATEGORY.forbidden)).toBe(true)

const token4 = session.getTokenAt(4, 1)
expect(expectedBool(token4, CATEGORY.number)).toBe(true)

const token5 = session.getTokenAt(4, 9)
expect(expectedBool(token5, CATEGORY.forbidden)).toBe(true)

const token6 = session.getTokenAt(4, 10)
expect(expectedBool(token6, CATEGORY.number)).toBe(true)
})

test('forbidden keywords', () => {
Expand Down
4 changes: 2 additions & 2 deletions src/editors/ace/modes/source.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,9 +113,9 @@ export function HighlightRulesSelector(
const VariantForbiddenRegexSelector = () => {
if (variant === Variant.TYPED) {
// Removes the part of the regex that highlights singular |, since Typed variant uses union types
return /\.{3}|--+|\+\++|\^|(==|!=)[^=]|[$%&*+\-~\/^]=+|[^&]*&[^&]/
return /\.{3}|--+|\+\++|\^|(==|!=)[^=]|[$%&*+\-~\/^]=+|(?<!&)&(?!&)/
}
return /\.{3}|--+|\+\++|\^|(==|!=)[^=]|[$%&*+\-~\/^]=+|[^&]*&[^&]|[^\|]*\|[^\|]/
return /\.{3}|--+|\+\++|\^|(==|!=)[^=]|[$%&*+\-~\/^]=+|(?<!&)&(?!&)|(?<!\|)\|(?!\|)/
}

// @ts-ignore
Expand Down

0 comments on commit 883ffe7

Please sign in to comment.