-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
807 lines (807 loc) · 35 KB
/
index.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
"use strict";
var through = require("through2");
var gutil = require("gulp-util");
var File = require("vinyl");
var path = require("path");
var util = require("util");
module.exports = function (file, options) {
options = options || {};
// to preserve existing |undefined| behaviour and to introduce |newLine: ""| for binaries
if (typeof options.newLine !== 'string') {
options.newLine = gutil.linefeed;
}
if (typeof options.extension !== 'string') {
options.extension = ".ts";
}
var angularTransform;
return through.obj(bufferContents, endStream);
function bufferContents(file, enc, cb) {
// ignore empty files
if (file.isNull()) {
cb();
return;
}
if (file.isStream()) {
this.emit("error", new gutil.PluginError("gulp-tsng", "Streams are not supported!"));
cb();
return;
}
if (angularTransform == null) {
angularTransform = new AngularTransform(this, options);
}
angularTransform.process(file);
cb();
}
function endStream(cb) {
if (angularTransform != null) {
angularTransform.emit(this, cb);
return;
}
cb();
}
};
var AngularTransform = (function () {
function AngularTransform(depot, options) {
this._fileRegex = {
// //@NgModule('moduleName')
// module My.Great.Module {
moduleComment: /^\s*\/\/@NgModule(?:\(?['"]?([\w.]+)['"]?\)?\s*)?$/,
moduleDeclaration: /^\s*(?:export\s+)?module\s*([\w.]*)\s*{\s*$/,
// //@NgController('controllerName')
// class MyController implements IMyViewModel {
controllerComment: /^\s*\/\/@NgController(?:\(?['"]?([\w]*|skip\=true)['"]?\)?\s*)?$/,
controllerDeclaration: /^\s*(?:export\s+)?class (\w+Controller)\s*/,
// //@NgService('serviceName')
// class MyService implements IMyService {
serviceComment: /^\s*\/\/@NgService(?:\(?['"]?(\w+)['"]?\)?\s*)?$/,
serviceDeclaration: /^\s*(?:export\s+)?class (\w+Service)\s+(?:implements\s+([\w.]+)\s*{)?/,
// //@NgDirective('directiveName')
// class MyDirective implements ng.IDirective {
directiveComment: /^\s*\/\/@NgDirective(?:\(?['"]?(\w+)['"]?\)?\s*)?$/,
directiveDeclaration: /^\s*(?:export\s+)?class (\w+Directive)\s+(?:implements\s+(\w.+)\s*{)?/,
// //@NgFilter('filterName')
// function filter(input: string) {
filterComment: /^\s*\/\/\s*@NgFilter(?:\s*\(\s*['"]?(\w+)['"]?\s*\))?\s*$/,
filterDeclaration: /^\s*function\s*([a-zA-Z_$]+)\s*\([a-zA-Z0-9_$:,\s]*\)/,
// constructor($window: ng.IWindowService) {
constructor: /constructor\s*\(\s*([^(]*)\s*\)\s*{/,
closingBrace: /^\s*}\s*$/
};
this._startupFnRegex = {
dependencies: /var\s+dependencies\s*=\s*\[([\w\s.,"']*)\]/,
// BUG: This finds configuration functions that are commented out
configFn: /function\s*(configuration)\s*\(\s*([\w$:.,\s]*)\s*\)\s*{/,
// BUG: This finds run functions that are commented out
runFn: /function\s*(run)\s*\(\s*([\w$:.,\s]*)\s*\)\s*{/
};
this._result = {
modules: [],
controllers: [],
services: [],
directives: [],
filters: [],
fileTally: 0
};
this._modules = {};
this._files = {};
this._depot = depot;
this._options = options;
}
AngularTransform.prototype.process = function (file) {
var content = file.contents.toString();
var filepath = file.path;
var fileResult = this.processFile(content, filepath);
fileResult.file = file;
fileResult.content = content;
fileResult.path = filepath;
if (fileResult.error) {
throw new Error(fileResult.error);
}
fileResult.module = this.mergeModules(fileResult);
if (!fileResult.module) {
throw new Error("File result for file " + filepath + " doesn't have a module");
}
if (!fileResult.module.file) {
this._files[filepath] = fileResult;
}
this.sumResult(fileResult, this._result);
this._result.modules.push(fileResult.module);
this._result.fileTally++;
};
AngularTransform.prototype.emit = function (depot, cb) {
var serviceNames = this._result.services.map(function (service) {
return service.name;
});
// Emit module files
var moduleNames = [];
for (var moduleName in this._modules) {
if (!this._modules.hasOwnProperty(moduleName)) {
continue;
}
moduleNames.push(this._modules[moduleName].name);
}
for (var moduleName in this._modules) {
if (!this._modules.hasOwnProperty(moduleName)) {
continue;
}
var file = this.buildModuleFile(this._modules[moduleName], moduleNames, serviceNames);
this.pushFile(file);
}
// Emit non-module files
for (var filepath in this._files) {
if (!this._files.hasOwnProperty(filepath)) {
continue;
}
var fileResult = this._files[filepath];
var file = this.updateFile(fileResult, serviceNames);
this.pushFile(file);
}
cb();
};
AngularTransform.prototype.processFile = function (content, filepath) {
var result = {
module: null,
controllers: [],
services: [],
directives: [],
filters: []
};
var lines = content.split(this._options.newLine);
var module, line, matches, state, lastClosingBraceLine, error;
var moduleFile;
var expecting = expect.anything;
for (var i = 0; i < lines.length; i++) {
line = lines[i];
// Check for closing brace on a line by itself
matches = line.match(this._fileRegex.closingBrace);
if (matches) {
lastClosingBraceLine = i;
continue;
}
if (expecting === expect.anything) {
// Check for module comment
matches = line.match(this._fileRegex.moduleComment);
if (matches) {
expecting = expect.moduleDeclaration;
state = matches;
continue;
}
// Check for module declaration
matches = line.match(this._fileRegex.moduleDeclaration);
if (matches) {
if (module) {
// A module is already declared for this file
error = "Error: " + filepath + "(" + i + "): Only one module can be declared per file";
break;
}
moduleFile = this.parseModuleFile(content);
moduleFile.name = matches[1];
moduleFile.declarationLine = i;
module = moduleFile;
state = null;
}
// Check for controller comment
matches = line.match(this._fileRegex.controllerComment);
if (matches) {
expecting = expect.controllerDeclaration;
state = matches;
continue;
}
// Check for controller declaration
matches = line.match(this._fileRegex.controllerDeclaration);
if (matches) {
var fnName = matches[1];
var name = (module ? module.name + "." : "") + fnName;
var ctor = this.parseConstructor(content) || { args: [] };
result.controllers.push({
module: module,
name: name,
fnName: fnName,
dependencies: ctor.args,
file: filepath,
ctorStartLine: ctor.startLine,
ctorEndLine: ctor.endLine
});
expecting = expect.anything;
continue;
}
// Check for service comment
matches = line.match(this._fileRegex.serviceComment);
if (matches) {
expecting = expect.serviceDeclaration;
state = matches;
continue;
}
// Check for service declaration
matches = line.match(this._fileRegex.serviceDeclaration);
if (matches) {
var className = matches[1];
var interfaceName = matches[2];
var name = (module ? module.name + "." : "") + (interfaceName || className);
var ctor = this.parseConstructor(content) || { args: [] };
result.services.push({
module: module,
name: name,
fnName: className,
dependencies: ctor.args,
file: filepath,
ctorStartLine: ctor.startLine,
ctorEndLine: ctor.endLine
});
expecting = expect.anything;
continue;
}
// Check for directive comment
matches = line.match(this._fileRegex.directiveComment);
if (matches) {
//debugger;
expecting = expect.directiveComment | expect.directiveDeclaration;
state = { names: [] };
state.names.push(matches[1]);
continue;
}
// Check for filter comment
matches = line.match(this._fileRegex.filterComment);
if (matches) {
expecting = expect.filterDeclaration;
state = matches;
continue;
}
}
if (expecting === expect.moduleDeclaration) {
// Check for module declaration
matches = line.match(this._fileRegex.moduleDeclaration);
if (matches) {
if (module) {
// A module is already declared for this file
error = "Error: " + filepath + "(" + i + "): Only one module can be declared per file";
break;
}
moduleFile = this.parseModuleFile(content);
moduleFile.name = state[1] || matches[1];
module = moduleFile;
state = null;
expecting = expect.anything;
}
else {
// A module comment was found but the next line wasn't a module declaration
error = "Error: " + filepath + "(" + i + "): @NgModule must be followed by a TypeScript module declaration, e.g. module My.Module.Name {";
break;
}
}
if (expecting === expect.controllerDeclaration) {
if (state[1] === "skip=true") {
state = null;
expecting = expect.anything;
continue;
}
// Check for controller declaration
matches = line.match(this._fileRegex.controllerDeclaration);
if (matches) {
(function () {
var name = (module ? module.name + "." : "") + (state[1] || matches[1]);
var ctor = this.parseConstructor(content) || { args: [] };
result.controllers.push({
module: module,
name: name,
fnName: matches[1],
dependencies: ctor.args,
file: filepath,
startLine: ctor.startLine,
endLine: ctor.endLine
});
}());
expecting = expect.anything;
continue;
}
else {
// A controller comment was found but the next line wasn't a controller declaration
error = "Error: " + filepath + "(" + i + "): @NgController must be followed by a TypeScript class declaration ending with 'Controller', e.g. class MyController implements IMyViewModel {";
break;
}
}
if (expecting === expect.serviceDeclaration) {
// Check for service declaration
matches = line.match(this._fileRegex.serviceDeclaration);
if (matches) {
(function () {
var className = matches[1];
var interfaceName = matches[2];
var name = (module ? module.name + "." : "") + ((state ? state[1] : null) || interfaceName || className);
var ctor = this.parseConstructor(content) || { args: [] };
result.services.push({
module: module,
name: name,
fnName: className,
dependencies: ctor.args,
file: filepath,
ctorStartLine: ctor.startLine,
ctorEndLine: ctor.endLine
});
}());
expecting = expect.anything;
continue;
}
}
if (expecting & expect.directiveComment) {
// Check for directive comment
matches = line.match(this._fileRegex.directiveComment);
if (matches) {
expecting = expect.directiveComment | expect.directiveDeclaration;
state.names.push(matches[1]);
continue;
}
}
if (expecting & expect.directiveDeclaration) {
// Check for directive function
matches = line.match(this._fileRegex.directiveDeclaration);
if (matches) {
(function () {
var fnName = matches[1];
var ctor = this.parseConstructor(content) || { args: [] };
state.names.forEach(function (name) {
result.directives.push({
module: module,
file: filepath,
name: name,
fnName: fnName,
classLine: i,
ctorStartLine: ctor.startLine,
ctorEndLine: ctor.endLine,
dependencies: ctor.args
});
});
}());
expecting = expect.anything;
continue;
}
}
if (expecting === expect.filterDeclaration) {
// Check for filter function
matches = line.match(this._fileRegex.filterDeclaration);
if (matches) {
result.filters.push({
module: module,
name: state[1] || matches[1],
fnName: matches[1],
file: filepath
});
state = null;
expecting = expect.anything;
continue;
}
}
}
// EOF
if (expecting !== expect.anything) {
error = "Error: End of file " + filepath + " reached while expecting " + expecting;
}
if (error) {
result.error = error;
return result;
}
result.closingBraceLine = lastClosingBraceLine;
result.module = module;
return result;
};
AngularTransform.prototype.parseConstructor = function (fileContents) {
// Extract details from constructor function
// constructor($window: ng.IWindowService) {
var regex = /constructor\s*\(\s*([^(]*)\s*\)\s*{/;
var matches = fileContents.match(regex);
var ctor = {};
if (!matches) {
// Not found ctor
return null;
}
ctor.args = [];
if (matches[1]) {
matches[1].split(",").forEach(function (argLine) {
var argParts = argLine.split(":");
var arg = { name: argParts[0].trim() };
if (argParts.length > 1) {
arg.type = argParts[1].trim();
}
ctor.args.push(arg);
});
}
// Find line numbers where the constructor function starts/ends
var startIndex = fileContents.indexOf(matches[0]);
var endIndex = startIndex + matches[0].length;
ctor.startLine = fileContents.substr(0, startIndex).split(this._options.newLine).length - 1;
ctor.endLine = fileContents.substr(0, endIndex).split(this._options.newLine).length - 1;
return ctor;
};
AngularTransform.prototype.parseModuleFile = function (content) {
var matches = {};
var module = {};
for (var key in this._startupFnRegex) {
if (!this._startupFnRegex.hasOwnProperty(key)) {
continue;
}
matches[key] = content.match(this._startupFnRegex[key]);
if (matches[key]) {
module.fileExisted = true;
}
}
if (!module.fileExisted) {
return module;
}
if (matches.dependencies) {
var members = matches.dependencies[1];
var dependencies = [];
if (members) {
members.split(",").forEach(function (dependency) {
dependency = this.trim(dependency.trim(), ["\"", "'"]);
dependencies.push(dependency);
}.bind(this));
}
module.dependencies = dependencies;
}
["configFn", "runFn"].forEach(function (fnName) {
if (matches[fnName]) {
var args = matches[fnName][2];
var dependencies = [];
if (args) {
args.split(",").forEach(function (arg) {
var parts = arg.split(":");
var dependency = {
name: parts[0].trim()
};
if (parts[1]) {
dependency.type = parts[1].trim();
}
dependencies.push(dependency);
});
}
module[fnName] = {
fnName: matches[fnName][1],
dependencies: dependencies
};
}
});
return module;
};
AngularTransform.prototype.sumResult = function (source, target) {
if (!source || !target) {
return;
}
for (var key in target) {
if (!target.hasOwnProperty(key) || !source.hasOwnProperty(key)) {
continue;
}
var targetType = (typeof (target[key])).toLowerCase();
var sourceType = (typeof (source[key])).toLowerCase();
if (targetType !== sourceType) {
continue;
}
if (targetType === "number" || targetType === "string") {
target[key] = target[key] + source[key];
}
else if (Array.isArray(target[key])) {
target[key] = target[key].concat(source[key]);
}
}
};
AngularTransform.prototype.buildModuleFile = function (module, moduleNames, serviceNames) {
var file = module.file;
// Not exist file
if (!file) {
var file = this.createModuleFile(module);
module.file = file;
return file;
}
this.updateModuleFile(file, module, moduleNames, serviceNames);
return file;
};
AngularTransform.prototype.updateModuleFile = function (file, module, moduleNames, serviceNames) {
var content = "";
// Module already has a file defined, just add the module registration
var srcLines = file.contents.toString().split(this._options.newLine);
//gulp.log.writeln("module.declarationLine=" + module.declarationLine);
var startLine = module.declarationLine + 1;
srcLines.forEach(function (line, i) {
if (i === startLine) {
// Add the module registration
content += this.indent() + "angular.module(\"" + module.name + "\", [" + this._options.newLine;
if (module.dependencies && module.dependencies.length) {
module.dependencies.forEach(function (d) {
var resolvedDependencyName = this.resolveTypeName(d, module.name, moduleNames);
content += this.indent(2) + "\"" + (resolvedDependencyName || d) + "\"," + this._options.newLine;
}.bind(this));
}
content += this.indent() + "])";
["config", "run"].forEach(function (method) {
var fn = module[method + "Fn"];
if (!fn) {
return;
}
content += "." + method + "([" + this._options.newLine;
fn.dependencies.forEach(function (d) {
var typeName;
if (d.name.substr(0, 1) === "$") {
typeName = d.name;
}
else {
typeName = this.resolveTypeName(d.type, module.name, serviceNames);
if (!typeName) {
// Couldn't resolve type name
throw new Error("Error: Can't resolve dependency for module function " + module.name + "." + method + " with name " + d.type);
}
}
content += this.indent(2) + "\"" + typeName + "\"," + this._options.newLine;
}.bind(this));
content += this.indent(2) + fn.fnName + this._options.newLine + this.indent() + "])";
}.bind(this));
content += ";" + this._options.newLine + this._options.newLine;
}
content += line;
if (i < (srcLines.length - 1)) {
content += this._options.newLine;
}
}.bind(this));
file.contents = new Buffer(content);
};
AngularTransform.prototype.createModuleFile = function (module) {
var filepath = module.name + this._options.extension;
var content = "";
// We need to render a whole file
content = "module " + module.name + " {" + this._options.newLine;
content += this.indent() + "angular.module(\"" + module.name + "\", []);" + this._options.newLine;
content += "}";
var file = new File({
path: filepath,
contents: new Buffer(content)
});
return file;
};
AngularTransform.prototype.updateFile = function (result, serviceNames) {
var file = result.file;
var filepath = file.path;
var content = "";
var module = result.module;
var srcLines = result.content.split(this._options.newLine);
var emitCtor = result.directives.length
&& !result.directives[0].ctorStartLine;
srcLines.forEach(function (line, i) {
if (i === 0 && module.file) {
}
var emitBind = result.directives.length ?
result.directives[0].ctorEndLine ?
(result.directives[0].ctorEndLine + 1) === i // Line after the ctor declartion ends
: (result.directives[0].classLine + 1) === i // No ctor already, so line after the class declaration
: false;
if (emitBind) {
if (emitCtor) {
// Need to generate a ctor
content += this.indent(2) + "constructor() {" + this._options.newLine;
}
// Emit function to bind instance methods to 'this'
content += this.indent(3) + "for (var m in this) {" + this._options.newLine;
content += this.indent(4) + "if (this[m].bind) {" + this._options.newLine;
content += this.indent(5) + "this[m] = this[m].bind(this);" + this._options.newLine;
content += this.indent(4) + "}" + this._options.newLine;
content += this.indent(3) + "}" + this._options.newLine;
if (emitCtor) {
// Need to generate a ctor
content += this.indent(2) + "}" + this._options.newLine + this._options.newLine;
}
}
if (i === result.closingBraceLine && module.file) {
content += this.indent() + this._options.newLine;
content += this.indent() + "angular.module(\"" + module.name + "\")";
// Register controllers
result.controllers.forEach(function (controller) {
content += this._options.newLine;
content += this.indent(2) + ".controller(\"" + controller.name + "\", [" + this._options.newLine;
if (controller.dependencies && controller.dependencies.length) {
controller.dependencies.forEach(function (d) {
var typeName;
if (d.name.substr(0, 1) === "$") {
typeName = d.name;
}
else {
typeName = this.resolveTypeName(d.type, module.name, serviceNames);
if (!typeName) {
// Couldn't resolve type name
throw new Error("Error: Can't resolve dependency for controller " + controller.name + " with name " + d.type);
}
}
content += this.indent(3) + "\"" + typeName + "\"," + this._options.newLine;
}.bind(this));
}
content += this.indent(3) + controller.fnName + this._options.newLine;
content += this.indent(2) + "])";
}.bind(this));
// Register services
result.services.forEach(function (service) {
content += this._options.newLine;
content += this.indent(2) + ".service(\"" + service.name + "\", [" + this._options.newLine;
if (service.dependencies && service.dependencies.length) {
service.dependencies.forEach(function (d) {
var typeName;
if (d.name.substr(0, 1) === "$") {
typeName = d.name;
}
else {
typeName = this.resolveTypeName(d.type, module.name, serviceNames);
if (!typeName) {
// Couldn't resolve type name
throw new Error("Error: Can't resolve dependency for service " + service.name + " with name " + d.type);
}
}
content += this.indent(3) + "\"" + typeName + "\"," + this._options.newLine;
}.bind(this));
}
content += this.indent(3) + service.fnName + this._options.newLine;
content += this.indent(2) + "])";
}.bind(this));
// Register directives
result.directives.forEach(function (directive) {
content += this._options.newLine;
content += this.indent(2) + ".directive(\"" + directive.name + "\", [" + this._options.newLine;
if (directive.dependencies && directive.dependencies.length) {
directive.dependencies.forEach(function (d) {
var typeName;
if (d.name.substr(0, 1) === "$") {
typeName = d.name;
}
else {
typeName = this.resolveTypeName(d.type, module.name, serviceNames);
if (!typeName) {
// Couldn't resolve type name
throw new Error("Error: Can't resolve dependency for directive " + directive.name + " with name " + d.type);
}
}
content += this.indent(3) + "\"" + typeName + "\"," + this._options.newLine;
}.bind(this));
}
var alphabet = "abcdefghijklmnopqrstuvwxyz";
alphabet += alphabet.toUpperCase();
var argList = directive.dependencies.map(function (d, index) {
return alphabet.substr(index, 1);
});
content += this.indent(3) + "function (";
content += argList;
content += ") {" + this._options.newLine;
content += this.indent(4) + "return new " + directive.fnName + "(" + argList + ");" + this._options.newLine;
content += this.indent(3) + "}" + this._options.newLine;
content += this.indent(2) + "])";
}.bind(this));
// Register filters
result.filters.forEach(function (filter) {
content += this._options.newLine;
content += this.indent(2) + ".filter(\"" + filter.name + "\", () => " + filter.fnName + ")";
}.bind(this));
content += ";" + this._options.newLine;
}
content += line;
if (i < (srcLines.length - 1)) {
content += this._options.newLine;
}
}.bind(this));
return new File({
path: file.path,
base: file.base,
contents: new Buffer(content)
});
};
AngularTransform.prototype.pushFile = function (file) {
this._depot.push(file);
};
AngularTransform.prototype.mergeModules = function (result) {
var module = result.module;
// Not found module
if (!module) {
return module;
}
// No angular types created, just no-op
if (!module.fileExisted && !this.hasTypes(result)) {
//console.log("Module " + module.name + " contains no angular types, skipping file emission");
return module;
}
// New module
if (!this._modules[module.name]) {
this._modules[module.name] = module;
if (module.fileExisted) {
module.file = result.file;
}
return module;
}
// Existing module
var resolvedModule = this._modules[module.name];
if (!module.fileExisted) {
return resolvedModule;
}
if (resolvedModule.file) {
// Error: Module defined in multiple files
throw new Error("tsng: Module '" + module.name + "' defined in multiple files");
}
resolvedModule.file = result.file;
return resolvedModule;
};
AngularTransform.prototype.resolveTypeName = function (typeName, moduleName, allNames) {
//console.log("TypeInfo:", { name: typeName, moduleName: moduleName, allNames: allNames });
var prefix, matchedIndex;
var parts = moduleName.split(".");
if (parts.length === 1) {
matchedIndex = allNames.indexOf(moduleName + "." + typeName);
if (matchedIndex >= 0) {
return allNames[matchedIndex];
}
// No match found!
return null;
}
for (var i = parts.length - 1; i >= 0; i--) {
prefix = "";
parts.forEach(function (part, index) {
if (index <= i) {
prefix += part + ".";
}
});
matchedIndex = allNames.indexOf(prefix + typeName);
if (matchedIndex >= 0) {
return allNames[matchedIndex];
}
}
// No match found!
return null;
};
AngularTransform.prototype.hasTypes = function (result) {
return !!((result.controllers && result.controllers.length) ||
(result.services && result.services.length) ||
(result.directives && result.directives.length) ||
(result.filters && result.filters.length));
};
AngularTransform.prototype.trim = function (target, chars) {
/// <param name="target" type="String" />
/// <param name="chars" type="Array" />
//debugger;
var result, c, i;
chars = chars || [" "];
if (!target) {
return target;
}
result = "";
// Trim from start
for (i = 0; i < target.length; i++) {
c = target[i];
if (chars.indexOf(c) < 0) {
result = target.substr(i);
break;
}
}
// Trim from end
for (i = result.length - 1; i >= 0; i--) {
c = result[i];
if (chars.indexOf(c) < 0) {
result = result.substring(0, i + 1);
break;
}
}
return result;
};
AngularTransform.prototype.indent = function (length, char) {
if (length === void 0) { length = 1; }
if (char === void 0) { char = " "; }
// length: Default to 1 level of indent
// char: Default to 4 spaces
var result = "";
for (var i = 0; i < length; i++) {
result += char;
}
return result;
};
return AngularTransform;
})();
var expect;
(function (expect) {
expect[expect["anything"] = 0] = "anything";
expect[expect["moduleDeclaration"] = 1] = "moduleDeclaration";
expect[expect["controllerDeclaration"] = 2] = "controllerDeclaration";
expect[expect["serviceDeclaration"] = 4] = "serviceDeclaration";
expect[expect["directiveComment"] = 8] = "directiveComment";
expect[expect["directiveDeclaration"] = 16] = "directiveDeclaration";
expect[expect["filterDeclaration"] = 32] = "filterDeclaration";
})(expect || (expect = {}));
//# sourceMappingURL=index.js.map