Skip to content

Instantly share code, notes, and snippets.

@jsoverson
Created July 10, 2019 15:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jsoverson/aded8a53f1ea1540da3289c1d31ac2e6 to your computer and use it in GitHub Desktop.
Save jsoverson/aded8a53f1ea1540da3289c1d31ac2e6 to your computer and use it in GitHub Desktop.
Hacking JS with JS medium post
const { traverse } = require("shift-traverser");
module.exports = function(ast) {
const ids = [];
traverse(ast, {
enter(node, parent) {
if (node.type === "VariableDeclarator") {
if (node.binding.type === "ObjectBinding") {
node.binding.properties.forEach(prop => ids.push(prop.binding.name));
if (node.binding.rest) ids.push(node.binding.rest.name);
} else if (node.binding.type === "ArrayBinding") {
node.binding.elements.forEach(el => ids.push(el.name));
if (node.binding.rest) ids.push(node.binding.rest.name);
} else {
ids.push(node.binding.name);
}
}
}
});
return ids;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment