【发布时间】: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