0

I want to create global variables and be sure that they don't conflict with existing global varibale names.

Example with replacing all array declarations:

let arrayDeclarations

const customPlugin = () => {
  return {
    visitor: {
      Program(path) {
        arrayDeclarations = []
        path.node.body.unshift(t.variableDeclaration('const', arrayDeclarations))
      },
      ArrayExpression(path) {
        const newUniqueName = // TODO
        const arrayIdentifier = t.identifier(newUniqueName)
        arrayDeclarations.push(t.variableDeclarator(arrayIdentifier, path.node))
        path.replaceWith(arrayIdentifier)
      }
    }
  }
}
Alexander Danilov
  • 3,038
  • 1
  • 30
  • 35

1 Answers1

0

Found the answer in babel handbook:

const newUniqueName = path.scope.generateUidIdentifier('a')
Alexander Danilov
  • 3,038
  • 1
  • 30
  • 35