Skip to content

Instantly share code, notes, and snippets.

@xaviervia
Last active February 5, 2017 23:43
Show Gist options
  • Save xaviervia/6cf7937b5a20d66203706a02f5379a14 to your computer and use it in GitHub Desktop.
Save xaviervia/6cf7937b5a20d66203706a02f5379a14 to your computer and use it in GitHub Desktop.
babel transform to trace function calls, try 1
  • Open https://astexplorer.net/
  • Set it to JavaScript if it's not
  • Set it to babylon6
  • Paste the source.js code in the top left pane
  • In the "Transform" menu on the top select babel6
  • Copy the transform.js code in the bottom left page
function hola (a, b) {
console.log("called: hola", arguments);
return `${a} ${b}`
}
hola('hello', 'world')
function hola (a, b) {
return `${a} ${b}`
}
hola('hello', 'world')
export default function (babel) {
const { types: t } = babel;
return {
visitor: {
FunctionDeclaration(path) {
const memberExpression = t.memberExpression(t.identifier('console'), t.identifier('log'))
const theArguments = [t.stringLiteral(`called: ${path.node.id.name}`), t.identifier('arguments')]
const expressionStatement = t.expressionStatement(t.callExpression(memberExpression, theArguments))
path.node.body.body.unshift(expressionStatement)
}
}
};
}
@xaviervia
Copy link
Author

try with:

(a, b) => `${a} ${b}`

const a2 = (c, d) => { 
  return `${c}${d}` 
}

function hola (z, b) {
  return z + b
}

(function hola (y, b) {
})

(function (y, b) {
})

const named = function (e, y) {
}

hola('hello', 'world')

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment