regexhq / function-regex

Function regex. Regular expression for matching function parts. Expose match groups for function name, arguments and function body.
http://j.mp/1xV5fzk
MIT License
30 stars 4 forks source link

ES6 support #2

Closed chocolateboy closed 9 years ago

chocolateboy commented 9 years ago

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
chocolateboy commented 9 years ago

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

jonschlinkert commented 9 years ago

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

jonschlinkert commented 9 years ago

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

chocolateboy commented 9 years ago

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.

chocolateboy commented 9 years ago

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 () {}
tunnckoCore commented 9 years ago

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

chocolateboy commented 9 years ago

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