语言服务:在位置找到节点

Language Services: find Node at position

我正在使用 Typescript 语言服务。

我需要找到一个 ts.Node,它只有一个 ts.SourceFile 实例和一个感兴趣的绝对位置(数字)作为输入参数。

此外,当感兴趣的标记是 else 关键字或左大括号 { 或位于 if 条件内时,我需要满足这些情况。

有谁知道语言服务中是否已经存在这样的功能?

UPD:我设法在 Typescript 源代码的某处找到函数 getTouchingToken(大概是我需要的),但我没有看到它出口。任何线索如何重用它?

I managed to find function getTouchingToken (that is presumably does what I need) somewhere in Typescript source code, but I don't see it being exported. Any clues how to reuse it

我使用 ts.getTokenAtPosition 例如在 alm tools for TypeScript

中提供快速修复功能

您可以使用 tsutils which includes getTokenAtPosition.

这比依赖内部 TypeScript API 更好,后者可能随时会崩溃。