Created
July 10, 2019 15:15
-
-
Save jsoverson/aded8a53f1ea1540da3289c1d31ac2e6 to your computer and use it in GitHub Desktop.
Hacking JS with JS medium post
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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