tunnckoCore/parse-function

ES6 features

skamenetskiy opened this issue · 7 comments

Hey, I'm trying to use some ES6 standards, which should be supported according to the readme.

const parseFunction = require('parse-function');

function a({
    a = null,
    b = null,
    c = null
}) {
    console.log('a', a, b, c);
}

const b = ({a=null, b=null, c}, d, {e=null, m=12} = {}, f) => {
    console.log('b', a, b, c);
};

const c = () => {
    console.log('c');
};

function d() {
    console.log('d');
}

function e(a) {
    console.log('e', a);
}

console.log(parseFunction(a));
console.log(parseFunction(b));
console.log(parseFunction(c));
console.log(parseFunction(d));
console.log(parseFunction(e));

Hi, I'm not sure the readme promises to support object destructuring. It says this:
full support of es6 arrow functions and es6 default params

And indeed, the following seems to work (though it does not "save" the default value anywhere):

const f = (a = 1) => {
    console.log('f', a);
}
console.log(parseFunction(f));

Yep. Need more work, it is just thin layer on top of acorn ast, so PRs welcome. Just need more ifs ;d

Also, I'm not sure what should be the response for your examples, @skamenetskiy.

@rkaw92, @skamenetskiy hi there.

Last few weeks i'm digging more and more on parsers, bundling and etc stuff and now i'm lookin into babylon ast, so I can improve that things.

const babylon = require('babylon')
const ast = babylon.parse(`function myFn (a = {aaa: {bea: 'c'}}, b = 123, ...rest) {
  return [a + b, rest]
}`)
console.log(ast.program.body[0].params)
// if AssignmentPattern then we will just get `.left` 

Actually, no, won't do anything. It totally not make any sense to have support for destructing. Default arguments make sense, because they have names which you can point to.

That library has one main purpose, stay as small and simple as possible. Super useful for dependency injection and etc stuff. If the user won't the cost of full using source code parsers like acorn and babylon, then so they can use function-arguments if their use case is DI.

So this is invalid for me

// not ok
function a({
    a = null,
    b = null,
    c = null
}) {
    console.log('a', a, b, c);
}

and won't be supported.

As about mixing default arguments and destructing...
Consider the following (edited from your snippets)

const bear = ({a=null, b=null, c}, def = 33, {e=null, m=12} = {}, foo, qux, ...rest) => {
    console.log('b', a, b, c)
}

const parseFunction = require('parse-function')
const parsed = parseFunction(bear)

console.log(parsed.args) // => [false, 'def', false, 'foo', 'qux', 'rest']
console.log(parsed.defaults) // => {def: 33, foo: undefined, qux: undefined, rest: undefined}

I'm going to close this one and update the package. Possibly will publish v3 using babylon or latest acorn

@skamenetskiy v4 is on the master. You could do what you want now, because we have plugins api and relies on Babylon by default (but can be switched). :) Feedback welcome :)