Babel transform, the value of the parent variable

Suppose I have such Javascript code

import someFunc from 'somewhere';
const prefix = 'Planet';
someFunc(prefix + 'Earth');

If I use babeljs in my project, I can write a simple conversion plugin

export default function({types: t}) {
  return {
    visitor: {
      CallExpression: {
        enter(path, {opts, file}) {
          if (path.node.callee.name !== 'someFunc') {
            return;
          }
          <...>
        }
      }
    }
  }
}

How can I take the value of the "prefix" variable inside the plugin or evaluate the full BinaryExpression expression to replace it with the result string to get this?

import someFunc from 'somewhere';
someFunc('PlanetEarth');
+4
source share

All Articles