从ts.LanguageService.getReferencesAtPosition获取AST节点

时间:2018-09-03 02:11:42

标签: typescript

我正在尝试使用TypeScript编译器API检索AST节点以引用对函数定义的引用。现在,我的实现看起来像

const functionName = functionDeclaration.name;
const refs = languageService.getReferencesAtPosition(
    filename, functionName.getStart(sourceFile));
for (const ref of refs) {
  const refNode = searchForNode(ref.textSpan, sourceFile);
}

searchForNode在AST处查找具有匹配textSpan的节点。

此代码有效,但似乎是一个非常round回的解决方案。内部函数直接返回https://github.com/Microsoft/TypeScript/blob/d60f4988a6260f81f0f4f83f69a64f4206c7fa82/src/services/findAllReferences.ts#L234的AST节点,但是在通过语言服务API返回之前,它会将AST节点转换为ts.TextSpan

有更快的方法吗?

0 个答案:

没有答案