How to get typeof variable in AST?

379 views Asked by At

How to get what is the typeof of variable in AST?

const code = `const foo = () => {
  const baz = this;
}`;

I want to find the variables that are typeof globalThis. (for example baz is typeof globalThis)

Any idea how to do that?

transform code:

module.exports = (fileInfo, api, options) => {
  
  const j = api.jscodeshift;
  const root = j(fileInfo.source);
  
  root.find(j.Identifier)
   .forEach(x => {
    if (x.value.name === 'baz') {
        console.log({ x });
        // type: typeof globalThis??
    }
    
  });
  
  return root.toSource();
}

astexplorer

1

There are 1 answers

1
captain-yossarian from Ukraine On BEST ANSWER

First of all, you are looking in wrong AST viewer.

For TS, you can use TS AST viewer Or, you can use directly TS compiler API, which you can find here