Home Download Buy Blog Forum Support

JavaScript syntax highlight quirk (with fix)

JavaScript syntax highlight quirk (with fix)

Postby zertosh on Sun Jun 24, 2012 4:23 pm

Some of the color schemes (like Monokai) will highlight function arguments. However, when an anonymous function is used as an argument, the anonymous function's argument will not be highlighted unless there is a space between the function keyword and the parentheses. Example:

Code: Select all
// argument el should be highlighted but it isn't
Array.prototype.slice.call(arguments).forEach(function(el)  {
   //stuff
});

// argument el is highlighted
// notice the space between function and (el)
Array.prototype.slice.call(arguments).forEach(function (el)  {
   //stuff
});


The bug seems to be in the regex to match regular functions. In JavaScript.tmLanguage line 260, you have:
Code: Select all
<string>\b(function)\s+([a-zA-Z_$]\w*)?\s*(\()(.*?)(\))</string>

but it should be
Code: Select all
<string>\b(function)\s*([a-zA-Z_$]\w*)?\s*(\()(.*?)(\))</string>


Hope this helps someone!
zertosh
 
Posts: 2
Joined: Sun Jun 24, 2012 4:05 pm

Re: JavaScript syntax highlight quirk (with fix)

Postby agibsonsw on Sun Jun 24, 2012 4:31 pm

It is a coding convention that there should be a space following the word function (for anonymous functions). So I thought the regex expression may even have been intentional.
"I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.
agibsonsw
 
Posts: 901
Joined: Fri Jan 27, 2012 9:11 pm

Re: JavaScript syntax highlight quirk (with fix)

Postby zertosh on Sun Jun 24, 2012 4:38 pm

Maybe it is convention, but when you're using an anonymous function in a function expression, the arguments are highlighted with or without a space after the function keyword:

Code: Select all
// min and max are highlighted with a space after function
Math.rand = function ( min, max ) {
   return Math.floor(Math.random() * (max - min + 1) + min);
}

//min and max are highlighted without space after function
Math.rand = function( min, max ) {
   return Math.floor(Math.random() * (max - min + 1) + min);
}


So if the regex was done intentionally for convention, then it's not being used consistently.
zertosh
 
Posts: 2
Joined: Sun Jun 24, 2012 4:05 pm

Re: JavaScript syntax highlight quirk (with fix)

Postby agibsonsw on Sun Jun 24, 2012 5:02 pm

@zertosh. K. Thanks for the fix. Andy.
"I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.
agibsonsw
 
Posts: 901
Joined: Fri Jan 27, 2012 9:11 pm


Return to General Discussion

Who is online

Users browsing this forum: No registered users and 34 guests