【问题标题】:Analyze a Typescript file to obtain class structure分析 Typescript 文件以获取类结构
【发布时间】:2018-02-10 09:55:03
【问题描述】:

我正在尝试为 Aurelia 编写一个 VS Code 扩展,它将在 HTML 文件中为其配对的 viewmodel typescript 类中的属性和方法提供智能感知。

我可以访问 typescript 文件,但我需要一个分析工具来提取 viewmodel 类的属性。这是此类的一个示例:

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,如果可能的话,还可以提取任何公共方法,即 setSelected。

最好的方法是使用 Require 来使用类,然后使用 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 Code 扩展的上下文中运行。

所以我一直在寻找可能能够在不消耗代码的情况下静态分析代码的工具,例如 TSLint,但我看不到任何提取类属性和方法的工具。

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

【问题讨论】:

    标签: javascript typescript visual-studio-code aurelia tslint


    【解决方案1】:

    您应该使用编译器本身来实现这一点。编译器提供了一个用于 Intel 感知的 API,用于所有支持 typescript 的 IDE。 Link to the API。语言服务应该能够分析课程并提供信息。我会调查你无法访问已经在 VS Code 中运行的语言服务的天气,但我没有这方面的信息。

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-04-12
    • 2020-03-07
    • 1970-01-01
    • 2017-08-23
    • 2012-08-16
    • 2013-02-08
    • 1970-01-01
    相关资源
    最近更新 更多