Description
Describe the bug
It appears Ace's JS highlight mode has trouble correctly highlighting the arrow function arguments in the following pieces of code:
// works as expected
func(a,b,1,2,3)
func(a,b,innerCall(c,d))
(a=1,b=2) => {}
function* generator() {}
// does not work
func(a,b,1,2,3, () => {})
func(a,b,innerCall(c,d), () => {})
(a=(1),b=(2)) => {}
function *generator() {}
function*generator() {}
Expected Behavior
I would expect a
, b
, and innerCall
to be marked with the class ace_identifier
since they are variable names, not argument names.
I would also expect b
in the arrow function to appear as an argument name (css classes ace_variable ace_parameter
), and not an identifier.
I would also expect the generator
function to highlight as a function in all cases.
Current Behavior
Instead, the highlighting is not correct:
Reproduction Steps
Using the Ace editor, simply paste the code:
// works as expected
func(a,b,1,2,3)
func(a,b,innerCall(c,d))
(a=1,b=2) => {}
function* generator() {}
// does not work
func(a,b,1,2,3, () => {})
func(a,b,innerCall(c,d), () => {})
(a=(1),b=(2)) => {}
function *generator() {}
function*generator() {}
Possible Solution
This is, of course, just an issue with the javascript_highlight_rules. It's located here:
https://github.com/ajaxorg/ace/blob/master/src/mode/javascript_highlight_rules.js#L420
It appears the rule here is too greedy, grabbing anything after a (
character and processing it as function arguments. This, I understand, is because arrow function arguments can have default values which contain parentheses. To fix it would potentially require finding the beginning parenthesis matching the one before the arrow =>
, although I can't say what that looks like.
The reason b
in the arrow function is not highlighted as an argument, though, is actually because the default_parameter
mode, when it fails to match, reverts back to no_regex
(the default mode) which never returns to function_arguments
:
https://github.com/ajaxorg/ace/blob/master/src/mode/javascript_highlight_rules.js#L324
This is affecting all function-types, not just arrow functions, e.g., function func(a=(1),b=(2)) {}
. Does Ace's highlight parser support a mode stack yet? e.g., a mode can be entered, but when leaving it, it falls back to the 'previous' mode instead of always going to a specific one. I don't see a way around this issue until a mode stack is supported. (I also always found this lack rather frustrating when implementing my own syntaxes)
Lastly, please allow whitespace between function
and *
for generator functions, and no whitespace between the *
and the function name, both of which are valid in javascript:
https://github.com/ajaxorg/ace/blob/master/src/mode/javascript_highlight_rules.js#L124 (there are many other occurrences)
This isn't as hard as the above issues, and could easily be solved with a regex piece like,
"(function\\s*(?:\\*|(?=\\s)))(\\s*)" // always using \\s* at the end, vs. the \\s+ in some places
Additional Information/Context
No response
Ace Version / Browser / OS / Keyboard layout
Ace v1.23.4 / All Browsers / All OSes / Not applicable