0

我想创建全局变量并确保它们不会与现有的全局变量名称冲突。

替换所有数组声明的示例:

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)
      }
    }
  }
}
4

1 回答 1

0

在babel 手册中找到了答案:

const newUniqueName = path.scope.generateUidIdentifier('a')
于 2021-07-25T12:21:14.733 回答