-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbt.ts
531 lines (466 loc) · 19.2 KB
/
bt.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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
//<reference path="node_modules/reflect-metadata/reflect-metadata.d.ts"/>
import cheerio = require('cheerio');
const __bt = '__@bt';
export const WebComponentProps = 'WebComponentProps';
export const ComputedRelationship = 'ComputedRelationship';
const designTypeMetaKey = 'design:type';
const getter = function(ID: string, defaultValue?: any){
return function(){
const lu = this[__bt];
if(!lu) return defaultValue;
return lu[ID];
}
}
const setter = function(ID: string){
return function(val){
let lu = this[__bt];
if(!lu){
lu = [];
this[__bt] = lu;
}
lu[ID] = val;
}
}
export interface IPropertyProps{
/**
* Default value for the property. If value is a function, the function is invoked and the return value is used as the default value of the property. If the default value should be an array or object unique to the instance, create the array or object inside a function. See Configuring default property values for more information.
*/
defaultValue?: any;
/**
* If true, the property is available for two-way data binding. In addition, an event, property-name-changed is fired whenever the property changes. See Property change notification events (notify) for more information.
*/
polymer_notify?: boolean;
/**
* The value is interpreted as a method name to be invoked when the property value changes. Note that unlike in 0.5, property change handlers must be registered explicitly. The propertyNameChanged method will not be invoked automatically. See Property change callbacks (observers) for more information.
*/
polymer_observer?: string;
/**
* If true, the property can't be set directly by assignment or data binding.
*/
polymer_readOnly?: boolean;
/**
* Set to true to cause the corresponding attribute to be set on the host node when the property value changes. If the property value is Boolean, the attribute is created as a standard HTML boolean attribute (set if true, not set if false). For other property types, the attribute value is a string representation of the property value. Equivalent to reflect in Polymer 0.5. See Reflecting properties to attributes for more information.
*/
polymer_reflectToAttribute?: boolean;
}
export interface IComputedPropInfo{
computedMethodName: string;
argList: string[];
}
//#region class reflection
export interface IPair{
lhs: string;
rhs: string;
}
export interface INameValuePair{
name: string;
value: string;
}
export interface IPropertyInfo extends IReflectionEntity {
//name?: string;
propertyDescriptor?: PropertyDescriptor;
//metadata: {[key: string]: string};
//metadata: INameValuePair[];
type?: any;
}
export class PropertyInfo implements IPropertyInfo{
constructor(public name: string, public propertyTypeClassRef: Function){}
private _propertyType;
public get propertyType(){
if(!this._propertyType){
this._propertyType = reflectClassPrototype(this.propertyTypeClassRef.prototype, true);
}
return this._propertyType;
}
}
export interface IMemberInfo extends IReflectionEntity{
propertyDescriptor ?: any;
isPublic?: boolean;
}
export interface IMethodArgument extends IReflectionEntity {
argumentType?: IType;
argumentTypeClassRef?: any;
}
export class MethodArgument implements IMethodArgument{
constructor(public name: string, public argumentTypeClassRef: Function){}
private _argumentType : IType;
public get argumentType(){
if(!this._argumentType){
this._argumentType = reflectClassPrototype(this.argumentTypeClassRef.prototype, true);
}
return this._argumentType;
}
}
export interface IMethodInfo extends IMemberInfo{
value?: any;
functionStr?: string;
returnType?: IType;
returnTypeClassRef?: Function;
args?: IMethodArgument[];
methodBody?: string;
}
export class MethodInfo implements IMethodInfo{
constructor(public name: string, public args: IMethodArgument[], public returnTypeClassRef?: Function){}
private _returnType: IType;
public get returnType(){
if(!this._returnType){
this._returnType = reflectClassPrototype(this.returnTypeClassRef.prototype, true);
}
return this._returnType;
}
methodBody: string;
}
export interface IReflectionEntity{
name?: string;
metadata?: {[key: string] : any;};
}
export interface IType extends IReflectionEntity{
properties?: IPropertyInfo[];
methods?: IMethodInfo[];
staticProperties?: IPropertyInfo[];
staticMethods?: IMethodInfo[];
}
export function reflectClassPrototype(classPrototype: any, recursive?: boolean) : IType{
let name : string = classPrototype.constructor.toString().replace('class ', '').trim();
const iPosOfOpenParen = name.indexOf('{');
name = name.substr(0, iPosOfOpenParen);
const splitName = name.split(' ');
name = splitName[0];
const returnType : IType = {
name: name
}
addMemberInfo(returnType, classPrototype, true, recursive);
return returnType;
}
export function processFACETSFileClass(className: string, facetsFile: any) : IType{
const classDef = facetsFile[className];
const classProto = classDef.prototype;
return reflectClassPrototype(classProto, true);
}
function getPropertyDescriptor(classPrototype: any, memberKey: string){
while(classPrototype){
const propertyDescriptor = Object.getOwnPropertyDescriptor(classPrototype, memberKey);
if(propertyDescriptor) return propertyDescriptor;
classPrototype = classPrototype.__proto__;
}
return null;
}
export function createNew<InterfaceImplementorType, InterfaceType>(classRef: Function, obj: InterfaceType ){
const implObj = new (<any>classRef)();
Object.assign(implObj, obj);
return <InterfaceImplementorType> implObj;
}
function addMemberInfo(returnType: IType, classRefOrClassPrototype: any, isPrototype: boolean, recursive?: boolean){
const memberNames = Object.getOwnPropertyNames(classRefOrClassPrototype);
for(const memberKey of memberNames){
const propertyDescriptor = getPropertyDescriptor(classRefOrClassPrototype, memberKey);
if(propertyDescriptor){
const memberInfo : IMemberInfo = {
name: memberKey,
propertyDescriptor : propertyDescriptor,
};
const metaDataKeys = Reflect.getMetadataKeys(classRefOrClassPrototype, memberKey);
for(let i = 0, n = metaDataKeys.length; i < n; i++){
const metaKey = metaDataKeys[i];
if(!memberInfo.metadata) memberInfo.metadata = {};
//debugger;
memberInfo.metadata[metaKey] = Reflect.getMetadata(metaKey, classRefOrClassPrototype, memberKey);
}
if(propertyDescriptor.value){
//#region method
//const methodInfo = <IMethodInfo> memberInfo;
const methodInfo = createNew<MethodInfo, IMethodInfo>(MethodInfo, memberInfo);
const methodSignature = propertyDescriptor.value.toString();
const signatureInsideParenthesis = substring_between(methodSignature, '(', ')');
if(!signatureInsideParenthesis){
console.log('TODO: handle this scenario');
continue;
}
const paramNames = signatureInsideParenthesis.split(',');
if(memberInfo.metadata){
const paramTypes = memberInfo.metadata['design:paramtypes'];
if(paramTypes.length > 0){
if(paramNames.length !== paramTypes.length){
throw `Discrepency found in method parameters for method: ${memberKey}`;
}
methodInfo.args = [];
methodInfo.returnTypeClassRef = memberInfo.metadata['design:returntype'];
for(let i = 0, n = paramTypes.length; i < n; i++){
const paramInfo = new MethodArgument(paramNames[i].trim(), paramTypes[i]);
methodInfo.args.push(paramInfo);
}
}
}else{
methodInfo.args = [];
for(var paramName of paramNames){
const paramInfo : IMethodArgument = {
name: paramName,
};
methodInfo.args.push(paramInfo);
}
}
const methodBody = substring_after( methodSignature, ')');
methodInfo.methodBody = methodBody;
if(isPrototype){
if(!returnType.methods) returnType.methods = [];
returnType.methods.push(methodInfo);
}else{
if(!returnType.staticMethods) returnType.staticMethods = [];
returnType.staticMethods.push(methodInfo);
}
//#endregion
}else if(propertyDescriptor.get || propertyDescriptor.set){
//#region property
const propInfo = createNew<PropertyInfo, IMemberInfo>(PropertyInfo, memberInfo);
if(isPrototype){
if(!returnType.properties) returnType.properties = [];
returnType.properties.push(propInfo);
}else{
if(!returnType.staticProperties) returnType.staticProperties = [];
returnType.staticProperties.push(propInfo);
}
if(recursive){
const propertyType = Reflect.getMetadata(designTypeMetaKey, classRefOrClassPrototype, memberKey);
propInfo.propertyTypeClassRef = propertyType;
// if(propertyType){
// propInfo.propertyType = reflectPrototype(propertyType.prototype, recursive);
// }
}
//#endregion
}
}
}
}
//#endregion
export function toProp(props?: IPropertyProps){
return (classPrototype: any, fieldName: string) =>{
//from http://blog.wolksoftware.com/decorators-metadata-reflection-in-typescript-from-novice-to-expert-part-ii
if (!this[fieldName]) {
// Create new property with getter and setter
const propDescriptor = Object.getOwnPropertyDescriptor(classPrototype, fieldName);
if(propDescriptor){
const getter = propDescriptor.get;
const setter = propDescriptor.set;
if(getter && !setter){
const getterString = getter.toString();
const splitReturn = getterString.split('return');
if(splitReturn.length === 2){
const beforeBrace = splitReturn[1].split('}');
if(beforeBrace.length === 2){
const splitParent = beforeBrace[0].split('(');
if(splitParent.length === 2){
const splitFunctionThis = splitParent[0].split('this.');
const splitArgsParenthis = splitParent[1].split(')');
if(splitFunctionThis.length === 2 && splitArgsParenthis.length === 2){
const computeFunctionName = splitFunctionThis[1].trim();
const args = splitArgsParenthis[0];
const splitArgs = args.split(',');
const computedPropInfo : IComputedPropInfo = {
argList : splitArgs,
computedMethodName: computeFunctionName,
};
Reflect.defineMetadata(ComputedRelationship, computedPropInfo, classPrototype, fieldName);
}
}
}
}
//const re = /?function/;
//const test = getterString.split(re);
}
}else{
Object.defineProperty(classPrototype, fieldName, {
get: getter(fieldName, props? props.defaultValue : null),
set: setter(fieldName),
enumerable: true,
configurable: true,
});
}
Reflect.defineMetadata(WebComponentProps, props, classPrototype, fieldName);
}
}
}
export function generateTemplateAbstractSyntaxTree(templateFnString: string){
const splitArrow = templateFnString.split('=>');
const parameterSide = splitArrow.shift();
const parameterSideWithoutParenthesis = parameterSide.replace('(', '').replace(')', '');
const parameterSideWithoutType = parameterSideWithoutParenthesis.split(':')[0].trim();
const functionBodySide = splitArrow.join('=>').trim().substr(1);
const templateHTML = `<xsl:template match="${parameterSideWithoutType}">
${functionBodySide}
</xsl:template>`;
const $ = cheerio.load(templateHTML);
processRoot($.root(), $);
return $;
}
function processRoot($node: Cheerio, $: CheerioStatic){
const templateTokenPair: IPair = {
lhs: '${',
rhs: '}'
};
$node.each((idx, node) =>{
processNodeElement(node, templateTokenPair, null, $);
})
}
function populateTextNode(nodeElement: CheerioElement, templateTokenPair: IPair, parent: CheerioElement, $: CheerioStatic){
const innerText = nodeElement['data'];
const splitPair = splitPairs(innerText, templateTokenPair);
if(splitPair.length > 1){
for(let i=0, ii = splitPair.length; i < ii; i++){
const token = splitPair[i];
if(token === templateTokenPair.lhs && i < ii - 2 && splitPair[i + 2] == templateTokenPair.rhs){
splitPair[i] = '<xsl:value-of select="';
const val = splitPair[i + 1];
const valWithoutHeadToken = removeHeadToken(val, '.');
const newVal = replaceAll( valWithoutHeadToken, '.', '/')
splitPair[i + 1] = newVal;
splitPair[i + 2] = '"/>';
}
}
//$parent.text(splitPair.join(''));
const $parent = $(parent);
$parent.html(splitPair.join(''));
//nodeElement['data'] = splitPair.join('');
}
}
function processNodeElement(nodeElement: CheerioElement, templateTokenPair: IPair, parent: CheerioElement, $: CheerioStatic){
if(!nodeElement) return;
if(nodeElement.type==='text'){
populateTextNode(nodeElement, templateTokenPair, parent, $);
}else{
processAttributes(nodeElement, templateTokenPair);
}
const children = nodeElement.children;
if(!children) return;
const processChildrenFn = () =>{
children.forEach(child => {
processNodeElement(child, templateTokenPair, nodeElement, $);
});
};
//const $nodeElement = $(nodeElement);
if(children.length !== 3) return processChildrenFn();
const lastChild = children[2];
if(lastChild.type !== 'text') return processChildrenFn();
const lastChildText = lastChild['data'] as string;
const lastChildTextTrim = lastChildText.trim();
if(!lastChildTextTrim || lastChildTextTrim !== "`).join('')}") return processChildrenFn()
const firstChild = children[0];
if(firstChild.type !== 'text') return processChildrenFn();
const firstChildText = firstChild['data'] as string;
const firstChildTextTrim = firstChildText.trim();
const splitMap = firstChildTextTrim.split('.map(');
if(splitMap.length !== 2) return processChildrenFn(); //TODO: deal with other cases
const listPath0 = splitMap[0];
const splitPath0 = listPath0.split('${');
if(splitPath0.length !== 2) return processChildrenFn();
const pathWithoutHeadToken = removeHeadToken(splitPath0[1], '.');
const loopPath = replaceAll( pathWithoutHeadToken, '.', '/');
const loopSignaure = splitMap[1].trim();
const loopVariable = loopSignaure.split('=>')[0].trim();
//firstChild['data'] = '<hello>';
//lastChild['data'] = '</hello>';
const $nodeElement = $(nodeElement);
const middleChild = children[1];
processNodeElement(middleChild, templateTokenPair, nodeElement, $);
//const middleChildHTML = '<' + middleChild.name + '>' + $(middleChild).html() + '</' + middleChild.name + '>'
const middleChildHTML = $.html(middleChild);
$nodeElement.html(`
<xsl:for-each select="${loopPath}">
<xsl:variable name='${loopVariable}' select='.'></xsl:variable>
${middleChildHTML}
</xsl:for-each>
`);
}
function processAttributes(nodeElement: CheerioElement, templateTokenPair: IPair){
const attribs = nodeElement.attribs;
if(!attribs) return;
for(let key in attribs){
let val = attribs[key] as string;
const splitPair = splitPairs(val, templateTokenPair);
if(splitPair.length > 1){
//const isEventHandler = key.startsWith('on');
const ii = splitPair.length;
splitPair.forEach((token, i) =>{
if(token === templateTokenPair.lhs && i < ii - 2 && splitPair[i + 2] == templateTokenPair.rhs){
//splitPair[i] = isEventHandler ? '' : '{{';
splitPair[i] = '{';
let val2 = splitPair[i + 1];
const posOfDot = val2.indexOf('.');
if(posOfDot > -1){
val2 = val2.substr(posOfDot + 1);
splitPair[i + 1] = val2;
}
//splitPair[i + 2] = isEventHandler ? '' : '}}';
splitPair[i + 2] = '}';
}
});
let newKey: string;
let newVal = splitPair.join('');
// if(isEventHandler){
// newVal = newVal.replace('()', '');
// newKey = 'on-' + key.substr(2);
// }else{
// newKey = key + '$';
// }
newKey = key;
attribs[newKey] = newVal;
//delete attribs[key];
}
}
}
function splitPairs(text: string, pair: IPair): string[]{
const returnObj: string[] = [];
let region: string[] = [];
const lhsFirstChr = pair.lhs.substr(0, 1);
const rhsFirstChr = pair.rhs.substr(0, 1);
const lhsLength = pair.lhs.length;
const rhsLength = pair.rhs.length;
for(let i = 0, ii = text.length; i < ii; i++){
const chr = text[i];
let foundLHSMatch: boolean;
let foundRHSMatch: boolean;
if(chr === lhsFirstChr){
if(text.substr(i, lhsLength) === pair.lhs){
foundLHSMatch = true;
}
}
if(chr === rhsFirstChr){
if(text.substr(i, rhsLength) === pair.rhs){
foundRHSMatch = true;
}
}
if(foundLHSMatch || foundRHSMatch){
if(region.length > 0) {
returnObj.push(region.join(''));
region = [];
}
returnObj.push(foundLHSMatch ? pair.lhs : pair.rhs );
i += (foundLHSMatch ? lhsLength: rhsLength) - 1;
}else{
region.push(chr);
}
}
if(region.length > 0){
returnObj.push(region.join(''));
}
return returnObj;
}
function replaceAll(text: string, oldVal: string, newVal: string){
return text.split(oldVal).join(newVal);
}
function removeHeadToken(text: string, search: string){
const iPos = text.indexOf(search);
if(iPos == 1) return text;
return text.substr(iPos + 1);
}
function substring_between(value: string, LHDelim: string, RHDelim: string){
const iPosOfLHDelim = value.indexOf(LHDelim);
if(iPosOfLHDelim === -1) return null;
const iPosOfRHDelim = value.indexOf(RHDelim);
if(iPosOfRHDelim === -1) return value.substring(iPosOfLHDelim + LHDelim.length);
return value.substring(iPosOfLHDelim + LHDelim.length, iPosOfRHDelim);
}
function substring_after(value: string, after: string){
const iPosOfAfter = value.indexOf(after);
if(iPosOfAfter === -1) return null;
return value.substr(iPosOfAfter + after.length);
}