String default value in arrow function breaks highlighting

See the reproduction here:

Variables are not properly highlighted as defined.

1 Like

This oneā€™s an upstream bug that is present in CodeMirror. Unfortunately, because arrow functions are very context-dependent (because (foo, bar='default') is a valid javascript expression and we only know that itā€™s a parameter list after encountering =>, and CodeMirrorā€™s syntax highlighting system is very context-independent, Iā€™m not sure that thereā€™s a clean fix before CodeMirror 6.

1 Like

Why does it work with a number then?

I havenā€™t tested this (so take this with a grain of salt), but I suspect this line in CodeMirrorā€™s JS mode is causing the arrow-function detection to fail. It looks like it was added in response to this issue.

EDIT: As evidence for this, note that using backticks around your default string argument causes syntax highlighting to work again. Try pasting the following into a notebook:

test4 = (foo, bar = `string`) => [foo, bar]
3 Likes

Reported to CodeMirror:

1 Like

This was fixed in codemirror/CodeMirror@4c30e11. It looks like that commit hasnā€™t yet been published to npm yet, but you should be able to install from the master branch on GitHub to get the fix.

1 Like

Tom backported the CodeMirror fix, so this ought to be highlighting correctly now.

Thanks, Tom!

2 Likes

Awesome. Thank you!