TypeError when using rest operator in function parameter destructuring
llamadeus opened this issue · 3 comments
I encountered an error when using the rest operator when destructuring a function parameter. This is the error thrown:
TypeError: Cannot read property 'type' of undefined
at Object.Property (/Users/anonymous/Code/test/node_modules/typhonjs-escomplex-commons/dist/utils/ast/astSyntax.js:807:30)
at ObjectPattern (/Users/anonymous/Code/test/node_modules/typhonjs-escomplex-commons/dist/utils/ast/astSyntax.js:829:18)
at Function.parse (/Users/anonymous/Code/test/node_modules/typhonjs-escomplex-commons/dist/utils/ast/ASTGenerator.js:66:39)
at /Users/anonymous/Code/test/node_modules/escomplex-plugin-syntax-estree/dist/PluginSyntaxESTree.js:491:61
at Array.forEach (<anonymous>)
at Trait._data (/Users/anonymous/Code/test/node_modules/escomplex-plugin-syntax-estree/dist/PluginSyntaxESTree.js:490:28)
at Trait.valueOf (/Users/anonymous/Code/test/node_modules/typhonjs-escomplex-commons/dist/module/traits/Trait.js:86:60)
at Object.enterNode (/Users/anonymous/Code/test/node_modules/typhonjs-escomplex-module/dist/ESComplexModule.js:114:53)
at ASTWalker._visitNode (/Users/anonymous/Code/test/node_modules/typhonjs-ast-walker/dist/ASTWalker.js:72:88)
at ASTWalker.<anonymous> (/Users/anonymous/Code/test/node_modules/typhonjs-ast-walker/dist/ASTWalker.js:101:19)
The following code produces the error.
function printPersonInfo({ name, ...rest }) {
console.log(name);
console.log(rest);
}
printPersonInfo({
id: 1337,
name: 'Peter',
age: 42,
});
However, destructuring the parameter in the function body works.
function printPersonInfo(person) {
const { name, ...rest } = person;
console.log(name);
console.log(rest);
}
printPersonInfo({
id: 1337,
name: 'Peter',
age: 42,
});
Hi @lamadeus! Thanks for submitting this bug. It indeed was a problem and I fixed it and published typhonjs-escomplex-commons 0.1.1 which should be picked up automatically when you reinstall typhonjs-escomplex.
If you can verify that npm update
/ npm install
works that would be great. Otherwise
delete your node_modules directory, package-lock.json and re-run npm install. IE be mindful of package-lock.json as well as it might not pick up the bump from 0.1.0
to 0.1.1
. npm-update
should update it, but let me know what works for you.
Hi @typhonrt! Thank you so much for your fast fix and for this great package. I wasn't able to install the new version by running yarn upgrade
, however yarn remove typhonjs-escomplex && yarn add typhonjs-escomplex
worked.
Yeah. I think the package-lock.json aspect mentioned is particular to the dev environment. Excellent that it worked. I'm going to take another look at handling rest variable names better as currently things default to ...rest
and it's easily possible to get the actual function parameter / variable name.