【问题标题】:Create unique identifier in babel plugin在 babel 插件中创建唯一标识符
【发布时间】:2021-10-01 16:34:20
【问题描述】:

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

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

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

【问题讨论】:

    标签: babeljs babel-plugin


    【解决方案1】:

    babel handbook找到答案:

    const newUniqueName = path.scope.generateUidIdentifier('a')
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-06-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-08-25
      • 2020-07-15
      相关资源
      最近更新 更多