Skip to content

Commit 4f5b769

Browse files
authored
fix(js/ts) prevent $[number] variables as false positives as numeric (#3648)
1 parent e0f7577 commit 4f5b769

4 files changed

Lines changed: 48 additions & 0 deletions

File tree

CHANGES.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ Grammars:
1717
- enh(cmake) support bracket comments [Hirse][]
1818
- enh(java) add yield keyword to java [MBoegers][]
1919
- enh(java) add permits keyword to java [MBoegers][]
20+
- fix(javascript/typescript) correct identifier matching when using numbers [Lachlan Heywood][]
2021

2122
[Josh Goebel]: https://github.com/joshgoebel
2223
[Josh Temple]: https://github.com/joshtemple
@@ -26,6 +27,7 @@ Grammars:
2627
[Hirse]: https://github.com/Hirse
2728
[The Flix Organisation]: https://github.com/flix
2829
[MBoegers]: https://github.com/MBoegers
30+
[Lachlan Heywood]: https://github.com/lachieh
2931

3032
## Version 11.6.0
3133

src/languages/javascript.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,8 @@ export default function(hljs) {
220220
HTML_TEMPLATE,
221221
CSS_TEMPLATE,
222222
TEMPLATE_STRING,
223+
// Skip numbers when they are part of a variable name
224+
{ match: /\$\d+/ },
223225
NUMBER,
224226
// This is intentional:
225227
// See https://github.com/highlightjs/highlight.js/issues/3288
@@ -453,6 +455,8 @@ export default function(hljs) {
453455
CSS_TEMPLATE,
454456
TEMPLATE_STRING,
455457
COMMENT,
458+
// Skip numbers when they are part of a variable name
459+
{ match: /\$\d+/ },
456460
NUMBER,
457461
CLASS_REFERENCE,
458462
{

test/markup/javascript/numbers.expect.txt

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,3 +75,24 @@ x0.<span class="hljs-property">e1</span>
7575
0b1e1
7676
0N
7777
<span class="hljs-number">00</span>.
78+
79+
<span class="hljs-comment">// numbers in identifiers</span>
80+
<span class="hljs-keyword">const</span> num = $1
81+
<span class="hljs-keyword">const</span> num = $0n9
82+
<span class="hljs-keyword">const</span> num = $abc012
83+
<span class="hljs-keyword">const</span> num = $0x9
84+
<span class="hljs-keyword">const</span> num = $0o0
85+
<span class="hljs-keyword">const</span> num = $09
86+
<span class="hljs-keyword">const</span> num = $9<span class="hljs-number">.09</span>
87+
<span class="hljs-keyword">const</span> num = $a9
88+
<span class="hljs-keyword">const</span> num = <span class="hljs-number">1</span>
89+
<span class="hljs-keyword">const</span> num = 0n9
90+
<span class="hljs-keyword">const</span> num = <span class="hljs-number">0x9</span>
91+
<span class="hljs-keyword">const</span> num = <span class="hljs-number">0o0</span>
92+
<span class="hljs-keyword">const</span> num = <span class="hljs-number">09</span>
93+
<span class="hljs-keyword">const</span> num = <span class="hljs-number">9.09</span>
94+
<span class="hljs-keyword">const</span> num = a9
95+
96+
<span class="hljs-comment">// numbers in identifiers in template strings</span>
97+
cosnt string = <span class="hljs-string">`<span class="hljs-subst">${$0}</span>`</span>
98+
cosnt string = <span class="hljs-string">`<span class="hljs-subst">${<span class="hljs-number">0</span>}</span>`</span>

test/markup/javascript/numbers.txt

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,3 +75,24 @@ x0.e1
7575
0b1e1
7676
0N
7777
00.
78+
79+
// numbers in identifiers
80+
const num = $1
81+
const num = $0n9
82+
const num = $abc012
83+
const num = $0x9
84+
const num = $0o0
85+
const num = $09
86+
const num = $9.09
87+
const num = $a9
88+
const num = 1
89+
const num = 0n9
90+
const num = 0x9
91+
const num = 0o0
92+
const num = 09
93+
const num = 9.09
94+
const num = a9
95+
96+
// numbers in identifiers in template strings
97+
cosnt string = `${$0}`
98+
cosnt string = `${0}`

0 commit comments

Comments
 (0)