Skip to content

Commit 883ffe7

Browse files
notmeegorengnotmeegoreng
andauthored
Fix faulty regex for syntax highlighter (#1723)
* Fix faulty regex * fix regex properly, removing extra *s * add tests --------- Co-authored-by: notmeegoreng <terencegoh.y.c@gmail.com>
1 parent f6ea571 commit 883ffe7

File tree

2 files changed

+12
-3
lines changed

2 files changed

+12
-3
lines changed

src/__tests__/mode.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ test('constants are not correctly loaded', () => {
7979
})
8080

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

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

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

9292
const token3 = session.getTokenAt(3, 5)
9393
expect(expectedBool(token3, CATEGORY.forbidden)).toBe(true)
94+
95+
const token4 = session.getTokenAt(4, 1)
96+
expect(expectedBool(token4, CATEGORY.number)).toBe(true)
97+
98+
const token5 = session.getTokenAt(4, 9)
99+
expect(expectedBool(token5, CATEGORY.forbidden)).toBe(true)
100+
101+
const token6 = session.getTokenAt(4, 10)
102+
expect(expectedBool(token6, CATEGORY.number)).toBe(true)
94103
})
95104

96105
test('forbidden keywords', () => {

src/editors/ace/modes/source.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -113,9 +113,9 @@ export function HighlightRulesSelector(
113113
const VariantForbiddenRegexSelector = () => {
114114
if (variant === Variant.TYPED) {
115115
// Removes the part of the regex that highlights singular |, since Typed variant uses union types
116-
return /\.{3}|--+|\+\++|\^|(==|!=)[^=]|[$%&*+\-~\/^]=+|[^&]*&[^&]/
116+
return /\.{3}|--+|\+\++|\^|(==|!=)[^=]|[$%&*+\-~\/^]=+|(?<!&)&(?!&)/
117117
}
118-
return /\.{3}|--+|\+\++|\^|(==|!=)[^=]|[$%&*+\-~\/^]=+|[^&]*&[^&]|[^\|]*\|[^\|]/
118+
return /\.{3}|--+|\+\++|\^|(==|!=)[^=]|[$%&*+\-~\/^]=+|(?<!&)&(?!&)|(?<!\|)\|(?!\|)/
119119
}
120120

121121
// @ts-ignore

0 commit comments

Comments
 (0)