Skip to content

Instantly share code, notes, and snippets.

@mreinstein
Created February 18, 2020 10:40
Show Gist options
  • Save mreinstein/fcd4d05119d2065170f5913a2cf67794 to your computer and use it in GitHub Desktop.
Save mreinstein/fcd4d05119d2065170f5913a2cf67794 to your computer and use it in GitHub Desktop.
parse javascript from stdin as an AST and remove specific functions by name
import escodegen from 'escodegen'
import esprima from 'esprima'
import estraverse from 'estraverse'
let src = ''
process.stdin.on('data', function (chunk) {
src += chunk
})
process.stdin.on('end', function () {
let ast = esprima.parse(src)
ast = estraverse.replace(ast, {
enter: function (node) {
//if (node.type === 'Property' && (node.key.name === 'run') && node.value.type === 'FunctionExpression')
// this.remove()
if (node.type === 'Property' && node.method && node.key.name === 'preRunAction')
return this.remove()
if (node.type === 'Property' && node.method && node.key.name === 'postRunAction')
return this.remove()
if (node.type === 'Property' && node.method && node.key.name === 'run')
return this.remove()
}
})
console.log(escodegen.generate(ast))
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment