分析 Typescript 文件以获得 class 结构

Analyze a Typescript file to obtain class structure

我正在尝试为 Aurelia 编写一个 VS Code 扩展,它将在 HTML 文件中为其配对的视图模型打字稿 class.

中的属性和方法提供智能感知

我可以访问打字稿文件,但我需要一个工具来分析以提取视图模型的属性 class。这是一个 class:

的例子
import { HttpClient } from 'aurelia-fetch-client';
import { observable, inject } from 'aurelia-framework';

@inject(HttpClient)
export class People {
    public people: Person[];
    @observable selectedPerson: Person;
    private httpClient: HttpClient
    public updateMessage: string;

    constructor(http: HttpClient) {
        this.httpClient = http;

        this.httpClient.fetch('http://localhost:5555/api/Persons?$top=10')
            .then(result => result.json() as Promise<ODataPerson>)
            .then(data => {
                this.people = data.value;
            });
    }

    setSelected(selected: Person) {
        this.selectedPerson = selected;
    }

    activate() {
    ...
    }
}

所以我想从中提取一组属性,即 people、selectedPerson 和 updateMessage,如果可能的话,还提取任何 public 方法,即 setSelected。

最好的方法是使用 Require 消耗 class,然后使用 javascript 反射,例如

var Reflector = function(obj) {
  this.getProperties = function() {
    var properties = [];
    for (var prop in obj) {
      if (typeof obj[prop] != 'function') {
        properties.push(prop);
      }
    }
    return properties;
  };

但要做到这一点,我必须将 ts 文件编译成 Javascript 然后我仍然无法使用它,因为要导入和注入像 HttpClient 这样的依赖项,它需要 运行 作为 Aurelia 应用程序的一部分,而我 运行 来自 VS 代码扩展的上下文。

所以我正在寻找可能能够静态分析代码而不使用它的工具,例如 TSLint,但我看不到任何提取 class 属性和方法的工具。

有人知道有什么工具可以实现我想要实现的目标吗?

您应该使用编译器本身来实现这一点。编译器为所有支持打字稿的 IDE 中使用的英特尔感知提供了 API。 Link to the API。语言服务应该能够分析 class 并提供信息。我会调查您是否无法访问 VS Code 中已经 运行 的语言服务,但我没有这方面的信息。