Skip to content

Instantly share code, notes, and snippets.

@chrisdickinson
Created March 15, 2013 16:54
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 chrisdickinson/4193ef1ba35a16d0963b to your computer and use it in GitHub Desktop.
Save chrisdickinson/4193ef1ba35a16d0963b to your computer and use it in GitHub Desktop.
module.exports = {
':root > expr > call > function': rewrite_function
, 'call:contains(isInstance) > lookup + * + *': rewrite_isinstance
, 'call:contains(fail) > lookup + *': rewrite_fail_call
, ':root > expr > assign > lookup + call:contains(platoon) > function > block > expr:first-child': annotate_name
, ':root > expr > assign > lookup + call:contains(platoon) > function:last-child': rewrite_exports
, 'call:contains(async) > function:last-child': rewrite_async
}
function annotate_name(node) {
node.update('')
}
function rewrite_exports(node) {
var assign = node.parent.parent
, platoon = assign.right.arguments.slice(1)
node.parent.parent.parent.update(
platoon.map(function(subnode) {
var body = subnode.body.body
, val = JSON.stringify(body[0].expression.value)
subnode.body.body = subnode.body.body.slice(1)
return 'test('+val+', '+subnode.source()+'\n)'
}).join('\n\n')
)
return false
}
function rewrite_async(node) {
node.parent.update(node.source())
}
function rewrite_function(node) {
node.update('mocktimeout('+node.source()+')')
}
function rewrite_fail_call(node) {
node.parent.callee.property.update('ok')
node.update('!('+node.source()+')')
}
function rewrite_isinstance(node) {
node.parent.callee.property.update('ok')
var obj = node.parent.arguments[0].source()
, inst = node.parent.arguments[1].source()
node.parent.update(node.parent.callee.source() + '(' + obj + ' instanceof ' + inst + ')')
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment