Skip to content

Need help performing code transformations #11736

Answered by mischnic
HannahBako asked this question in Q&A
Discussion options

You must be logged in to vote

So you want to transform CallExpression(...) into
CallExpression(MemberExpression(OldCallExpression, 'on'), ...args)

You need to wrap the existing CallExpression into a MemberExpression and a new CallExpression with replaceWith:

export default function (babel) {
  const { types: t } = babel;
  return {
    name: "ast-transform", // not required
    visitor: {
      CallExpression: {
        exit(path) {
          if (path.node.callee.type === "MemberExpression") {
            if (path.node.callee.property.name == "attr") {
              if (path.node.arguments[0].value == "height") {
                console.log(path.node.arguments[1].body);
                path.replaceWith(

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@HannahBako
Comment options

Answer selected by JLHwung
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants