Skip to content

Commit 3efb708

Browse files
authored
fix: address duplicate token bug when syntax-highlighting
PR-URL: #2542 Reviewed-by: Athan Reines <kgryte@gmail.com> Signed-off-by: Snehil Shah <snehilshah.989@gmail.com>
1 parent fcc9603 commit 3efb708

File tree

2 files changed

+29
-5
lines changed

2 files changed

+29
-5
lines changed

lib/node_modules/@stdlib/repl/lib/syntax_highlighter.js

+27-3
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ var readline = require( 'readline' );
2626
var logger = require( 'debug' );
2727
var format = require( '@stdlib/string/format' );
2828
var setNonEnumerableReadOnly = require( '@stdlib/utils/define-nonenumerable-read-only-property' );
29+
var isEmptyArray = require( '@stdlib/assert/is-empty-array' );
2930
var objectKeys = require( '@stdlib/utils/keys' );
3031
var omit = require( '@stdlib/utils/omit' );
3132
var hasOwnProp = require( '@stdlib/assert/has-own-property' );
@@ -54,6 +55,26 @@ function tokenComparator( a, b ) {
5455
return a.start - b.start;
5556
}
5657

58+
/**
59+
* Removes duplicate tokens from a sorted tokens array.
60+
*
61+
* @private
62+
* @param {Array<Object>} tokens - sorted tokens array
63+
* @returns {Array<Object>} array with unique tokens
64+
*/
65+
function removeDuplicateTokens( tokens ) {
66+
var out = [];
67+
var i;
68+
69+
out.push( tokens[ 0 ] );
70+
for ( i = 1; i < tokens.length; i++ ) {
71+
if ( tokens[ i - 1 ].start !== tokens[ i ].start ) {
72+
out.push( tokens[ i ] );
73+
}
74+
}
75+
return out;
76+
}
77+
5778

5879
// MAIN //
5980

@@ -125,8 +146,7 @@ setNonEnumerableReadOnly( SyntaxHighlighter.prototype, '_highlightLine', functio
125146
var i;
126147
var j;
127148

128-
// Sort and traverse the tokens...
129-
tokens.sort( tokenComparator );
149+
// Traverse the tokens...
130150
for ( i = 0; i < tokens.length; i++ ) {
131151
token = tokens[ i ];
132152
color = theme[ token.type ];
@@ -309,11 +329,15 @@ setNonEnumerableReadOnly( SyntaxHighlighter.prototype, 'onKeypress', function on
309329
// Tokenize:
310330
debug( 'Line change detected. Tokenizing line: %s', this._rli.line );
311331
tokens = tokenizer( this._rli.line, this._repl._context );
312-
if ( !tokens ) {
332+
if ( isEmptyArray( tokens ) ) {
313333
debug( 'No tokens found. Skipping highlighting...' );
314334
this._multilineHandler.updateLine( this._rli.line ); // save displayed line
315335
return;
316336
}
337+
// Process tokens:
338+
tokens.sort( tokenComparator );
339+
tokens = removeDuplicateTokens( tokens );
340+
317341
// Highlight:
318342
debug( '%d tokens found. Highlighting...', tokens.length );
319343
this._line = this._rli.line; // updated line buffer

lib/node_modules/@stdlib/repl/test/integration/fixtures/syntax-highlighting/member_expressions.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
2-
"expression": "var a = { 'b': 'bar' }; var foo = { 'bar': { 'func': function() { return true; } } };\nfoo[a.b].func()",
3-
"expected": "\u001b[31mfoo\u001b[0m[\u001b[31ma\u001b[0m.\u001b[32mb\u001b[0m].\u001b[33mfunc\u001b[0m()",
2+
"expression": "var a = { 'b': { 'c': 'bar' } }; var foo = { 'bar': { 'func': function() { return true; } } };\nfoo[a.b.c].func()",
3+
"expected": "\u001b[31mfoo\u001b[0m[\u001b[31ma\u001b[0m.\u001b[31mb\u001b[0m.\u001b[32mc\u001b[0m].\u001b[33mfunc\u001b[0m()",
44
"theme": {
55
"object": "red",
66
"function": "yellow",

0 commit comments

Comments
 (0)