babel-utils

Creates as visitor that calls a handler callback for each import declaration that matches the supplied package.

Usage no npm install needed!

<script type="module">
  import babelUtils from 'https://cdn.skypack.dev/babel-utils';
</script>

README

babel-utils

getImportDeclarationVisitor(packageName, handler)

Creates as visitor that calls a handler callback for each import declaration that matches the supplied package.

visitNamedImports(importPath, moduleName, hander)

Given an import path declaration node, calls a handler callback for each reference to the supplied named modules.

Suppose we want to transform usages of foo from some-package.

import {foo as bar} from "some-package";

bar({});
const visitor = getImportDeclarationVisitor("some-package", path => {
  visitNamedImports(path, "foo", paths => {
    paths.forEach(path => {
      // ...
    });
  });
});

resolveToValue(path)

Takes an identifier path and recursively traverses the scope (through variable declarations) until it finds a non-variable declaration.

const foo = {foo: "foo"};
const bar = foo;
const baz = bar;

someFunction(baz);

Suppose we want to transform the argument of someFunction. resolveToValue will yield the object expression (assigned to foo) given the identifier node path passed to someFunction.