forked from BANG88/typescript-react-intl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
177 lines (160 loc) · 4.42 KB
/
index.ts
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
import ts = require("typescript");
function isDefineMessages(el: ts.Declaration, tagName: string): el is ts.VariableDeclaration {
return (
ts.isVariableDeclaration(el) &&
el.initializer &&
ts.isCallExpression(el.initializer) &&
el.initializer.expression &&
ts.isIdentifier(el.initializer.expression) &&
el.initializer.expression.text === tagName
);
}
// Should be pretty fast: https://stackoverflow.com/a/34491287/14379
function emptyObject(obj: any) {
for (var x in obj) {
return false;
}
return true;
}
interface LooseObject {
[key: string]: any
}
function findProps(node: ts.Node, tagName: string): LooseObject[] {
var res: LooseObject[] = [];
find(node);
function find(node: ts.Node): LooseObject[] {
if (!node) {
return undefined;
}
if (ts.isObjectLiteralExpression(node)) {
node.properties.forEach(p => {
var prop: LooseObject = {};
if (
ts.isPropertyAssignment(p) &&
ts.isObjectLiteralExpression(p.initializer) &&
p.initializer.properties
) {
p.initializer.properties.forEach(ip => {
if (ts.isIdentifier(ip.name)) {
let name = ip.name.text
if (ts.isPropertyAssignment(ip) && ts.isStringLiteral(ip.initializer)) {
prop[name] = ip.initializer.text;
}
}
});
res.push(prop);
}
});
if (tagName === "formatMessage") {
var prop: LooseObject = {};
let name;
node.properties.forEach(p => {
if (ts.isPropertyAssignment(p) && ts.isStringLiteral(p.initializer)) {
name = (p.name as any).escapedText;
prop[name] = p.initializer.text;
}
});
res.push(prop);
}
}
return ts.forEachChild(node, find);
}
return res;
}
function forAllVarDecls(node: ts.Node, cb: (el: ts.VariableDeclaration) => void) {
if (ts.isVariableDeclaration(node)) {
cb(node)
} else {
ts.forEachChild(node, n => forAllVarDecls(n, cb))
}
}
function findFirstJsxOpeningLikeElementWithName(
node: ts.SourceFile,
tagName: string,
dm?: boolean
) {
var res: LooseObject[] = [];
find(node);
function find(node: ts.Node | ts.SourceFile): undefined {
if (!node) {
return undefined;
}
if (dm && ts.isSourceFile(node)) {
// getNamedDeclarations is not currently public
forAllVarDecls(node, (el: ts.Declaration) => {
if (isDefineMessages(el, tagName)) {
if (
ts.isCallExpression(el.initializer) &&
el.initializer.arguments.length
) {
var nodeProps = el.initializer.arguments[0];
var props = findProps(nodeProps, tagName);
// props is an array of LooseObject
res = res.concat(props);
}
}
})
} else {
// Is this a JsxElement with an identifier name?
if (
ts.isJsxOpeningLikeElement(node) &&
ts.isIdentifier(node.tagName)
) {
// Does the tag name match what we're looking for?
const childTagName = node.tagName;
if (childTagName.text === tagName) {
// node is a JsxOpeningLikeElement
res.push(node);
}
}
}
return ts.forEachChild(node, find);
}
return res;
}
/**
* Parse tsx files
*
* @export
* @param {string} contents
* @returns {array}
*/
function main(contents: string): {}[] {
var sourceFile = ts.createSourceFile(
"file.ts",
contents,
ts.ScriptTarget.ES2015,
/*setParentNodes */ false,
ts.ScriptKind.TSX
);
var elements = findFirstJsxOpeningLikeElementWithName(
sourceFile,
"FormattedMessage"
);
var dm = findFirstJsxOpeningLikeElementWithName(
sourceFile,
"defineMessages",
true
);
var fm = findFirstJsxOpeningLikeElementWithName(
sourceFile,
"formatMessage",
true
);
var res = elements
.map(element => {
var msg: LooseObject = {};
debugger;
element.attributes &&
element.attributes.properties.forEach((attr: LooseObject) => {
// found nothing
if (!attr.name || !attr.initializer) return;
msg[attr.name.text] =
attr.initializer.text || attr.initializer.expression.text;
});
return msg;
})
.filter(r => !emptyObject(r));
return res.concat(dm).concat(fm);
}
export default main;