-
-
Notifications
You must be signed in to change notification settings - Fork 142
Expand file tree
/
Copy pathes5-visitors.js
More file actions
806 lines (680 loc) · 31.7 KB
/
es5-visitors.js
File metadata and controls
806 lines (680 loc) · 31.7 KB
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
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
var es5_visitors = (function() {
var types = require("@babel/types"),
defaultExtendDecoratorName = "JavaProxy",
columnOffset = 1,
ASTERISK_SEPARATOR = "*",
customExtendsArr = [],
normalExtendsArr = [],
interfacesArr = [],
UNSUPPORTED_TYPESCRIPT_EXTEND_FORMAT_MESSAGE = "[WARN] TypeScript-extended class has a super() call in an unsupported format.",
customExtendsArrGlobal = [];
/* ENTRY POINT!
* Traverses each passed node with several visitors.
* Result from visit can be got from static methods.
*
* Input parameters:
* path - node to visit
* config - filename, decorator name ...
*/
function es5Visitor(path, config) {
if (!config.filePath) {
config.filePath = "No file path provided";
}
if (path.node.skipMeOnVisit) {
return;
}
// ES5 Syntax
// anchor is extend (normal extend pattern + custom extend pattern)
if (types.isMemberExpression(path) && path.node.property.name === "extend") {
traverseEs5Extend(path, config);
}
//anchor is new keyword (interface pattern)
if (types.isNewExpression(path)) {
traverseInterface(path, config);
}
// // Parsed Typescript to ES5 Syntax (normal extend pattern + custom extend pattern)
// // anchor is __extends
if (types.isIdentifier(path) && path.node.name === "__extends") {
traverseTsExtend(path, config);
}
// Maybe it's not a good idea to expose this scenario because it can be explicitly covered
// //anchor is JavaProxy (optional)
// var customDecoratorName = config.extendDecoratorName === undefined ? defaultExtendDecoratorName : config.extendDecoratorName;
// if(t.isIdentifier(path) && path.node.name === customDecoratorName) {
// if(path.node.skipMeOnVisit) {
// return;
// }
// console.log("enters because there is a java proxy down the way")
// traverseJavaProxyExtend(path, config, customDecoratorName);
// }
}
/*
* Returns the custom extends array generated from visitor
*/
es5Visitor.getProxyExtendInfo = function() {
var res = customExtendsArr.slice();
customExtendsArr = [];
return res;
}
/*
* Returns the common extends array generated from visitor
*/
es5Visitor.getCommonExtendInfo = function() {
var res = [];
for (var index in normalExtendsArr) {
if (normalExtendsArr[index][0] !== "*") {
res.push(normalExtendsArr[index]);
}
}
normalExtendsArr = [];
return res;
}
/*
* Returns the extended interfaces array generated from visitor
*/
es5Visitor.getInterfaceInfo = function() {
var res = interfacesArr.slice();
interfacesArr = [];
return res;
}
/*
* Traverses the typescript extend case (__extends())
* Write results in "normalExtendsArr" or "customExtendsArr".
*/
function traverseTsExtend(path, config) {
// information for normal extend (unnamed)
var extendClass;
try {
extendClass = _getArgumentFromNodeAsString(path, 5, config)
} catch (e) {
config.logger.warn(e.message)
return;
}
var overriddenMethodNames = _getOverriddenMethodsTypescript(path, 3);
var extendPath = _getParent(path, 3, config);
var declaredClassName = "";
var typescriptClassExtendSuperCallLocation = getTypeScriptExtendSuperCallLocation(extendPath, config);
var extendParent = _getParent(path, 1, config);
if (types.isCallExpression(extendParent)) {
declaredClassName = extendParent.node.arguments[0].name;
}
var decorateNodes = traverseForDecorate(path, config, 3);
var isDecoratedWithExtend = false,
customExtendDecoratorName,
customExtendDecoratorValue,
implementedInterfaces = [];
if (!decorateNodes) {
// 7 -> Takes 7 levels up to get to the scope where the class is declared
decorateNodes = traverseForDecorateSpecial(path, config, 7);
}
if (decorateNodes) {
for (var i in decorateNodes) {
var currentDecorator = decorateNodes[i];
if (types.isCallExpression(currentDecorator)) {
// Interfaces/Implements
if (currentDecorator.callee.name === config.interfacesDecoratorName) {
currentDecorator.callee.skipMeOnVisit = true;
var interfaces = currentDecorator.arguments[0].elements;
for (var i in interfaces) {
var interfaceName = _getWholeInterfaceNameFromInterfacesNode(interfaces[i]);
implementedInterfaces.push(interfaceName);
}
}
// JavaProxy
if (currentDecorator.callee.name === config.extendDecoratorName) {
currentDecorator.callee.skipMeOnVisit = true;
isDecoratedWithExtend = true;
customExtendDecoratorName = config.extendDecoratorName === undefined ? defaultExtendDecoratorName : config.extendDecoratorName;
customExtendDecoratorValue = currentDecorator.arguments[0].value;
}
}
}
}
if (isDecoratedWithExtend) {
traverseJavaProxyExtend(customExtendDecoratorValue, config, customExtendDecoratorName, extendClass, overriddenMethodNames, implementedInterfaces);
} else {
var lineToWrite;
lineToWrite = _generateLineToWrite("", extendClass, overriddenMethodNames, { file: config.filePath, line: typescriptClassExtendSuperCallLocation.line, column: typescriptClassExtendSuperCallLocation.column, className: declaredClassName }, "", implementedInterfaces);
if (config.logger) {
config.logger.info(lineToWrite)
}
normalExtendsArr.push(lineToWrite);
}
}
function getTypeScriptExtendSuperCallLocation(extendPath, config) {
var constructorFunctionName;
var returnIdentifierName;
var superCalleeStartColumn;
var superCalleeLine;
var locationAssigned = false;
// checks if constructor function and return identifiers match in a transpiled typescript class extend
if (extendPath.node.body && extendPath.node.body.length >= 3) {
if (types.isFunctionDeclaration(extendPath.node.body[1]) && extendPath.node.body[1].id)
constructorFunctionName = extendPath.node.body[1].id.name;
if (types.isReturnStatement(extendPath.node.body[extendPath.node.body.length - 1]))
returnIdentifierName = extendPath.node.body[extendPath.node.body.length - 1].argument.name;
}
// checks the typescript-extended class for a strict format, and a super call/apply inside
/**
* function MyBtn() {
var _this = _super.call(this) || this;
return global.__native(_this);
}
**UGLIFIED extended class**
function t(t) {
var r = e.call(this) || this;
r.textBase = t;
return global.__native(r);
}
*/
if (constructorFunctionName === returnIdentifierName && !!constructorFunctionName) {
var constructorFunctionScope = extendPath.node.body[1];
// the 'super' variable will always be passed as the second argument to the __extends call
// try to find the 'super' identifier which may be uglified
var superVariableIdentifier = getSuperVariableIdentifier(extendPath.node.body[0]);
//// helper functions to find the correct super.call(this) node
function getSuperVariableIdentifier(__extendsNode) {
if (types.isExpressionStatement(__extendsNode) && types.isCallExpression(__extendsNode.expression)) {
var __extendsCallArguments = __extendsNode.expression.arguments;
if (__extendsCallArguments.length == 2) {
return __extendsCallArguments[1].name;
}
}
}
/** Getting the super.call node from assignment
if expressionStatement => check possibleExpressionStatements[0]
if possibleExpressionStatements[0].expression is assignment expression
if possibleExpressionStatements[0].expression.right is logical expression
if possibleExpressionStatements[0].expression.right.left is Call expression
if possibleExpressionStatements[0].expression.right.left.callee.object.name === superVariableIdentifier
if the above is valid, then variableRHS = possibleVariableDeclarations[0].expression.right.left
*/
function getThisAssignmentSuperCallLineNode(nodes, superIdentifier) {
var matchingNodes = nodes.filter(
(node) => {
return types.isAssignmentExpression(node.expression) &&
types.isLogicalExpression(node.expression.right) &&
types.isCallExpression(node.expression.right.left) &&
node.expression.right.left.callee.object.name === superIdentifier;
});
return matchingNodes.length > 0 ? matchingNodes[0].expression.right.left : null;
}
/** Getting the super.call node from declaration
if variableDeclaration => check possibleVariableDeclarations[0].declarations[0].init isn't null
if possibleNodesForSuperCall[0].declarations[0].init is logical expression
if possibleNodesForSuperCall[0].declarations[0].init.left is Call Expression
if possibleNodesForSuperCall[0].declarations[0].init.left.callee.object.name === superVariableIdentifier
if the above is valid, then variableRHS = possibleVariableDeclarations[0].declarations[0].init.left
*/
function getThisDeclarationSuperCallLineNode(nodes, superIdentifier) {
var matchingNodes = nodes.filter(
(node) => {
return types.isLogicalExpression(node.declarations[0].init) &&
types.isCallExpression(node.declarations[0].init.left) &&
node.declarations[0].init.left.callee.object.name === superIdentifier;
});
return matchingNodes.length > 0 ? matchingNodes[0].declarations[0].init.left : null;
}
////
if (superVariableIdentifier) {
var possibleVariableDeclarations = [];
var possibleExpressionStatements = [];
constructorFunctionScope.body.body.forEach(node => {
if (types.isVariableDeclaration(node)) {
possibleVariableDeclarations.push(node);
} else if (types.isExpressionStatement(node)) {
possibleExpressionStatements.push(node);
}
});
if (possibleVariableDeclarations.length > 0 || possibleExpressionStatements.length > 0) {
var superCallRHS = getThisDeclarationSuperCallLineNode(possibleVariableDeclarations, superVariableIdentifier) ||
getThisAssignmentSuperCallLineNode(possibleExpressionStatements, superVariableIdentifier);
if (superCallRHS) {
var superCallee = superCallRHS.callee.property;
superCalleeStartColumn = superCallee.loc.start.column + 1;
superCalleeLine = superCallee.loc.start.line;
locationAssigned = true;
}
}
}
}
if (!locationAssigned) {
config.logger.info(UNSUPPORTED_TYPESCRIPT_EXTEND_FORMAT_MESSAGE + " ctor function name: " + constructorFunctionName);
}
return {
line: superCalleeLine,
column: superCalleeStartColumn
};
}
function traverseForDecorate(path, config, depth) {
var iifeRoot = _getParent(path, depth)
var body = iifeRoot.node.body;
for (var index in body) {
var ci = body[index];
if (isDecorateStatement(ci)) {
// returns the node of the decorate (node.expression.right.callee)
// __decorate([..])
return getRightExpression(ci.expression).arguments[0].elements;
}
}
return null;
}
function traverseForDecorateSpecial(path, config, depth) {
var iifeRoot = _getParent(path, depth);
var sibling = iifeRoot.getSibling(iifeRoot.key + 1).node;
if (sibling) {
if (isDecorateStatement(sibling)) {
// returns the node of the decorate (node.expression.right.callee)
// __decorate([..])
return getRightExpression(sibling.expression).arguments[0].elements;
}
}
return null;
}
function getRightExpression(expression) {
if(!expression) {
return null;
}
var rightExpression = expression.right;
// if the right expression is a new assignment, get the right expression from that assignment
while (types.isAssignmentExpression(rightExpression)) {
rightExpression = rightExpression.right;
}
return rightExpression;
}
function isDecorateStatement(node) {
var rightExpression = getRightExpression(node.expression);
return types.isExpressionStatement(node) &&
types.isAssignmentExpression(node.expression) &&
rightExpression.callee &&
rightExpression.callee.name === "__decorate" &&
rightExpression.arguments &&
types.isArrayExpression(rightExpression.arguments[0])
}
/*
* Traverses the node, which is a "new" expression and find if it's a native interface or not.
* Write results in "interfacesArr".
*/
function traverseInterface(path, config) {
if (!config.interfaceNames) {
throw "JSParser Error: No interface names are provided! You can pass them in config.interfaceNames as an array!"
}
var o = path.node.callee,
interfaceArr = _getWholeName(o),
foundInterface = false,
interfaceNames = config.interfaceNames
var currentInterface = interfaceArr.reverse().join(".");
for (var i in interfaceNames) {
var interfaceName = interfaceNames[i].trim();
if (interfaceName === currentInterface) {
currentInterface = interfaceName;
foundInterface = true;
break;
}
}
if (foundInterface) {
var arg0 = "",
arg1;
if (path.node.arguments.length === 1) {
arg1 = path.node.arguments[0];
} else if (path.node.arguments.length === 2) {
arg0 = path.node.arguments[0];
arg1 = path.node.arguments[1];
} else {
throw {
message: "JSParser Error: Not enough or too many arguments passed(" + path.node.arguments.length + ") when trying to extend interface: " + interfaceName + " in file: " + config.fullPathName,
errCode: 1
}
}
var isCorrectInterfaceName = _testClassName(arg0.value);
var overriddenInterfaceMethods = _getOverriddenMethods(arg1, config);
var extendInfo = {
file: config.filePath,
line: path.node.loc.start.line,
column: path.node.loc.start.column + columnOffset,
className: (isCorrectInterfaceName ? arg0.value : "")
}
var lineToWrite = _generateLineToWrite("", currentInterface, overriddenInterfaceMethods.join(","), extendInfo, "");
if (config.logger) {
config.logger.info(lineToWrite)
}
interfacesArr.push(lineToWrite)
}
}
/*
* Finds the java proxy name from custom class decorator.
* Write results in "customExtendsArr"
*/
function traverseJavaProxyExtend(path, config, customDecoratorName, extendClass, overriddenMethodNames, implementedInterfaces) {
if (config.logger) {
config.logger.info("\t+in " + customDecoratorName + " anchor");
}
var classNameFromDecorator = path; //_getDecoratorArgument(path, config, customDecoratorName);
var lineToWrite = _generateLineToWrite(classNameFromDecorator, extendClass, overriddenMethodNames, "", config.fullPathName, implementedInterfaces);
if (config.logger) {
config.logger.info(lineToWrite)
}
addCustomExtend(classNameFromDecorator, config.fullPathName, lineToWrite)
}
/*
* Finds the normal extend name, overridden methods and possibly java proxy name from passed node.
* Writes to "customExtendsArr" or "normalExtendsArr".
* Left whole for readability.
*/
function traverseEs5Extend(path, config) {
var callee = path.parent.callee;
if (callee) {
var o = callee.object
extendClass = _getWholeName(o);
var extendArguments = path.parent.arguments;
var arg0,
arg1;
if (extendArguments.length === 1 && types.isObjectExpression(arg0)) {
arg0 = extendArguments[0];
} else if (types.isStringLiteral(arg0)) {
}
var arg0 = "",
arg1;
if (extendArguments.length) {
// Get implementation object when there is only 1 argument
if (extendArguments.length === 1 && types.isObjectExpression(extendArguments[0])) {
arg1 = extendArguments[0];
}
// Get the name of the extended class and the implementation object when both arguments are present
else if (extendArguments.length === 2) {
if (types.isStringLiteral(extendArguments[0]) && types.isObjectExpression(extendArguments[1])) {
arg0 = extendArguments[0];
arg1 = extendArguments[1];
}
} else {
// don't throw here, because there can be a valid js extend that has nothing to do with NS
return;
throw {
message: "JSParser Error: Not enough or too many arguments passed(" + extendArguments.length + ") when trying to extend class: " + extendClass + " in file: " + config.fullPathName,
errCode: 1
}
}
} else {
// don't throw here, because there can be a valid js extend that has nothing to do with NS
return;
throw {
message: "JSParser Error: You need to call the extend with parameters. Example: '...extend(\"a.b.C\", {...overrides...})') for class: " + extendClass + " in file: " + config.fullPathName,
errCode: 1
}
}
className = arg0.value ? arg0.value : "";
// Get all methods from the implementation object
var methodsAndInterfaces = _getOverridenMethodsAndImplementedInterfaces(arg1, config);
var overriddenMethodNames = methodsAndInterfaces[0];
var implementedInterfaces = methodsAndInterfaces[1];
var isCorrectExtendClassName = _testJavaProxyName(className);
var isCorrectClassName = _testClassName(className);
if (className && !isCorrectClassName && !isCorrectExtendClassName) {
throw {
message: "JSParser Error: The 'extend' you are trying to make has an invalid name. Example: '...extend(\"a.b.C\", {...overrides...})'), for class: " + extendClass + " file: " + config.fullPathName,
errCode: 1
}
}
var lineToWrite = "";
if (isCorrectExtendClassName) {
if (config.logger) {
config.logger.info(lineToWrite)
}
var classNameFromDecorator = isCorrectExtendClassName ? className : "";
lineToWrite = _generateLineToWrite(classNameFromDecorator, extendClass.reverse().join("."), overriddenMethodNames, "", config.fullPathName, implementedInterfaces);
addCustomExtend(classNameFromDecorator, config.fullPathName, lineToWrite);
return;
}
if (config.logger) {
config.logger.info(lineToWrite)
}
var extendInfo = {
file: config.filePath,
line: path.node.property.loc.start.line,
column: path.node.property.loc.start.column + columnOffset,
className: className
};
lineToWrite = _generateLineToWrite(isCorrectExtendClassName ? className : "", extendClass.reverse().join("."), overriddenMethodNames, extendInfo, "", implementedInterfaces);
normalExtendsArr.push(lineToWrite)
} else {
// don't throw here, because there can be a valid js extend that has nothing to do with NS
return;
throw {
message: "JSParser Error: You need to call the extend '...extend(\"extend_name\", {...overrides...})'), for class: " + extendClass + " file: " + config.fullPathName,
errCode: 1
}
}
}
/*
* HELPER METHODS
*/
// Returns the Identifier name of a property's key, or null when the property
// is a SpreadElement, has a computed key, or uses a non-Identifier key
// (e.g. StringLiteral, NumericLiteral). Such properties cannot be mapped to
// a Java method binding, so callers should skip them.
function _getIdentifierKeyName(property) {
if (!property || property.computed) {
return null;
}
if (!property.key || !types.isIdentifier(property.key)) {
return null;
}
return property.key.name;
}
function _getOverriddenMethods(node, config) {
var overriddenMethodNames = [];
if (types.isObjectExpression(node)) {
var objectProperties = node.properties;
for (var index in objectProperties) {
var keyName = _getIdentifierKeyName(objectProperties[index]);
if (keyName === null) {
continue;
}
overriddenMethodNames.push(keyName);
}
}
return overriddenMethodNames;
}
// NOTE: It's a near-identical method to _getOverridenMethods for optimisation reasons
// we do not want to check for interfaces while creating an interface
// and likewise, we do not want to iterate twice through the impl. object's properties to read the interfaces
function _getOverridenMethodsAndImplementedInterfaces(node, config) {
var result = [];
var overriddenMethodNames = [];
var implementedInterfaces = [];
var interfacesFound = false;
if (types.isObjectExpression(node)) {
var objectProperties = node.properties;
/*
Iterates through all properties of the implementation object, e.g.
{
method1: function() {
},
method3: function() {
}
}
will get 'method1' and 'method3'
*/
for (var index in objectProperties) {
var keyName = _getIdentifierKeyName(objectProperties[index]);
// Skip spreads, computed keys, and non-Identifier keys — they
// cannot be statically mapped to a Java binding. Without this
// guard, valid (non-NS) `.extend({ ...other, foo })` calls in
// bundled vendor code (e.g. Zod schemas) would crash the parser.
if (keyName === null) {
continue;
}
// if the user has declared interfaces that he is implementing
if (!interfacesFound &&
keyName.toLowerCase() === "interfaces" &&
types.isArrayExpression(objectProperties[index].value)) {
interfacesFound = true;
var interfaces = objectProperties[index].value.elements;
for (var i in interfaces) {
var interfaceName = _getWholeInterfaceNameFromInterfacesNode(interfaces[i]);
implementedInterfaces.push(interfaceName);
}
} else {
overriddenMethodNames.push(keyName)
}
}
}
result.push(overriddenMethodNames);
result.push(implementedInterfaces);
return result;
}
function _getWholeInterfaceNameFromInterfacesNode(node) {
var interfaceName = "";
if (types.isMemberExpression(node)) {
interfaceName += _resolveInterfacePath(node.object);
interfaceName += node.property.name;
}
return interfaceName;
}
function _resolveInterfacePath(node) {
var subNode = "";
if (types.isMemberExpression(node)) {
if (types.isMemberExpression(node.object)) {
subNode += _resolveInterfacePath(node.object);
subNode += node.property.name + ".";
} else {
subNode += node.object.name + ".";
subNode += node.property.name + ".";
}
}
return subNode;
}
function _getWholeName(node) {
var arr = [],
isAndroidInterface = false;
while (node !== undefined) {
if (!types.isMemberExpression(node)) {
if (isAndroidInterface) {
arr.push(node.name)
}
break;
}
isAndroidInterface = true;
arr.push(node.property.name)
node = node.object
}
return arr;
}
function _getArgumentFromNodeAsString(path, count, config) {
var extClassArr = [];
var extendedClass = _getParent(path, count, config);
if (extendedClass) {
if (types.isCallExpression(extendedClass.node)) {
var o = extendedClass.node.arguments[0];
} else {
throw {
message: "JSParser Error: Node type is not a call expression. File=" + config.fullPathName + " line=" + path.node.loc.start.line,
errCode: 1
}
}
}
extClassArr = _getWholeName(o);
return extClassArr.reverse().join(".");
}
function _getDecoratorArgument(path, config, customDecoratorName) {
if (path.parent && types.isCallExpression(path.parent)) {
if (path.parent.arguments && path.parent.arguments.length > 0) {
var classNameFromDecorator = path.parent.arguments[0].value
var isCorrectExtendClassName = _testJavaProxyName(classNameFromDecorator);
if (isCorrectExtendClassName) {
return path.parent.arguments[0].value;
} else {
throw {
message: "JSParser Error: The first argument '" + classNameFromDecorator + "' of the " + customDecoratorName + " decorator is not following the right pattern which is: '[namespace.]ClassName'. Example: '" + customDecoratorName + "(\"a.b.ClassName\", {overrides...})', file: " + config.fullPathName,
errCode: 1
}
}
} else {
throw {
message: "JSParser Error: No arguments passed to " + customDecoratorName + " decorator. Example: '" + customDecoratorName + "(\"a.b.ClassName\", {overrides...})', file: " + config.fullPathName,
errCode: 1
}
}
} else {
throw {
message: "JSParser Error: Decorator " + customDecoratorName + " must be called with parameters: Example: '" + customDecoratorName + "(\"a.b.ClassName\", {overrides...})', file: " + config.fullPathName,
errCode: 1
}
}
return undefined;
}
function _getOverriddenMethodsTypescript(path, count) {
var overriddenMethods = [];
var cn = _getParent(path, count)
// this pattern follows typescript generated syntax
for (var item in cn.node.body) {
var ci = cn.node.body[item];
if (types.isExpressionStatement(ci)) {
if (types.isAssignmentExpression(ci.expression)) {
if (ci.expression.left.property) {
overriddenMethods.push(ci.expression.left.property.name)
}
}
}
}
return overriddenMethods;
}
function _getParent(node, numberOfParents, config) {
if (!node) {
throw {
message: "JSParser Error: No parent found for node in file: " + config.fullPathName,
errCode: 1
}
}
if (numberOfParents === 0) {
return node;
}
return _getParent(node.parentPath, --numberOfParents)
}
function _testJavaProxyName(name) {
if (name) {
return /^((\w+\.)+\w+)$/.test(name)
}
return false;
}
function _testClassName(name) {
if (name && name != "") {
return /^(\w+)$/.test(name)
}
return false;
}
function _generateLineToWrite(classNameFromDecorator, extendClass, overriddenMethodNames, extendInfo, filePath, implementedInterfaces = "") {
const extendInfoFile = extendInfo.file ? extendInfo.file.replace(/[-\\/\\. ]/g, "_") : "";
const extendInfoLine = extendInfo.line ? extendInfo.line : "";
const extendInfoColumn = extendInfo.column ? extendInfo.column : "";
const extendInfoNewClassName = extendInfo.className ? extendInfo.className : "";
var lineToWrite = `${extendClass}${ASTERISK_SEPARATOR}` +
`${extendInfoFile}${ASTERISK_SEPARATOR}` +
`${extendInfoLine}${ASTERISK_SEPARATOR}` +
`${extendInfoColumn}${ASTERISK_SEPARATOR}` +
`${extendInfoNewClassName}${ASTERISK_SEPARATOR}` +
`${overriddenMethodNames}${ASTERISK_SEPARATOR}` +
`${classNameFromDecorator}${ASTERISK_SEPARATOR}` +
`${filePath}${ASTERISK_SEPARATOR}` +
`${implementedInterfaces}`
return lineToWrite;
}
function addCustomExtend(param, extendPath, lineToWrite) {
if (customExtendsArrGlobal.indexOf(param) === -1) {
customExtendsArr.push(lineToWrite)
customExtendsArrGlobal.push(param)
} else {
console.log("Warning: there already is an extend called " + param + ".")
if (extendPath.indexOf("tns_modules") === -1) {
// app folder will take precedence over tns_modules
console.log("Warning: The static binding generator will generate extend from:" + extendPath + " implementation")
customExtendsArr.push(lineToWrite)
customExtendsArrGlobal.push(param)
}
}
}
return {
es5Visitor: es5Visitor
}
})();
module.exports = es5_visitors;