Skip to content

traverse - find all global references #11654

Discussion options

You must be logged in to vote

We have a ReferencedIdentifier "virtual" type: maybe this would help?

function findGlobals (ast) {
  const globals = new Map()
  traverse(ast, {
    // ReferencedIdentifier
    ReferencedIdentifier: (path) => {
      // skip if it refers to an existing variable
      const name = path.node.name
      if (path.scope.hasBinding(name, true)) return
      
      // check if arguments refers to a var, this shouldn't happen in strict mode
      if (name === 'arguments') {
        if (isInFunctionDeclaration(path)) return
      }

      // save global
      saveGlobal(path)
    },
    ThisExpression: (path) => {
      if (isInFunctionDeclaration(path)) return
      saveGlobal(path, 'this')
    }
  

Replies: 2 comments

Comment options

You must be logged in to vote
0 replies
Answer selected by kumavis
Comment options

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
2 participants