gandm / language-babel

ES2017, flow, React JSX and GraphQL grammar and transpilation for ATOM
https://atom.io/packages/language-babel
MIT License
476 stars 83 forks source link

Function highlight has a bug when parameters default value are "(" or ")" #428

Closed belchior closed 7 years ago

belchior commented 7 years ago
const functionHighlight = (param = 'A') => {};

const variableHighlight = (param = '(') => {};

const variableHighlight = (param = ')') => {};

package version: language-babel 2.75.1

gandm commented 7 years ago

Yes I'm aware of this issue but it is difficult to fix without making a regex impossibly difficult. Currently the regex that parses lines of this type is

\s*+(\basync\b)?\s*+(?=(<(?:(?>[^<>]+)|\g<-1>)*>)?\s*+(\((?:(?>[^()]+)|\g<-1>)*\))\s*+(((?::(?:(\s*+(&|\|)?(\s*+((?:[$_\p{L}\p{Nl}]|\\u\h{4}|\\u{\h+})(?:[$_\p{L}\p{Mn}\p{Mc}\p{Nd}\p{Nl}\p{Pc}]|\\u\h{4}|\\u{\h+})*+)(<(?:(?>[^<>]+)|\g<-1>)*>)?|\s*+({(?:(?>[^{}]+)|\g<-1>)*\})|\s*+(\[(?:(?>[^\[\]]+)|\g<-1>)*\])|\s*+(\s*(["']).*?\k<-1>(?<!\\.))|\s*[x0-9A-Fa-f]+))|(?:\s*+/\*.*?\*/))*+)*)|(?:/\*.*?\*/))\s*=>)

in particular a bit of the above is (\((?:(?>[^()]+)|\g<-1>)*\) which parses any balanced brackets that might appear prior to an arrow. I realised that any unbalanced parens inside a string might upset this but as the only purpose of the above if to change the scopename of the variable to indicate it holds a function it doesn't seem too worthwhile changing it. The whole regex is quite complex because of the various syntax items that flow might introduce.

gandm commented 7 years ago

Note to self (\((?>[^()"']|("(\\.|[^"])*+")++|('(\\.|[^'])*+')++|\g<-5>)*+\))