forked from fivetran/typescript-closure-tools
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.min.js
16914 lines (14755 loc) · 591 KB
/
main.min.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
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
(function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s})({1:[function(require,module,exports){
function set_deep(parent, keys, value) {
for (var i = 0; i < keys.length - 1; i++)
parent = parent[keys[i]] || (parent[keys[i]] = {});
parent[keys[i]] = value;
}
/**
* Combine classes, interfaces, and modules
*
* @param file
* @returns {Symbols}
*/
function members(file) {
var acc = {
classes: {},
modules: {}
};
Object.keys(file).forEach(function (name) {
var symbol = file[name];
// Class and constructor
if (symbol.jsdoc.tags.some(function (t) { return t.title === 'interface'; }) ||
symbol.jsdoc.tags.some(function (t) { return t.title === 'constructor'; })) {
acc.classes[name] = { '': symbol };
}
else if (name.indexOf('.prototype.') !== -1) {
var parts = name.split('.prototype.');
var className = parts[0];
var memberName = parts[1];
if (acc.classes[className])
acc.classes[className][memberName] = symbol;
}
else {
var dot = name.lastIndexOf('.');
var moduleName = name.substring(0, dot);
var memberName = name.substring(dot + 1);
if (!acc.modules[moduleName])
acc.modules[moduleName] = {};
acc.modules[moduleName][memberName] = symbol;
}
});
return acc;
}
exports.members = members;
},{}],2:[function(require,module,exports){
/// <reference path="../index/node.d.ts"/>
var fs = require('fs');
var parser = require('./parser');
var combine = require('./combine');
var options = require('./options');
var fileByProvide = {};
var providesByFile = {};
function parent_symbol(name) {
var dot = name.lastIndexOf('.');
return name.substring(0, dot);
}
/**
* @param symbolName
* @returns file that goog.provide's name
*/
function file(symbolName) {
if (symbolName)
return fileByProvide[symbolName] || file(parent_symbol(symbolName));
else
return null;
}
exports.file = file;
/**
* @param fileName
* @returns value of name
*/
function symbols(fileName) {
if (!providesByFile[fileName])
providesByFile[fileName] = combine.members(parser.jsdoc(fs.readFileSync(fileName, 'utf8')));
if (providesByFile[fileName])
return providesByFile[fileName];
}
exports.symbols = symbols;
function symbol(symbolName) {
var fileName = file(symbolName);
if (fileName) {
var s = symbols(fileName);
return s.classes[symbolName] || s.modules[symbolName];
}
else
return null;
}
exports.symbol = symbol;
function member(symbolName) {
var fileName = file(symbolName);
if (fileName) {
var s = symbols(fileName);
var dot = symbolName.lastIndexOf('.');
var moduleName = symbolName.substring(0, dot);
var memberName = symbolName.substring(dot + 1);
var moduleValue = s.modules[moduleName] || {};
return moduleValue[memberName] || null;
}
else
return null;
}
exports.member = member;
// If user gives a TSV file with provided symbols, use that
if (options.provides) {
fs.readFileSync(options.provides, 'utf8').split('\n').forEach(function (line) {
var columns = line.split('\t');
var file = columns[0];
var symbol = columns[1];
fileByProvide[symbol] = file;
});
}
else {
options.todo.forEach(function (todo) {
var found = symbols(todo.input);
var classNames = Object.keys(found.classes);
var moduleNames = Object.keys(found.modules);
var names = classNames.concat(moduleNames);
names.forEach(function (name) { return fileByProvide[name] = todo.input; });
});
}
},{"./combine":1,"./options":5,"./parser":7,"fs":9}],3:[function(require,module,exports){
/// <reference path="../index/node.d.ts"/>
/// <reference path="../index/doctrine.d.ts"/>
/// <reference path="../index/esprima.d.ts"/>
/// <reference path="../index/escodegen.d.ts"/>
var finder = require('./finder');
var escodegen = require('escodegen');
var reserved = [
'break',
'case',
'catch',
'continue',
'debugger',
'default',
'delete',
'do',
'else',
'finally',
'for',
'function',
'if',
'in',
'instanceof',
'new',
'return',
'switch',
'this',
'throw',
'try',
'typeof',
'var',
'void',
'while',
'with',
'class',
'enum',
'export',
'extends',
'import',
'super'
];
function last(values) {
return values[values.length - 1];
}
function find(values, predicate) {
for (var i = 0; i < values.length; i++) {
if (predicate(values[i]))
return values[i];
}
return null;
}
function get_in(object, keys) {
for (var i = 0; i < keys.length; i++)
if (object instanceof Object)
object = object[keys[i]];
return object;
}
function generate_type_name(name) {
if (name === 'Array')
return 'any[]';
else
return name;
}
function generate_dictionary_type(applications) {
var tKey = generate_type(applications[0]);
var tValue = generate_type(applications[1]);
// Typescript only allows string and number in the key position
switch (tKey) {
case 'string':
case 'number':
break;
default:
tKey = 'string';
}
return '{ [key: ' + tKey + ']: ' + tValue + ' }';
}
function generate_type_application(expression, applications) {
if (expression.name === 'Array')
return generate_type(applications[0]) + '[]';
else if (expression.name === 'Object')
return generate_dictionary_type(applications);
else
return generate_applied_type(expression) + '<' + applications.map(generate_type).join(',') + '>';
}
function generate_indexed_function_parameter(type, index) {
return '_' + index + ': ' + generate_type(type);
}
function generate_function_type(params, result) {
var paramString = '(' + params.map(generate_indexed_function_parameter).join(', ') + ')';
return '{ ' + paramString + ': ' + generate_type(result) + ' }';
}
function comment(text) {
text = text.replace(/\/\*/g, '(');
text = text.replace(/\*\//g, ')');
return '/*' + text + '*/';
}
/**
* Generate a type, without worrying about whether generics have been applied
* @param t
*/
function generate_applied_type(t) {
switch (t.type) {
case 'NameExpression':
references[t.name] = true;
return generate_type_name(t.name);
case 'TypeApplication':
return generate_type_application(t.expression, t.applications);
case 'OptionalType':
case 'NullableType':
case 'NonNullableType':
return generate_type(t.expression);
case 'FunctionType':
return generate_function_type(t.params, t.result);
case 'RestType':
return generate_type(t.expression) + '[]';
case 'UnionType':
return t.elements.map(generate_type).join('|');
case 'AllLiteral':
case 'NullableLiteral':
return 'any';
case 'NullLiteral':
return 'any /*null*/';
case 'UndefinedLiteral':
return 'any /*undefined*/';
case 'VoidLiteral':
return 'void';
case 'RecordType':
return '{ ' + t.fields.map(generate_record_field).join('; ') + ' }';
case 'ArrayType':
return generate_type(t.elements[0]) + '[]';
case 'FieldType':
case 'ParameterType':
default:
throw new Error('Unknown type expression ' + t.type);
}
}
function generate_type(t) {
if (!t)
return 'any /*missing*/';
var result = generate_applied_type(t);
// If t is a name expression, look for a definition and verify templates
if (t.type === 'NameExpression') {
var symbol = finder.symbol(t.name);
var tags = get_in(symbol, ['', 'jsdoc', 'tags']) || [];
var params = [];
tags.filter(function (t) { return t.title === 'template'; })
.forEach(function (tag) {
tag.description.split(',').forEach(function (i) { return params.push('any'); });
});
if (params.length > 0)
return t.name + '<' + params.join(', ') + '>';
}
return result;
}
function safe_name(name) {
if (reserved.indexOf(name) !== -1)
return '_' + name;
else
return name;
}
function generate_function_parameter_name(annotation) {
if (!annotation.type)
return safe_name(annotation.name);
else if (annotation.type.type === 'OptionalType')
return safe_name(annotation.name) + '?';
else if (annotation.type.type === 'RestType')
return '...' + safe_name(annotation.name);
else
return safe_name(annotation.name);
}
function generate_function_parameter(annotation) {
return generate_function_parameter_name(annotation) + ': ' + generate_type(annotation.type);
}
function param_names(docs) {
if (docs.value && docs.value.params)
return docs.value.params.map(function (x) { return x.name; });
else
return docs.jsdoc.tags.filter(function (t) { return t.title === 'param'; }).map(function (x) { return x.name; });
}
function tags_by_name(tags) {
var acc = {};
tags.forEach(function (tag) {
acc[tag.name] = tag;
});
return acc;
}
function generate_var(name, docs) {
var typeTag = find(docs.tags, function (t) { return t.title === 'type'; });
return 'var ' + name + ': ' + generate_type(typeTag && typeTag.type) + ';';
}
var VOID_TYPE = { type: { type: 'NameExpression', name: 'void' } };
function generics(docs) {
var tags = docs.tags || [];
var templateTags = tags.filter(function (t) { return t.title === 'template'; });
var names = templateTags.map(function (x) { return x.description; });
if (names.length === 0)
return '';
else
return '<' + names.join(', ') + '>';
}
// TODO now that overloads are no longer needed, this should return string not string[]
function generate_functions(name, value) {
var docs = value.jsdoc;
var names = param_names(value);
var tags = docs.tags || [];
var paramTags = tags.filter(function (t) { return t.title === 'param'; });
var overloads = [tags_by_name(paramTags)];
return overloads.map(function (tagsByName) {
var paramStrings = generate_param_strings(names, tagsByName);
var returnTag = find(tags, function (t) { return t.title === 'return' || t.title === 'returns'; }) || VOID_TYPE;
return 'function ' + name + generics(docs) + '(' + paramStrings.join(', ') + '): ' + generate_type(returnTag.type) + ';';
});
}
// TODO now that overloads are no longer needed, this should return string not string[]
function generate_properties(name, value) {
// Function
if (is_function(value))
return generate_functions(name, value);
else
return [generate_var(name, value.jsdoc)];
}
function generate_param_strings(names, types) {
return names.map(function (name) {
if (name in types)
return generate_function_parameter(types[name]);
else if (name.substring(0, 4) === 'opt_')
return safe_name(name) + '?: any /* jsdoc error */';
else
return safe_name(name) + ': any /* jsdoc error */';
});
}
// TODO now that overloads are no longer needed, this should return string not string[]
function generate_methods(name, value) {
var docs = value.jsdoc;
var names = param_names(value);
var tags = docs.tags || [];
var paramTags = tags.filter(function (t) { return t.title === 'param'; });
var overloads = [tags_by_name(paramTags)];
return overloads.map(function (tagsByName) {
var paramStrings = generate_param_strings(names, tagsByName);
var returnTag = find(tags, function (t) { return t.title === 'return' || t.title === 'returns'; }) || VOID_TYPE;
return name + generics(docs) + '(' + paramStrings.join(', ') + '): ' + generate_type(returnTag.type);
});
}
function generate_field_name(name, type) {
if (type && type.type === 'OptionalType')
return name + '?';
else
return name;
}
function generate_field(name, docs) {
var tags = docs.tags || [];
var typeTag = find(tags, function (t) { return t.title === 'type'; });
var type = typeTag && typeTag.type;
var fieldName = generate_field_name(name, type);
return fieldName + ': ' + generate_type(type);
}
function generate_record_field(field) {
var fieldName = generate_field_name(field.key, field.value);
return fieldName + ': ' + generate_type(field.value);
}
function is_function(value) {
var tags = value.jsdoc.tags || [];
return (value.value && value.value.type === 'FunctionExpression')
|| tags.some(function (t) { return t.title === 'param'; })
|| tags.some(function (t) { return t.title === 'return'; });
}
// TODO now that overloads are no longer needed, this should return string not string[]
function generate_members(name, value) {
// Function
if (is_function(value))
return generate_methods(name, value);
else
return [generate_field(name, value.jsdoc)];
}
function with_underscore(type) {
var prefix = /[\w\.]+/.exec(type)[0];
var suffix = type.substring(prefix.length);
return prefix + '__Class' + suffix;
}
function generate_class_extends(docs) {
var tags = docs.tags || [];
var supers = tags
.filter(function (t) { return t.title === 'extends'; })
.map(function (x) { return x.type; })
.map(generate_type)
.map(with_underscore);
if (supers.length > 0)
return 'extends ' + supers.join(', ') + ' ';
else
return '';
}
function generate_interface_extends(docs) {
var tags = docs.tags || [];
var supers = tags
.filter(function (t) { return t.title === 'extends'; })
.map(function (x) { return x.type; })
.map(generate_type);
if (supers.length > 0)
return 'extends ' + supers.join(', ') + ' ';
else
return '';
}
function generate_implements(docs) {
var supers = docs.tags
.filter(function (t) { return t.title === 'implements'; })
.map(function (x) { return x.type; })
.map(generate_type);
if (supers.length > 0)
return 'implements ' + supers.join(', ') + ' ';
else
return '';
}
function generate_interface(name, prototype) {
var constructor = prototype[''];
var acc = 'interface ' + name + generics(constructor.jsdoc) + ' ' + generate_interface_extends(constructor.jsdoc) + '{\n';
Object.keys(prototype).filter(function (name) { return name !== ''; }).forEach(function (name) {
var value = prototype[name];
var text = value.originalText.replace(/\n/g, '\n ');
acc += '\n';
generate_members(name, value).forEach(function (member) {
acc += ' ' + text + '\n';
acc += ' ' + member + ';\n';
});
});
acc += '}';
return acc;
}
// TODO now that overloads are no longer needed, this should return string not string[]
function generate_constructors(value) {
var docs = value.jsdoc;
var names = param_names(value);
var paramTags = docs.tags.filter(function (t) { return t.title === 'param'; });
var overloads = [tags_by_name(paramTags)];
return overloads.map(function (tagsByName) {
return 'constructor(' + generate_param_strings(names, tagsByName).join(', ') + ')';
});
}
function get_type_name(tag) {
if (tag.name)
return tag.name;
else if (tag.type) {
if (tag.type.type === 'NameExpression')
return tag.type.name;
else if (tag.type.type === 'TypeApplication')
return tag.type.expression.name;
}
}
function generate_class(name, prototype) {
var constructor = prototype[''];
var acc = 'class ' + name + generics(constructor.jsdoc) + ' extends ' + name + '__Class' + generics(constructor.jsdoc) + ' { }\n';
acc += '/** Fake class which should be extended to avoid inheriting static properties */\n';
acc += 'class ' + name + '__Class' + generics(constructor.jsdoc) + ' ' + generate_class_extends(constructor.jsdoc) + generate_implements(constructor.jsdoc) + ' { \n';
var text = constructor.originalText.replace(/\n/g, '\n' + ' ' + ' ');
acc += '\n';
generate_constructors(constructor).forEach(function (constructor) {
acc += ' ' + ' ' + text + '\n';
acc += ' ' + ' ' + constructor + ';\n';
});
function add_members(prototype) {
Object.keys(prototype).filter(function (name) { return name !== ''; }).forEach(function (name) {
var value = prototype[name];
var docs = value.jsdoc;
var tags = docs.tags || [];
var text = value.originalText.replace(/\n/g, '\n' + ' ' + ' ');
if (!tags.some(function (t) { return t.title === 'override'; })) {
acc += '\n';
generate_members(name, value).forEach(function (member) {
acc += ' ' + ' ' + text + '\n';
acc += ' ' + ' ' + member + ';\n';
});
}
});
// Look for implemented interfaces and inject them
var constructor = prototype[''];
var tags = constructor.jsdoc.tags || [];
var names = tags
.filter(function (t) { return t.title === 'implements'; })
.map(get_type_name);
var locations = names
.map(finder.symbol)
.filter(function (s) { return Boolean(s); });
locations.forEach(add_members);
}
add_members(prototype);
acc += '} \n';
return acc;
}
function generate_enum(name, values) {
function key_id(property) {
if ('name' in property)
return property.name;
else if ('value' in property)
return property.value;
else
throw new Error('Unknown enum property ' + property);
}
function safe_id(name) {
if (/^\w+$/.test(name))
return name;
else
return "'" + name + "'";
}
if (values.type === 'ObjectExpression') {
var keys = values.properties
.map(function (x) { return x.key; })
.map(key_id)
.map(safe_id);
return 'enum ' + name + ' { ' + keys.join(', ') + ' } ';
}
else {
var reference = escodegen.generate(values);
var dot = reference.lastIndexOf('.');
var moduleName = reference.substring(0, dot);
var enumName = reference.substring(dot + 1);
var fileName = finder.file(moduleName);
if (!fileName)
return 'enum ' + name + ' { /* ' + reference + ' */ } ';
var symbols = finder.symbols(fileName);
var moduleValue = symbols.modules[moduleName];
var enumValue = moduleValue[enumName].value;
return generate_enum(name, enumValue);
}
}
function generate_typedef(name, docs) {
var tag = find(docs.tags, function (t) { return t.title === 'typedef'; });
var typedef = tag.type;
switch (typedef.type) {
// Skip modifiers
case 'OptionalType':
case 'NullableType':
case 'NonNullableType':
tag.type = typedef.expression;
return generate_typedef(name, docs);
// F function(...) becomes interface F { (...): ... }
case 'FunctionType':
var argumentString = typedef.params.map(generate_indexed_function_parameter).join(', ');
var returnString = generate_type(typedef.result);
return 'interface ' + name + ' {\n (' + argumentString + '): ' + returnString + '\n}';
// S { ... } becomes interface T { ... }
case 'RecordType':
var fieldsString = typedef.fields.map(function (field) {
return field.key + ': ' + generate_type(field.value);
}).join(';\n ');
return 'interface ' + name + ' {\n ' + fieldsString + '\n}';
// T NamedType becomes interface T extends NamedType { }
case 'NameExpression':
references[typedef.name] = true;
return 'type ' + name + ' = ' + typedef.name;
// T NamedType<Param> becomes interface T extends NamedType<Param> { }
case 'TypeApplication':
if (typedef.expression.name === 'Object')
return generate_dictionary_type(typedef.applications);
else {
var base = typedef.expression.name;
var generics = '<' + typedef.applications.map(generate_type).join(',') + '>';
return 'interface ' + name + ' extends ' + base + generics + ' { }';
}
case 'UnionType':
var union = generate_type(typedef);
return 'type ' + name + ' = ' + union + ';';
// Anything else becomes interface Name { /* explanation */ }
default:
return 'interface ' + name + ' { ' + comment(generate_type(typedef)) + ' }';
}
}
var references = {};
function defs(symbols) {
var modules = {};
references = {};
// Generate classes
Object.keys(symbols.classes).forEach(function (name) {
var dot = name.lastIndexOf('.');
var moduleName = name.substring(0, dot);
var propertyName = name.substring(dot + 1);
var symbol = symbols.classes[name];
var constructor = symbol[''];
if (!modules[moduleName])
modules[moduleName] = {};
if (constructor.jsdoc.tags.some(function (t) { return t.title === 'interface'; }))
modules[moduleName][propertyName] = generate_interface(propertyName, symbol);
else
modules[moduleName][propertyName] = generate_class(propertyName, symbol);
});
// Generate statics
Object.keys(symbols.modules).forEach(function (moduleName) {
var symbol = symbols.modules[moduleName];
if (!modules[moduleName])
modules[moduleName] = {};
Object.keys(symbol).forEach(function (propertyName) {
var value = symbol[propertyName];
var comment = value.originalText + '\n';
if (value.jsdoc.tags.some(function (t) { return t.title === 'enum'; }))
modules[moduleName][propertyName] = comment + generate_enum(propertyName, value.value);
else if (value.jsdoc.tags.some(function (t) { return t.title === 'typedef'; }))
modules[moduleName][propertyName] = comment + generate_typedef(propertyName, value.jsdoc);
else {
modules[moduleName][propertyName] = generate_properties(propertyName, value)
.map(function (property) { return comment + property; })
.join('\n');
}
});
});
return {
modules: modules,
references: Object.keys(references)
};
}
exports.defs = defs;
},{"./finder":2,"escodegen":24}],4:[function(require,module,exports){
/// <reference path="../index/node.d.ts"/>
/// <reference path="../index/mkdirp.d.ts"/>
/// <reference path="../index/colors.d.ts"/>
var fs = require('fs');
var finder = require('./finder');
var generate = require('./generate');
var pretty_print = require('./pretty_print');
var options = require('./options');
var mkdirp = require('mkdirp');
require('colors');
exports.currentInput;
exports.currentOutput;
options.todo.forEach(function (todo) {
exports.currentInput = todo.input;
exports.currentOutput = todo.output;
try {
var symbols = finder.symbols(todo.input);
var defs = generate.defs(symbols);
var pretty = pretty_print.pretty(defs);
var parentPart = (/^.*\//).exec(todo.output) || [];
var parentDir = parentPart[0] || '';
mkdirp.sync(parentDir);
if (fs.existsSync(todo.output) && fs.readFileSync(todo.output, 'utf8') === pretty) {
console.error('No changes\t'.green + todo.output);
}
else {
fs.writeFile(todo.output, pretty);
console.error('Wrote\t'.red + todo.output);
}
}
catch (e) {
console.error('ERROR\t'.bold.red + todo.input + ' ' + todo.output);
throw e;
}
});
},{"./finder":2,"./generate":3,"./options":5,"./pretty_print":8,"colors":16,"fs":9,"mkdirp":45}],5:[function(require,module,exports){
(function (process){
/// <reference path="../index/node.d.ts"/>
var options = process.argv.slice(2);
function get_option(name) {
var index = options.indexOf('--' + name);
if (index !== -1) {
var value = options[index + 1];
options.splice(index, 2);
return value;
}
}
exports.provides = get_option('provides');
exports.globals = get_option('globals');
exports.inputRoot = get_option('input_root') || '';
exports.outputRoot = get_option('output_root') || '';
exports.includePrivate = get_option('include_private') || false;
exports.todo = [];
for (var i = 0; i < options.length; i += 2) {
exports.todo.push({
input: options[i],
output: options[i + 1]
});
}
}).call(this,require('_process'))
},{"_process":11}],6:[function(require,module,exports){
var parser = require('./parser');
var combine = require('./combine');
var generate = require('./generate');
var pretty_print = require('./pretty_print');
function convert(text) {
var docs = parser.jsdoc(text);
var symbols = combine.members(docs);
var out = generate.defs(symbols);
var text = pretty_print.pretty(out);
return text;
}
exports.convert = convert;
window['convert'] = convert;
},{"./combine":1,"./generate":3,"./parser":7,"./pretty_print":8}],7:[function(require,module,exports){
/// <reference path="../index/node.d.ts"/>
/// <reference path="../index/doctrine.d.ts"/>
/// <reference path="../index/esprima.d.ts"/>
/// <reference path="../index/escodegen.d.ts"/>
var esprima = require('esprima');
var escodegen = require('escodegen');
var doctrine = require('doctrine');
var options = require('./options');
function values(object) {
return Object.keys(object).map(function (k) { return object[k]; });
}
function local_variables(tree) {
var acc = [];
function walk(tree) {
if (!tree) {
}
else if (tree.type === 'FunctionDeclaration')
acc.push(tree.id.name);
else if (tree.type === 'VariableDeclarator')
acc.push(tree.id.name);
else if (tree instanceof Object) {
values(tree).forEach(walk);
}
}
walk(tree);
return acc;
}
/**
* Walk AST and extract JSDoc comments on global variables
*/
function extract_jsdoc(tree) {
/**
* Keys are global variables, values are { value, jsdoc }
*/
var docstrings = {};
/**
* Keys are local variables, values are true or false
*/
var locals = {};
var requirejs = false;
function not_in_locals(name) {
return !locals[name];
}
function add_local(name) {
locals[name] = true;
}
function remove_local(name) {
locals[name] = false;
}
function is_global(tree) {
if (tree.type === 'Identifier')
return !locals[tree.name];
else if (tree.type === 'MemberExpression' && tree.property.type === 'Identifier')
return is_global(tree.object);
else
return false;
}
function is_global_assignment(tree) {
return tree.type === 'ExpressionStatement' &&
tree.expression.type === 'AssignmentExpression' &&
is_global(tree.expression.left);
}
function is_global_declaration(tree) {
return tree.type === 'ExpressionStatement' &&
is_global(tree.expression);
}
function is_this_member(tree) {
return tree.type === 'MemberExpression' && tree.object.type === 'ThisExpression';
}
function is_this_assignment(tree) {
return tree.type === 'ExpressionStatement' &&
tree.expression.type === 'AssignmentExpression' &&
is_this_member(tree.expression.left);
}
function is_this_declaration(tree) {
return tree.type === 'ExpressionStatement' &&
is_this_member(tree.expression);
}
function is_identifier(tree) {
if (tree.type === 'Identifier')
return true;
else if (tree.type === 'MemberExpression' && tree.property.type === 'Identifier')
return is_identifier(tree.object);
else
return false;
}
function is_assignment(tree) {
return tree.type === 'ExpressionStatement' &&
tree.expression.type === 'AssignmentExpression' &&
is_identifier(tree.expression.left);
}
function is_declaration(tree) {
return tree.type === 'ExpressionStatement' &&
is_identifier(tree.expression);
}
function is_function(tree) {
return tree.type === 'FunctionDeclaration';
}
function is_var(tree) {
return tree.type === 'VariableDeclaration';
}
/**
* If we are walking inside a constructor, the expression that currently represents this
*/
var currentThis;
/**
* Look for global variables with JSDoc annotations
*/
function walk(tree) {
// If leaf, return
if (!(tree instanceof Object))
return;
// If tree is a global assignment, add it to docstrings
if (is_global_assignment(tree) && tree.leadingComments) {
var name = escodegen.generate(tree.expression.left);
tree.leadingComments.forEach(function (comment) {
if (comment.type === 'Block' && comment.value.charAt(0) === '*') {
docstrings[name] = {
value: tree.expression.right,
jsdoc: '/*' + comment.value + '*/'
};
// If tree is a constructor, remember its name as the current this
if (comment.value.indexOf('@constructor') !== -1)
currentThis = escodegen.generate(tree.expression.left);
}
});
}
// If tree is a global declaration, add it to docstrings
if (is_global_declaration(tree) && tree.leadingComments) {
var name = escodegen.generate(tree.expression);
tree.leadingComments.forEach(function (comment) {
if (comment.type === 'Block' && comment.value.charAt(0) === '*') {
docstrings[name] = {
value: tree.expression.right,
jsdoc: '/*' + comment.value + '*/'
};
}
});
}
// If tree is this.property = ..., add it to docstrings
if (is_this_assignment(tree) && tree.leadingComments) {
var memberName = escodegen.generate(tree.expression.left.property);
var name = currentThis + ".prototype." + memberName;
tree.leadingComments.forEach(function (comment) {
if (comment.type === 'Block' && comment.value.charAt(0) === '*') {
var rawComment = comment.value;
var removeIndent = rawComment.replace(/\n +/g, '\n ');
docstrings[name] = {
value: tree.expression.right,
jsdoc: '/*' + removeIndent + '*/'
};
}
});
}
// If tree is this.property;, add it to docstrings
if (is_this_declaration(tree) && tree.leadingComments) {
var memberName = escodegen.generate(tree.expression.property);
var name = currentThis + ".prototype." + memberName;
tree.leadingComments.forEach(function (comment) {
if (comment.type === 'Block' && comment.value.charAt(0) === '*') {
var rawComment = comment.value;
var removeIndent = rawComment.replace(/\n +/g, '\n ');
docstrings[name] = {
value: tree.expression.right,
jsdoc: '/*' + removeIndent + '*/'
};
}
});
}
// If tree is a local assignment and requirejs is active, add it to docstrings under MODULE
if (requirejs && is_assignment(tree) && tree.leadingComments) {
var name = 'MODULE.' + escodegen.generate(tree.expression.left);
tree.leadingComments.forEach(function (comment) {
if (comment.type === 'Block' && comment.value.charAt(0) === '*') {
docstrings[name] = {
value: tree.expression.right,
jsdoc: '/*' + comment.value + '*/'
};
}
});
}
// If tree is a local declaration and requirejs is active, add it to docstrings under MODULE
if (requirejs && is_declaration(tree) && tree.leadingComments) {
var name = 'MODULE.' + escodegen.generate(tree.expression);
tree.leadingComments.forEach(function (comment) {
if (comment.type === 'Block' && comment.value.charAt(0) === '*') {
docstrings[name] = {
value: tree.expression.right,
jsdoc: '/*' + comment.value + '*/'
};
}
});
}
// If tree is a function declaration and requirejs is active, add it to docstrings under MODULE
if (requirejs && is_function(tree) && tree.leadingComments) {
var name = 'MODULE.' + tree.id.name;
tree.leadingComments.forEach(function (comment) {
if (comment.type === 'Block' && comment.value.charAt(0) === '*') {
docstrings[name] = {
value: tree,
jsdoc: '/*' + comment.value + '*/'
};
}
});
}
// If tree is var declaration and requirejs is active, add it to docstrings under MODULE
if (requirejs && is_var(tree) && tree.leadingComments) {
tree.declarations.forEach(function (declaration) {
var name = 'MODULE.' + declaration.id.name;
tree.leadingComments.forEach(function (comment) {
if (comment.type === 'Block' && comment.value.charAt(0) === '*') {
docstrings[name] = {
value: declaration.init,
jsdoc: '/*' + comment.value + '*/'
};
}
});
});
}
// If tree is a function expression, create a new scope
if (tree.type === 'FunctionExpression' || tree.type === 'FunctionDeclaration') {
var params = tree.params.map(function (p) { return p.name; });
var vars = local_variables(tree);
var introduced = vars.concat(params).filter(not_in_locals);
// Add new local variables
introduced.forEach(add_local);
// Walk children
values(tree.body).forEach(walk);
// Remove local variables
introduced.forEach(remove_local);
}
else if (tree.type === 'CallExpression' && tree.callee.name === 'define' && !locals['define']) {
requirejs = true;
values(tree).forEach(walk);
}
else {
values(tree).forEach(walk);
}
}
// function is_assign_member(tree) {
// return tree &&
// tree.type === 'ExpressionStatement' &&
// tree.expression.type === 'AssignmentExpression' &&
// tree.expression.left.type === 'MemberExpression';
// }
//
// function is_assign_module(tree) {
// return is_assign_member(tree) &&
// tree.expression.left.object.name === 'module' &&
// tree.expression.left.property.name === 'exports' &&
// tree.expression.right.type === 'Identifier';
// }
//
// function is_assign_exports(tree) {
// return is_assign_member(tree) &&
// tree.expression.left.object.name === 'exports' &&
// tree.expression.right.type === 'Identifier';
// }
//
// /**
// * Look for exports of the form
// * exports.$name = $var
// * module.exports = $var
// */
// function find_exports(tree) {
// var exports = {};
//
// function dfs(tree) {
// if (!(tree instanceof Object)) { }
// // module.exports = ...
// else if (is_assign_module(tree)) {
// var localName = tree.expression.right.name;
//
// exports['EXPORTS'] = docstrings[localName];
// }
// // exports.$var = ...
// else if (is_assign_exports(tree)){
// var exportName = tree.expression.left.property;
// var localName = tree.expression.right.name;
//
// exports[exportName] = docstrings[localName];
// }
// else {
// values(tree).forEach(dfs);
// }
// }
//
// dfs(tree.body[21]);
//
// return {