Destructuring
Opened this issue ยท 11 comments
Yo, how hard would it be to make this package understand destructuring?
({ foo, bar = 5 }) => nullWanted to ask before trying to do it myself.
I think it wouldn't be easy. Or if it is easy, it is very strange case and may lead to more things. Once i asked, one should be the expected result of such arguments?
But yea, in any way, it's cool because we support plugins, so i don't think it would be needed to include it by default. If it isn't something big - yes, otherwise no.
Can't answer, didn't see or remember what the AST would be in destructuring case.
I was able to make it work the way I expected with a few lines of code:
function parseParam(param) {
switch (param.type) {
case 'ArrayPattern':
return param.elements.map(parseParam);
case 'ObjectPattern':
return param.properties.reduce((o, property) => {
let value;
if (property.value.type === 'Identifier') {
value = true;
} else {
value = parseParam(property.value);
}
const { name } = property.key;
o[name] = value;
return o;
}, {});
case 'Identifier':
return param.name;
default:
throw new TypeError(`${param.type} is not a recognized "type"`);
}
}
parse.use(app => (node, result) => {
node.params.forEach((param, i) => {
result.args[i] = parseParam(param);
});
return result;
})Feel free to merge upstream if you would like.
Sweeeet! :) You can release it as plugin now, so we can add it to the readme ๐
@TooTallNate, btw, it seems that it would override existing regular argument with same name?
For example
const foo = (foo, { bar, abc = 123, foo = 5 }) => {}I don't think so because each argument is it's own array entry. So in your example result.args would be ['foo', { bar: true, abc: 123, foo: 5 }]
Oh yea, really, mislooked the things.
Hey @TooTallNate, do you mind adding a PR with it? Next release (#138) can just include it. So we can add it to bullets of default supported features :P
Sweet, looks even more good.
@olstenlarck anything wanna do at pr? (but not include make test, but can see here)
because i can't run build this repo at local (also when no any my commit)