-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathparser.js
105 lines (91 loc) · 2.45 KB
/
parser.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
const ts = require('typescript');
const path = require('path');
const fs = require('fs');
const types = {
136: 'string'
};
function getDecorator(node, kind) {
if (node.decorators && node.decorators.length) {
for (const decorator of node.decorators) {
if (decorator.expression.expression.text === kind) {
return decorator.expression.expression.text;
}
}
}
}
function getDocs(node) {
let doc = [];
if(node.jsDoc && node.jsDoc.length) {
for(const d of node.jsDoc) {
doc.push(d.comment)
}
}
if(doc.length) {
return doc.join('/n');
} else {
return undefined;
}
}
function getInputType(node) {
if(node.type) {
let type = node.type.kind;
if(types[node.type.kind]){
return types[node.type.kind];
} else {
if (node.type.kind === ts.SyntaxKind.TypeReference) {
return node.type.typeName.text;
}
}
return type;
}
}
function getOutputType(node) {
if (node.type.kind === ts.SyntaxKind.TypeReference) {
if (node.type.typeArguments && node.type.typeArguments.length) {
for (const arg of node.type.typeArguments) {
if(arg.typeName) {
return arg.typeName.text;
}
}
}
}
}
function parseFiles(matchedFiles) {
const metas = [];
for (const sourcePath of matchedFiles) {
const sourceContent = fs.readFileSync(sourcePath, 'utf-8');
const sourceFile = ts.createSourceFile(
sourcePath, sourceContent, ts.ScriptTarget.Latest, false, ts.ScriptKind.TS);
const visit = (node, parent) => {
let meta;
if (node.kind === ts.SyntaxKind.ClassDeclaration) {
meta = {
component: node.name.text,
docs: getDocs(node),
inputs: {},
outputs: {}
};
if (getDecorator(node, 'Component')) {
metas.push(meta);
}
} else if (node.kind === ts.SyntaxKind.PropertyDeclaration) {
if (getDecorator(node, 'Input')) {
parent.inputs[node.name.text] = {
doc: getDocs(node),
value: node.initializer ? node.initializer.text : undefined,
type: getInputType(node)
};
} else if (getDecorator(node, 'Output')) {
parent.outputs[node.name.text] = {
doc: getDocs(node),
type: getOutputType(node)
};
}
}
ts.forEachChild(node, (n) => visit(n, meta));
};
visit(sourceFile);
}
return metas;
}
module.exports = parseFiles;