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

const log = t => (name, args) => {
  const memberExpression = t.memberExpression(
    t.identifier('console'), 
    t.identifier('log')
  )

  const theArguments = [t.StringLiteral(name)].concat(
    args != null 
    	? args.map(arg => t.identifier(arg))
    	: [t.identifier('arguments')]
  )
  const expressionStatement = t.expressionStatement(
    t.callExpression(memberExpression, theArguments)
  )
  return expressionStatement
}

export default function (babel) {
  const { types: t } = babel;
  
  return {
    visitor: {
      ArrowFunctionExpression (path) {
        if (path.node.body.type !== 'BlockStatement') {
          const block = t.blockStatement([t.returnStatement(path.node.body)])
          path.node.body = block
        }
        
        const consoleLog = log(t)(
          path.parent.type === 'VariableDeclarator' ? path.parent.id.name : 'anonymous', 
          path.node.params.map(x => x.name)
        )
        path.node.body.body.unshift(consoleLog)
      },
      
      FunctionDeclaration (path) {
        const consoleLog = log(t)(
          path.node.id.name
        )
        path.node.body.body.unshift(consoleLog)
      },
      
      FunctionExpression (path) {
        const consoleLog = log(t)(
          path.node.id 
          	? path.node.id.name
            : (path.parent.type === 'VariableDeclarator' ? path.parent.id.name : 'anonymous')
        )
        path.node.body.body.unshift(consoleLog)
      }
    }
  };
}

@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