regexhq/function-regex

ES6 support

Closed this issue · 7 comments

Various ES6 features are unsupported e.g.:

test

var re = require('function-regex')();

var str1 = "function foo ({ bar, baz }) { }";
var str2 = "function foo ([ bar, baz ]) { }";
var str3 = "function foo (bar = 42) { }";
var str4 = "function foo (...bar) { }";

console.log(re.exec(str1));
console.log(re.exec(str2));
console.log(re.exec(str3));
console.log(re.exec(str4));

output

null
null
null
null

There's also function* (ES6) and async function (ES7).

IMHO this qualifies for a different regex. like es6-function-regex

technically this shouldn't even be called "function-regex" since it's not. It should be more specific, like: https://github.com/jonschlinkert/parse-code-context/blob/master/index.js#L14-L33

IMHO this qualifies for a different regex. like es6-function-regex

I agree that this is probably out of scope for this package.

But, come to think of it, a regex will never be able to handle destructured parameters in the general case, since regular languages don't support balanced delimiters.

https://github.com/jonschlinkert/parse-code-context/blob/master/index.js#L14-L33

FYI: that regex shares several issues with this regex and adds at least one new issue e.g. it doesn't handle:

function  foo () {} // > 1 space

or:

function // newline
foo () {}

Yea, I think it should go to something like es6-function-regex because we should also handle arrow functions?

Closing this, since — as mentioned — a regex can't handle all ES6 function syntax (e.g. destructuring).