如何在AST中获取变量的类型



如何获得AST中变量的typeof

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

我想找到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();
}

探索

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

对于TS,您可以使用TS AST查看器或者,您可以直接使用TS编译器API,您可以在这里找到

最新更新