Search code examples
babeljsabstract-syntax-tree

How to find variable declarator for an identifier using Babel?


Suppose I have the following piece of code and wish to find the variable declaration for the needle identifier in the foo(needle) call expression:

import { foo } from 'bar'

const needle = 'bad'

function deep () {
  let needle = 0
  needle = 1

  function deeper () {
    foo(needle)
  }
}

What would I need to use with Babel to write something robust that could try to resolve the last value assigned to needle, if a variable declaration for it exists?


Solution

  • path.scope.getBinding('needle')

    path.scope.getAllBindings() for all.