I would like to go over my .ts files at runtime and get some information out about which classes are in the file, which functions it has, which properties and whether they are public or private.
In actionscript3 I used to use "describeType" for this, which returned this info in XML. Anyone know of a way to get this information in typescript?
Are there perhaps tools that can parse jsdoc of any file type and return information about that?
Since you want to process files, I don't understand the requirement "at runtime".
Apart from that, if you just want to analyse TS code using TS, you can use the TypeScript language services, which have all the APIs needed to support a TypeScript IDE, all written in TypeScript. In this particular example, languageService.getScriptLexicalStructure(file) should do the trick.
If you want to do this from the commandline, or want to see an example of using the language services, you can try https://github.com/clausreinke/typescript-tools
$ cat reflection.ts
module foo{
export class A{
public x1:number = 10;
public x2 = 20;
public s1: string = "";
constructor(public x = true) {}
}
}
function listProperties(o, isClass?) {
var props = [];
console.log("name:",o["name"]);
var tmp = isClass ? new o : o;
for (var p in tmp)
console.log(p,typeof tmp[p]);
}
listProperties(foo.A,true);
$ node typescript-tools/bin/tss.js reflection.ts
"loaded c:/javascript/typescript/reflection.ts, TSS listening.."
prettyJSON true
"pretty JSON: true"
> structure reflection.ts
[
{
"loc": {
"name": "foo",
"kind": "module",
"kindModifiers": "",
"matchKind": "exact",
"fileName": "c:/javascript/typescript/reflection.ts",
"minChar": 0,
"limChar": 183,
"containerName": "",
"containerKind": ""
},
"file": "c:/javascript/typescript/reflection.ts",
"min": {
"line": 1,
"character": 1
},
"lim": {
"line": 9,
"character": 2
}
},
{
"loc": {
"name": "A",
"kind": "class",
"kindModifiers": "export",
"matchKind": "exact",
"fileName": "c:/javascript/typescript/reflection.ts",
"minChar": 17,
"limChar": 180,
"containerName": "foo",
"containerKind": "module"
},
"file": "c:/javascript/typescript/reflection.ts",
"min": {
"line": 2,
"character": 5
},
"lim": {
"line": 8,
"character": 6
}
},
{
"loc": {
"name": "x1",
"kind": "property",
"kindModifiers": "public",
"matchKind": "exact",
"fileName": "c:/javascript/typescript/reflection.ts",
"minChar": 49,
"limChar": 63,
"containerName": "foo.A",
"containerKind": "class"
},
"file": "c:/javascript/typescript/reflection.ts",
"min": {
"line": 3,
"character": 16
},
"lim": {
"line": 3,
"character": 30
}
},
{
"loc": {
"name": "x2",
"kind": "property",
"kindModifiers": "public",
"matchKind": "exact",
"fileName": "c:/javascript/typescript/reflection.ts",
"minChar": 81,
"limChar": 88,
"containerName": "foo.A",
"containerKind": "class"
},
"file": "c:/javascript/typescript/reflection.ts",
"min": {
"line": 4,
"character": 16
},
"lim": {
"line": 4,
"character": 23
}
},
{
"loc": {
"name": "s1",
"kind": "property",
"kindModifiers": "public",
"matchKind": "exact",
"fileName": "c:/javascript/typescript/reflection.ts",
"minChar": 116,
"limChar": 131,
"containerName": "foo.A",
"containerKind": "class"
},
"file": "c:/javascript/typescript/reflection.ts",
"min": {
"line": 6,
"character": 16
},
"lim": {
"line": 6,
"character": 31
}
},
{
"loc": {
"name": "constructor",
"kind": "constructor",
"kindModifiers": "",
"matchKind": "exact",
"fileName": "c:/javascript/typescript/reflection.ts",
"minChar": 142,
"limChar": 173,
"containerName": "foo.A",
"containerKind": "class"
},
"file": "c:/javascript/typescript/reflection.ts",
"min": {
"line": 7,
"character": 9
},
"lim": {
"line": 7,
"character": 40
}
},
{
"loc": {
"name": "listProperties",
"kind": "function",
"kindModifiers": "",
"matchKind": "exact",
"fileName": "c:/javascript/typescript/reflection.ts",
"minChar": 187,
"limChar": 368,
"containerName": "",
"containerKind": ""
},
"file": "c:/javascript/typescript/reflection.ts",
"min": {
"line": 11,
"character": 1
},
"lim": {
"line": 17,
"character": 2
}
}
]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With