/
ts-util.ts
212 lines (197 loc) · 6.4 KB
/
ts-util.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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
import * as ts from 'typescript';
import { bindingMode } from './binding';
import { hyphenate } from './util';
function resolveBindingMode(mode: string): bindingMode | undefined {
switch (mode) {
case 'oneTime': return bindingMode.oneTime;
case 'toView': case 'oneTway': return bindingMode.toView;
case 'twoWay': return bindingMode.twoWay;
case 'fromView': return bindingMode.fromView;
}
console.log(`Cannot resolve binding mode for mode: [${mode}].`);
return undefined;
}
export function getObjectPropertyValue(obj: ts.ObjectLiteralExpression, name: string): ts.Expression | undefined {
let properties = obj.properties;
if (!properties || !properties.length) {
return undefined;
}
for (let i = 0, ii = properties.length; ii > i; ++i) {
let prop = properties[i];
if (!ts.isPropertyAssignment(prop)) {
continue;
}
let propName = prop.name;
if (ts.isIdentifier(propName) && propName.escapedText.toString() === name) {
return prop.initializer;
}
}
return undefined;
}
export function getBindableDecoratorBindingMode(dec: ts.Decorator) {
let expression = dec.expression;
// @bindable
if (!ts.isCallExpression(expression)) {
return undefined;
}
// @bindable()
let args = expression.arguments;
if (!args || !args.length) {
return undefined;
}
if (args.length > 1) {
// @bindable(arg1, ...)
console.log('@bindable() used with more than 1 parameter.');
}
// @bindable({ ... })
let config = expression.arguments[0];
// @bindable('...')
if (!ts.isObjectLiteralExpression(config)) {
return undefined;
}
let propValue = getObjectPropertyValue(config, 'defaultBindingMode');
if (!propValue) {
// @bindable({ no defaultBindingMode specified object })
return undefined;
}
if (!ts.isPropertyAccessExpression(propValue)) {
return undefined;
}
expression = propValue.expression;
if (!ts.isIdentifier(expression)) {
// { defaultBindingMode: (() => {})() } : too complex
// TODO: consider support craziness
return undefined;
}
let accessedObjName = expression.escapedText.toString();
if (accessedObjName !== 'bindingMode') {
// bindingMode.* support only
// TODO: consider support renamed import
return undefined;
}
// { defaultBindingMode: bindingMode.(mode) }
let mode = propValue.name.escapedText.toString();
return resolveBindingMode(mode);
}
export function getBindableDecoratorPrimaryPropertyValue(dec: ts.Decorator): boolean {
let expression = dec.expression;
// @bindable
if (!ts.isCallExpression(expression)) {
return false;
}
// @bindable()
let args = expression.arguments;
if (!args || !args.length) {
return false;
}
if (args.length > 1) {
// @bindable(arg1, ...)
console.log('@bindable() used with more than 1 parameter.');
}
// @bindable({ ... })
let config = expression.arguments[0];
// @bindable('...')
if (!ts.isObjectLiteralExpression(config)) {
return false;
}
let propValue = getObjectPropertyValue(config, 'primaryProperty');
if (!propValue) {
// @bindable({ no defaultBindingMode specified object })
return false;
}
if (!ts.isToken(propValue)) {
return false;
}
return propValue.kind === ts.SyntaxKind.TrueKeyword;
}
export function getBindableMode(classMember: ts.ClassElement) {
if (!ts.isPropertyDeclaration(classMember)) {
return undefined;
}
let bindableDecorator = getBindableDecorator(classMember);
if (!bindableDecorator) {
return undefined;
}
return getBindableDecoratorBindingMode(bindableDecorator);
}
export function hasBindableDecorator(member: ts.ClassElement): member is ts.PropertyDeclaration {
if (!ts.isPropertyDeclaration(member)) {
return false;
}
if (!member.decorators) {
return false;
}
return member.decorators.some(decorator => (decorator.expression as ts.Identifier).text === 'bindable');
}
export function getDecoratorByName(decorators: ts.Decorator[] | ts.NodeArray<ts.Decorator>, name: string): ts.Decorator | null {
return decorators.slice(0).find(dec => {
let expression = dec.expression;
if (ts.isCallExpression(expression)) {
let innerExpression = expression.expression;
return ts.isIdentifier(innerExpression) && innerExpression.escapedText.toString() === name;
} else if (ts.isIdentifier(dec.expression)) {
return dec.expression.escapedText.toString() === name;
}
return false;
}) || null;
}
export function getBindableDecorator(prop: ts.PropertyDeclaration): ts.Decorator | null {
if (!prop.decorators) {
return null;
}
return getDecoratorByName(prop.decorators, 'bindable');
}
export function normalizeElementClassName(name: ts.ClassDeclaration | ts.Identifier | string) {
name = typeof name === 'string'
? name
: ts.isIdentifier(name)
? name.escapedText.toString()
: name.name
? name.name.escapedText.toString()
: 'Anonymous';
return name.replace(/CustomElement$/, '');
}
export function getBehaviorHtmlName(name: ts.ClassDeclaration | ts.Identifier | string): string {
return hyphenate(normalizeElementClassName(name));
}
export function getAttributeHtmlName(name: ts.Identifier | string): string {
name = typeof name === 'string' ? name : name.escapedText.toString();
return hyphenate(name.replace(/CustomAttribute$/, ''));
}
export function getElementViewName(name: ts.ClassDeclaration | ts.Identifier | string): string {
return `$${normalizeElementClassName(name)}View`;
}
export function getPrivateClassName(name: ts.ClassDeclaration | ts.Identifier | string): string {
return `$${
typeof name === 'string'
? name
: ts.isIdentifier(name)
? name.escapedText.toString()
: name.name
? name.name.escapedText.toString()
: 'Anonymous'
}`;
}
export function removeExport(modifiers?: ts.NodeArray<ts.Modifier>): ts.NodeArray<ts.Modifier> | undefined {
if (!modifiers) {
return modifiers;
}
let idx = modifiers.findIndex(m => m.kind === ts.SyntaxKind.ExportKeyword);
if (idx !== -1) {
let mods = [...modifiers];
mods.splice(idx, 1);
return mods as any;
}
return modifiers;
}
export function getPropertyName(prop: ts.PropertyDeclaration): string {
let name = prop.name;
if (ts.isComputedPropertyName(name)) {
// TODO: support computed property name extraction
throw new Error('Not supported yet');
}
if (ts.isIdentifier(name)) {
return name.escapedText.toString();
}
return name.text;
}