-
Notifications
You must be signed in to change notification settings - Fork 0
/
sequence.js
948 lines (882 loc) · 27.5 KB
/
sequence.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
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
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
/**
* Created by tommyZZM on 2015/4/3.
* 根据typescript之间的引用关系排序,从egret框架的编译工具中挖掘出来的。
*/
var eventStream = require('event-stream');
//var gutil = require('gulp-util');
var efile = require("./file.js");
var analysis = require("./analysis.js");
var path = require("path");
/**
* 键是类名,值为这个类依赖的类名列表
*/
var classInfoList;
/**
* 键是文件路径,值为这个类依赖的类文件路径,且所依赖的类都是被new出来的。
*/
var newClassInfoList;
/**
* 键是文件路径,值为这个类实例化时需要依赖的类文件路径列表
*/
var subRelyOnInfoList;
/**
* 键为类名,值为这个类所在的文件路径
*/
var classNameToPath;
var noneExportClassName;
/**
* 键是不含命名空间的类名,值是命名空间
*/
var classNameToModule;
/**
* 键为文件路径,值为这个文件包含的类名列表
*/
var pathToClassNames;
/**
* 键为文件路径,值为这个文件依赖的类名列表(从import或全局变量中读取的)
*/
var pathInfoList;
/**
* 键为文件路径,值为这个文件引用的文件列表
*/
var referenceInfoList;
var thmList;
var modeulClassToPath;
var exmlConfig;
/**
* ts关键字
*/
var functionKeys = ["static", "var", "export", "public","protected", "private", "function", "get", "set", "class", "interface","module","extends","implements","super","this"];
var filesToHandle = [];
var filesToHandleDict = {};
function resetCache(){
filesToHandle = [];
filesToHandleDict = {};
classInfoList = {};
newClassInfoList = {};
subRelyOnInfoList = {};
classNameToPath = {};
noneExportClassName = {};
classNameToModule = {};
pathInfoList = {};
pathToClassNames = {};
referenceInfoList = {};
thmList = [];
modeulClassToPath = null;
}
function SequenceTypeScriptFile(){
resetCache();
var onFile = function (file) {
var filePath = path.normalize(file.path);
//gutil.log('pushing ' + filePath + ' into the map');
filesToHandle.push({text:file.contents.toString(),path:filePath});
filesToHandleDict[filePath] = file;
};
return eventStream.through(onFile, sequence);
}
/**
* 创建manifest列表
*/
function sequence(){
//gutil.log('sequencing ...');
var i,length = filesToHandle.length;
for (i = 0; i < length; i++) {
var file = filesToHandle[i];
//console.log(file.text);
readClassNamesFromTs(file.text,file.path);
}
var sortresult = sortFileList(filesToHandle);
//gutil.log(sortresult,filesToHandle)
for(i=0;i<sortresult.length;i++){
//gutil.log(sortresult[i]);
this.emit("data",filesToHandleDict[sortresult[i]])
}
this.emit('end');
}
/**
* 按照引用关系排序指定的文件列表
*/
function sortFileList(list){
var file,ext;
var length = list.length;
for (var i = 0; i < length; i++) {
file = list[i];
ext = efile.getExtension(file.path).toLowerCase();
//console.log(file.path)
if(ext=="ts"){
readRelyOnFromTs(file.text,file.path);
}
}
for (i = 0; i < length; i++) {
file = list[i];
ext = efile.getExtension(file.path).toLowerCase();
if(ext=="ts"){
readReferenceFromTs(file.text,file.path);
}
}
var paths = [];
var path = file.path;
//把所有引用关系都合并到pathInfoList里,并把类名替换为对应文件路径。
for (var path in pathInfoList) {
paths.push(path);
var list = pathInfoList[path];
var classList = pathToClassNames[path];
length = classList.length;
for (i = 0; i < length; i++) {
var className = classList[i];
var relyOnList = classInfoList[className];
if(relyOnList){
var len = relyOnList.length;
for (var j = 0; j < len; j++) {
className = relyOnList[j];
if (list.indexOf(className) == -1) {
list.push(className);
}
}
}
}
length = list.length;
for (i = length - 1; i >= 0; i--) {
className = list[i].path;
var relyPath = classNameToPath[className];
if (relyPath && relyPath != path) {
list[i].path = relyPath;
} else {
list.splice(i, 1);
}
}
}
var pathList = sortOnPathLevel(paths,pathInfoList,true);
var gameList = [];
for (var key in pathList) {
list = pathList[key];
list = sortOnReference(list);
gameList = list.concat(gameList);
}
return gameList;
}
/**
* 按照引用关系进行排序
*/
function sortOnReference(list){
var pathRelyInfo = {};
var length = list.length;
for(var i=0;i<length;i++){
var path = list[i];
var refList = [];
var reference = referenceInfoList[path];
for(var j=list.length-1;j>=0;j--){
var p = reference[j];
if(list.indexOf(p)!=-1){
refList.push(p);
}
}
pathRelyInfo[path] = refList;
}
var pathList = sortOnPathLevel(list,pathRelyInfo,false);
var gameList = [];
for (var key in pathList) {
list = pathList[key];
list.sort();
gameList = list.concat(gameList);
}
return gameList;
}
/**
* 根据引用深度排序
*/
function sortOnPathLevel(list,pathRelyInfo,throwError){
var length = list.length;
var pathLevelInfo = {};
for(var i=0;i<length;i++){
var path = list[i];
setPathLevel(path, 0, pathLevelInfo, [path],pathRelyInfo,throwError);
}
//pathList里存储每个level对应的文件路径列表
var pathList = [];
for (path in pathLevelInfo) {
var level = pathLevelInfo[path];
if (pathList[level]) {
pathList[level].push(path);
}
else {
pathList[level] = [path];
}
}
return pathList;
}
/**
* 设置文件引用深度
*/
function setPathLevel(path, level, pathLevelInfo, map,pathRelyInfo,throwError,checkNew) {
if (pathLevelInfo[path] == null) {
pathLevelInfo[path] = level;
} else {
if(pathLevelInfo[path]<level){
pathLevelInfo[path] = level;
}
else{
return;
}
}
var list = pathRelyInfo[path];
if(checkNew){
var list = list.concat();
var subList = subRelyOnInfoList[path];
if(subList){
for(i = subList.length-1;i>=0;i--){
relyPath = subList[i];
if(list.indexOf(relyPath)==-1){
list.push(relyPath);
}
}
}
}
if(throwError){
var newList = newClassInfoList[path];
}
var length = list.length;
for (var i = 0; i < length; i++) {
var relyPath = list[i];
if (map.indexOf(relyPath) != -1) {
if(throwError){
map.push(relyPath);
console.log("1103: error 类文件之间存在循环依赖,请检查类的继承关系或静态变量的初始化引用。",path);
//globals.exit(1103, "There is a circular dependency amongthe {0}: error files, please check the class inheritance or static variable initialization references.");
process.exit(1103);
}
break;
}
var checkNewFlag = checkNew||(newList&&newList.indexOf(relyPath)!=-1);
setPathLevel(relyPath, level + 1, pathLevelInfo, map.concat(relyPath),pathRelyInfo,throwError,checkNewFlag);
}
}
/**
* 读取一个TS文件引用的类名列表
*/
function readReferenceFromTs(text,path){
//var text = efile.read(path);
var orgText = analysis.removeCommentExceptQuote(text);
text = analysis.removeComment(text,path);
var block = "";
var tsText = "";
var moduleList = {};
while (text.length > 0) {
var index = text.indexOf("{");
if (index == -1) {
if (tsText) {
tsText = "";
}
break;
} else {
var preStr = text.substring(0, index);
tsText += preStr;
text = text.substring(index);
index = analysis.getBracketEndIndex(text);
if (index == -1) {
break;
}
block = text.substring(1, index);
text = text.substring(index + 1);
var ns = analysis.getLastWord(preStr);
preStr = analysis.removeLastWord(preStr,ns);
var word = analysis.getLastWord(preStr);
if (word == "module") {
if (tsText) {
tsText = "";
}
if(!moduleList[ns]){
moduleList[ns] = block;
}
else{
moduleList[ns] += block;
}
} else {
tsText += "{" + block + "}";
}
}
}
var list = [];
checkAllClassName(classNameToPath,path,list,moduleList,orgText);
var length = list.length;
for(var i=0;i<length;i++){
list[i] = classNameToPath[list[i]];
}
if(modeulClassToPath){
var newList = []
checkAllClassName(modeulClassToPath,path,newList,moduleList,orgText);
length = newList.length;
for(i=0;i<length;i++){
var value = modeulClassToPath[newList[i]];
if(list.indexOf(value)==-1){
list.push(value);
}
}
}
referenceInfoList[path] = list;
}
function checkAllClassName(classNameToPath,path,list,moduleList,orgText){
var exclude = pathToClassNames[path];
findClassInLine(orgText,exclude,"",list,classNameToPath)
for(var ns in moduleList){
var text = moduleList[ns];
findClassInLine(text,exclude,ns,list,classNameToPath);
}
}
/**
* 读取一个ts文件引用的类列表
*/
function readClassNamesFromTs(text,path) {
//var text = efile.read(path);
text = analysis.removeComment(text,path);
//console.log(text)
var list = [];
var noneExportList = [];
analyzeModule(text,list,noneExportList,"");
if(noneExportList.length>0){
noneExportClassName[path] = noneExportList;
}
//console.log("readClassNamesFromTs",list,path,text)
var length = list.length;
for (var i = 0; i < length; i++) {
var className = list[i]
if(classNameToPath[className]){
checkRepeatClass(path,classNameToPath[className],className);
}
classNameToPath[className] = path;
}
pathToClassNames[path] = list;
}
function checkRepeatClass(newPath,oldPath,className){
if(newPath==oldPath||!newPath||!oldPath){
return;
}
var index = newPath.lastIndexOf(".");
var p1 = newPath.substring(0,index);
index = oldPath.lastIndexOf(".");
var p2 = oldPath.substring(0,index);
if(p1==p2){
return;
}
var list = noneExportClassName[newPath];
if(list&&list.indexOf(className)!=-1){
return;
}
list = noneExportClassName[oldPath];
if(list&&list.indexOf(className)!=-1){
return;
}
console.log(1308,"repeat class",className,newPath,oldPath);
}
/**
* 分析一个ts文件
*/
function analyzeModule(text,list,noneExportList,moduleName)
{
var block = "";
//console.log("analyzeModule",text);
while (text.length > 0){
var index = analysis.getFirstVariableIndex("module",text);
if (index == -1){
readClassFromBlock(text,list,noneExportList,moduleName);
break;
}
else{
var preStr = text.substring(0, index).trim();
if(preStr){
readClassFromBlock(preStr,list,noneExportList,moduleName);
}
text = text.substring(index+6);
var ns = analysis.getFirstWord(text);
ns = analysis.trimVariable(ns);
index = analysis.getBracketEndIndex(text);
if (index == -1){
break;
}
block = text.substring(0, index);
text = text.substring(index + 1);
index = block.indexOf("{");
block = block.substring(index+1);
if(moduleName){
ns = moduleName+"."+ns;
}
analyzeModule(block,list,noneExportList,ns);
}
}
}
/**
* 从代码块中读取类名,接口名,全局函数和全局变量,代码块为一个Module,或类外的一段全局函数定义
*/
function readClassFromBlock(text,list,noneExportList,ns){
var newText = "";
while(text.length>0){
var index = text.indexOf("{");
if(index==-1){
newText += text;
break;
}
newText += text.substring(0,index);
text = text.substring(index);
index = analysis.getBracketEndIndex(text);
if(index==-1){
newText += text;
break;
}
text = text.substring(index+1);
}
text = newText;
while (text.length > 0){
var noneExported = false;
var classIndex = analysis.getFirstVariableIndex("class", text);
if(classIndex==-1){
classIndex = Number.POSITIVE_INFINITY;
}
else if(ns){
if(analysis.getLastWord(text.substring(0,classIndex))!="export")
{
noneExported = true;
}
}
var interfaceIndex = analysis.getFirstVariableIndex("interface", text);
if(interfaceIndex==-1){
interfaceIndex = Number.POSITIVE_INFINITY;
}
else if(ns){
if(analysis.getLastWord(text.substring(0,interfaceIndex))!="export")
{
noneExported = true;
}
}
var enumIndex = getFirstKeyWordIndex("enum",text,ns);
var functionIndex = getFirstKeyWordIndex("function", text,ns);
var varIndex = getFirstKeyWordIndex("var", text,ns);
classIndex = Math.min(interfaceIndex,classIndex,enumIndex,functionIndex,varIndex);
if (classIndex == Number.POSITIVE_INFINITY){
break;
}
var isVar = (classIndex==varIndex);
var keyLength = 5;
switch (classIndex){
case varIndex:
keyLength = 3;
break;
case interfaceIndex:
keyLength = 9;
break;
case functionIndex:
keyLength = 8;
break;
case enumIndex:
keyLength = 4;
break;
}
text = text.substring(classIndex + keyLength);
var word = analysis.getFirstVariable(text);
if (word) {
var className;
if (ns){
className = ns + "." + word;
}
else{
className = word;
}
if (list.indexOf(className) == -1){
list.push(className);
if(noneExported){
noneExportList.push(className);
}
if(ns){
var nsList = classNameToModule[word];
if(!nsList){
nsList = classNameToModule[word] = [];
}
if(nsList.indexOf(ns)==-1){
nsList.push(ns);
}
}
}
text = analysis.removeFirstVariable(text);
}
if(isVar){
classIndex = text.indexOf("\n");
if(classIndex==-1){
classIndex = text.length;
}
text = text.substring(classIndex);
}
else{
classIndex = analysis.getBracketEndIndex(text);
text = text.substring(classIndex + 1);
}
}
}
/**
* 读取第一个关键字的索引
*/
function getFirstKeyWordIndex(key,text,ns){
var index = analysis.getFirstVariableIndex(key, text);
if(index==-1){
index = Number.POSITIVE_INFINITY;
}
else if(ns){
if(analysis.getLastWord(text.substring(0,index))!="export")
{
index = Number.POSITIVE_INFINITY;
}
}
return index;
}
/**
* 读取一个ts文件引用的类列表
*/
function readRelyOnFromTs(text,path) {
var fileRelyOnList = [];
//var text = efile.read(path);
text = analysis.removeComment(text,path);
readRelyOnFromImport(text, fileRelyOnList);
analyzeModuleForRelyOn(text,path,fileRelyOnList,"");
pathInfoList[path] = fileRelyOnList;
}
/**
* 从import关键字中分析引用关系
*/
function readRelyOnFromImport(text, fileRelyOnList) {
while (text.length > 0) {
var index = analysis.getFirstVariableIndex("import", text);
if (index == -1) {
break;
}
text = text.substring(index + 6);
text = analysis.removeFirstVariable(text).trim();
if (text.charAt(0) != "=") {
continue;
}
text = text.substring(1);
var className = analysis.getFirstWord(text);
className = analysis.trimVariable(className);
if (fileRelyOnList.indexOf(className) == -1) {
fileRelyOnList.push(className);
}
}
}
/**
* 分析一个ts文件
*/
function analyzeModuleForRelyOn(text,path,fileRelyOnList,moduleName){
while (text.length > 0){
var index = analysis.getFirstVariableIndex("module",text);
if (index == -1){
readRelyOnFromBlock(text,path,fileRelyOnList,moduleName);
break;
}
else{
var preStr = text.substring(0, index).trim();
if(preStr){
readRelyOnFromBlock(preStr,path,fileRelyOnList,moduleName);
}
text = text.substring(index+6);
var ns = analysis.getFirstWord(text);
ns = analysis.trimVariable(ns);
index = analysis.getBracketEndIndex(text);
if (index == -1){
break;
}
var block = text.substring(0, index+1);
text = text.substring(index + 1);
if(moduleName){
ns = moduleName+"."+ns;
}
analyzeModuleForRelyOn(block,path,fileRelyOnList,ns);
}
}
}
/**
* 从代码块中分析引用关系,代码块为一个Module,或类外的一段全局函数定义
*/
function readRelyOnFromBlock(text, path,fileRelyOnList,ns) {
while (text.length > 0) {
var index = analysis.getFirstVariableIndex("class", text);
if(index==-1){
escapFunctionLines(text,pathToClassNames[path],ns,fileRelyOnList);
break;
}
var keyLength = 5;
var preStr = text.substring(0, index + keyLength);
escapFunctionLines(preStr,pathToClassNames[path],ns,fileRelyOnList)
text = text.substring(index + keyLength);
var word = analysis.getFirstVariable(text);
if (word) {
var className;
if (ns) {
className = ns + "." + word;
} else {
className = word;
}
var relyOnList = classInfoList[className];
if (!relyOnList) {
relyOnList = classInfoList[className] = [];
}
text = analysis.removeFirstVariable(text);
word = analysis.getFirstVariable(text);
if (word == "extends") {
text = analysis.removeFirstVariable(text);
word = analysis.getFirstWord(text);
word = analysis.trimVariable(word);
word = getFullClassName(word,ns);
if (relyOnList.indexOf(word) == -1) {
relyOnList.push(word);
}
}
}
index = analysis.getBracketEndIndex(text);
var classBlock = text.substring(0, index + 1);
text = text.substring(index + 1);
index = classBlock.indexOf("{");
classBlock = classBlock.substring(index);
getSubRelyOnFromClass(classBlock,ns,className);
getRelyOnFromStatic(classBlock, ns,className, relyOnList);
}
}
/**
* 根据类类短名,和这个类被引用的时所在的module名来获取完整类名。
*/
function getFullClassName(word,ns) {
if (!ns||!word) {
return word;
}
var prefix = "";
var index = word.lastIndexOf(".");
var nsList;
if(index==-1){
nsList = classNameToModule[word];
}
else{
prefix = word.substring(0,index);
nsList = classNameToModule[word.substring(index+1)];
}
if(!nsList){
return word;
}
var length = nsList.length;
var targetNs = "";
for(var k=0;k<length;k++){
var superNs = nsList[k];
if(prefix){
var tail = superNs.substring(superNs.length-prefix.length);
if(tail==prefix){
superNs = superNs.substring(0,superNs.length-prefix.length-1);
}
else{
continue;
}
}
if(ns.indexOf(superNs)==0){
if(superNs.length>targetNs.length){
targetNs = superNs;
}
}
}
if(targetNs){
word = targetNs+"."+word;
}
return word;
}
/**
* 从类代码总读取构造函数和成员变量实例化的初始值。
*/
function getSubRelyOnFromClass(text,ns, className) {
if(!text){
return;
}
text = text.substring(1,text.length-1);
var list = [];
var functMap = {};
while (text.length > 0) {
var index = analysis.getBracketEndIndex(text);
if (index == -1) {
index = text.length;
}
var codeText = text.substring(0,index+1);
text = text.substring(index+1);
index = codeText.indexOf("{");
if(index==-1){
index = codeText.length;
}
var funcText = codeText.substring(index);
codeText = codeText.substring(0,index);
index = analysis.getBracketStartIndex(codeText,"(",")");
if(funcText&&index!=-1){
codeText = codeText.substring(0,index);
var word = analysis.getLastWord(codeText);
if(word=="constructor"){
word = "__constructor";
}
functMap[word] = funcText;
}
findClassInLine(codeText,[className],ns,list,classNameToPath);
}
readSubRelyOnFromFunctionCode("__constructor",functMap,ns,className,list);
for(var i=list.length- 1;i>=0;i--){
var name = list[i];
var path = classNameToPath[name];
if(path){
list[i] = path;
}
else{
list.splice(i,1);
}
}
path = classNameToPath[className];
subRelyOnInfoList[path] = list;
}
/**
* 从构造函数开始递归查询初始化时被引用过的类。
*/
function readSubRelyOnFromFunctionCode(funcName,functMap,ns,className,list){
var text = functMap[funcName];
if(!text)
return;
delete functMap[funcName];
findClassInLine(text,[className],ns,list,classNameToPath);
for (funcName in functMap){
if(text.indexOf(funcName+"(")!=-1&&analysis.containsVariable(funcName,text)){
readSubRelyOnFromFunctionCode(funcName,functMap,ns,className,list);
}
}
}
/**
* 从代码的静态变量中读取依赖关系
*/
function getRelyOnFromStatic(text,ns, className, relyOnList) {
var newList = [];
while (text.length > 0) {
var index = analysis.getFirstVariableIndex("static", text);
if (index == -1) {
break;
}
text = text.substring(index);
text = trimKeyWords(text);
text = analysis.removeFirstVariable(text).trim();
if (text.charAt(0) == "(") {
continue;
}
if (text.charAt(0) == ":") {
text = text.substring(1);
while (text.length > 0) {
text = analysis.removeFirstVariable(text).trim();
if (text.charAt(0) != ".") {
break;
}
text = text.substring(1).trim();
}
}
if (text.charAt(0) != "=") {
continue;
}
text = text.substring(1).trim();
index = text.indexOf("\n");
var line = text;
if(index!=-1){
line = text.substring(0,index);
text = text.substring(index);
}
if(line.indexOf("new")==0){
var code = analysis.removeFirstVariable(line).trim();
index = code.indexOf("(");
if(index!=-1){
code = code.substring(0,index);
}
code = analysis.trimVariable(code);
code = getFullClassName(code,ns);
var path = classNameToPath[code];
if(path&&code!=className&&newList.indexOf(path)==-1){
newList.push(path);
}
}
escapFunctionLines(line,[className],ns,relyOnList);
}
path = classNameToPath[className];
newClassInfoList[path] = newList;
}
/**
* 排除代码段里的全局函数块。
*/
function escapFunctionLines(text,classNames,ns,relyOnList){
while(text.length>0){
var index = analysis.getFirstVariableIndex("function",text);
if(index==-1){
findClassInLine(text,classNames,ns,relyOnList,classNameToPath);
break;
}
findClassInLine(text.substring(0,index),classNames,ns,relyOnList,classNameToPath);
text = text.substring(index);
index = analysis.getBracketEndIndex(text);
if(index==-1){
break;
}
text = text.substring(index);
}
}
function findClassInLine(text,classNames,ns,relyOnList,classNameToPath){
var word = "";
var length = text.length;
for (var i = 0; i < length; i++) {
var char = text.charAt(i);
if (char <= "Z" && char >= "A" || char <= "z" && char >= "a" || char <= "9" && char >= "0" || char == "_" || char == "$"||char==".") {
word += char;
} else if(word){
if(functionKeys.indexOf(word)==-1&&classNames.indexOf(word)==-1){
var found = false;
var names;
if(word.indexOf(".")!=-1) {
names = word.split(".");
}
else{
names = [word];
}
var len = names.length;
for(var j=0;j<len;j++){
if(j==0)
word = names[0];
else
word += "."+names[j];
var path = classNameToPath[word];
if(path&&typeof(path)=="string"&&classNames.indexOf(word)==-1){
found = true;
break;
}
if(ns){
word = ns+"."+word;
path = classNameToPath[word];
if(path&&typeof(path)=="string"&&classNames.indexOf(word)==-1){
found = true;
break;
}
}
}
if(found){
if (relyOnList.indexOf(word) == -1) {
relyOnList.push(word);
}
}
}
word = "";
}
}
}
/**
* 删除字符串开头的所有关键字
*/
function trimKeyWords(codeText) {
codeText = codeText.trim();
var word;
while (codeText.length > 0) {
word = analysis.getFirstVariable(codeText);
if (functionKeys.indexOf(word) == -1) {
break;
}
codeText = analysis.removeFirstVariable(codeText, word);
}
return codeText;
}
module.exports = SequenceTypeScriptFile;