0

如何获得typeofAST中的变量是什么?

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

我想找到 typeof 的变量globalThis。(例如baztypeof globalThis

知道怎么做吗?

转换代码:

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

探索者

4

1 回答 1

1

首先,您正在查看错误的 AST 查看器。

对于 TS,你可以使用TS AST viewer 或者,你可以直接使用 TS compiler API,你可以在这里找到

于 2020-12-09T09:13:48.323 回答