/
esm5_host_spec.ts
2534 lines (2266 loc) Β· 106 KB
/
esm5_host_spec.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
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
/**
* @license
* Copyright Google Inc. All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/
import * as ts from 'typescript';
import {absoluteFrom, getFileSystem, getSourceFileOrError} from '../../../src/ngtsc/file_system';
import {TestFile, runInEachFileSystem} from '../../../src/ngtsc/file_system/testing';
import {ClassMemberKind, CtorParameter, Decorator, Import, TsHelperFn, isNamedClassDeclaration, isNamedFunctionDeclaration, isNamedVariableDeclaration} from '../../../src/ngtsc/reflection';
import {getDeclaration} from '../../../src/ngtsc/testing';
import {loadFakeCore, loadTestFiles} from '../../../test/helpers';
import {Esm2015ReflectionHost} from '../../src/host/esm2015_host';
import {Esm5ReflectionHost, getIifeBody} from '../../src/host/esm5_host';
import {MockLogger} from '../helpers/mock_logger';
import {getRootFiles, makeTestBundleProgram} from '../helpers/utils';
import {expectTypeValueReferencesForParameters} from './util';
runInEachFileSystem(() => {
describe('Esm5ReflectionHost', () => {
let _: typeof absoluteFrom;
let SOME_DIRECTIVE_FILE: TestFile;
let CTOR_DECORATORS_ARRAY_FILE: TestFile;
let ACCESSORS_FILE: TestFile;
let SIMPLE_ES2015_CLASS_FILE: TestFile;
let SIMPLE_CLASS_FILE: TestFile;
let TOPLEVEL_DECORATORS_FILE: TestFile;
let FOO_FUNCTION_FILE: TestFile;
let INVALID_DECORATORS_FILE: TestFile;
let INVALID_DECORATOR_ARGS_FILE: TestFile;
let INVALID_PROP_DECORATORS_FILE: TestFile;
let INVALID_PROP_DECORATOR_ARGS_FILE: TestFile;
let INVALID_CTOR_DECORATORS_FILE: TestFile;
let INVALID_CTOR_DECORATOR_ARGS_FILE: TestFile;
let IMPORTS_FILES: TestFile[];
let EXPORTS_FILES: TestFile[];
let FUNCTION_BODY_FILE: TestFile;
let DECORATED_FILES: TestFile[];
let UNWANTED_PROTOTYPE_EXPORT_FILE: TestFile;
let TYPINGS_SRC_FILES: TestFile[];
let TYPINGS_DTS_FILES: TestFile[];
let MODULE_WITH_PROVIDERS_PROGRAM: TestFile[];
let NAMESPACED_IMPORT_FILE: TestFile;
beforeEach(() => {
_ = absoluteFrom;
SOME_DIRECTIVE_FILE = {
name: _('/some_directive.js'),
contents: `
import { Directive, Inject, InjectionToken, Input } from '@angular/core';
var INJECTED_TOKEN = new InjectionToken('injected');
var ViewContainerRef = {};
var TemplateRef = {};
var SomeDirective = (function() {
function SomeDirective(_viewContainer, _template, injected) {
this.instanceProperty = 'instance';
}
SomeDirective.prototype = {
instanceMethod: function() {},
};
SomeDirective.staticMethod = function() {};
SomeDirective.staticProperty = 'static';
SomeDirective.decorators = [
{ type: Directive, args: [{ selector: '[someDirective]' },] }
];
SomeDirective.ctorParameters = function() { return [
{ type: ViewContainerRef, },
{ type: TemplateRef, },
{ type: undefined, decorators: [{ type: Inject, args: [INJECTED_TOKEN,] },] },
]; };
SomeDirective.propDecorators = {
"input1": [{ type: Input },],
"input2": [{ type: Input },],
};
return SomeDirective;
}());
`,
};
TOPLEVEL_DECORATORS_FILE = {
name: _('/toplevel_decorators.js'),
contents: `
import { Directive, Inject, InjectionToken, Input } from '@angular/core';
var INJECTED_TOKEN = new InjectionToken('injected');
var ViewContainerRef = {};
var TemplateRef = {};
var SomeDirective = (function() {
function SomeDirective(_viewContainer, _template, injected) {}
return SomeDirective;
}());
SomeDirective.decorators = [
{ type: Directive, args: [{ selector: '[someDirective]' },] }
];
SomeDirective.ctorParameters = function() { return [
{ type: ViewContainerRef, },
{ type: TemplateRef, },
{ type: undefined, decorators: [{ type: Inject, args: [INJECTED_TOKEN,] },] },
]; };
SomeDirective.propDecorators = {
"input1": [{ type: Input },],
"input2": [{ type: Input },],
};
`,
};
CTOR_DECORATORS_ARRAY_FILE = {
name: _('/ctor_decorated_as_array.js'),
contents: `
var CtorDecoratedAsArray = (function() {
function CtorDecoratedAsArray(arg1) {
}
CtorDecoratedAsArray.ctorParameters = [{ type: ParamType, decorators: [{ type: Inject },] }];
return CtorDecoratedAsArray;
}());
`,
};
ACCESSORS_FILE = {
name: _('/accessors.js'),
contents: `
import { Directive, Input, Output } from '@angular/core';
var SomeDirective = (function() {
function SomeDirective() {
}
Object.defineProperty(SomeDirective.prototype, "setter", {
set: function (value) { this.value = value; },
enumerable: true,
configurable: true
});
Object.defineProperty(SomeDirective.prototype, "getter", {
get: function () { return null; },
enumerable: true,
configurable: true
});
Object.defineProperty(SomeDirective.prototype, "setterAndGetter", {
get: function () { return null; },
set: function (value) { this.value = value; },
enumerable: true,
configurable: true
});
Object.defineProperty(SomeDirective, "staticSetter", {
set: function (value) { this.value = value; },
enumerable: true,
configurable: true
});
Object.defineProperty(SomeDirective.prototype, "none", {
enumerable: true,
configurable: true
});
Object.defineProperty(SomeDirective.prototype, "incomplete");
SomeDirective.decorators = [
{ type: Directive, args: [{ selector: '[someDirective]' },] }
];
SomeDirective.propDecorators = {
"setter": [{ type: Input },],
"getter": [{ type: Output },],
"setterAndGetter": [{ type: Input },],
};
return SomeDirective;
}());
`,
};
SIMPLE_ES2015_CLASS_FILE = {
name: _('/simple_es2015_class.d.ts'),
contents: `
export class EmptyClass {}
`,
};
SIMPLE_CLASS_FILE = {
name: _('/simple_class.js'),
contents: `
var EmptyClass = (function() {
function EmptyClass() {
}
return EmptyClass;
}());
var NoDecoratorConstructorClass = (function() {
function NoDecoratorConstructorClass(foo) {
}
return NoDecoratorConstructorClass;
}());
var OuterClass1 = (function() {
function InnerClass1() {
}
return InnerClass1;
}());
var OuterClass2 = (function() {
function InnerClass2() {
}
InnerClass2_1 = InnerClass12
var InnerClass2_1;
return InnerClass2;
}());
var SuperClass = (function() { function SuperClass() {} return SuperClass; }());
var ChildClass = /** @class */ (function (_super) {
__extends(ChildClass, _super);
function InnerChildClass() {}
return InnerChildClass;
}(SuperClass);
`,
};
FOO_FUNCTION_FILE = {
name: _('/foo_function.js'),
contents: `
import { Directive } from '@angular/core';
function foo() {}
foo.decorators = [
{ type: Directive, args: [{ selector: '[ignored]' },] }
];
`,
};
INVALID_DECORATORS_FILE = {
name: _('/invalid_decorators.js'),
contents: `
import { Directive } from '@angular/core';
var NotArrayLiteral = (function() {
function NotArrayLiteral() {
}
NotArrayLiteral.decorators = () => [
{ type: Directive, args: [{ selector: '[ignored]' },] },
];
return NotArrayLiteral;
}());
var NotObjectLiteral = (function() {
function NotObjectLiteral() {
}
NotObjectLiteral.decorators = [
"This is not an object literal",
{ type: Directive },
];
return NotObjectLiteral;
}());
var NoTypeProperty = (function() {
function NoTypeProperty() {
}
NoTypeProperty.decorators = [
{ notType: Directive },
{ type: Directive },
];
return NoTypeProperty;
}());
var NotIdentifier = (function() {
function NotIdentifier() {
}
NotIdentifier.decorators = [
{ type: 'StringsLiteralsAreNotIdentifiers' },
{ type: Directive },
];
return NotIdentifier;
}());
`,
};
INVALID_DECORATOR_ARGS_FILE = {
name: _('/invalid_decorator_args.js'),
contents: `
import { Directive } from '@angular/core';
var NoArgsProperty = (function() {
function NoArgsProperty() {
}
NoArgsProperty.decorators = [
{ type: Directive },
];
return NoArgsProperty;
}());
var args = [{ selector: '[ignored]' },];
var NoPropertyAssignment = (function() {
function NoPropertyAssignment() {
}
NoPropertyAssignment.decorators = [
{ type: Directive, args },
];
return NoPropertyAssignment;
}());
var NotArrayLiteral = (function() {
function NotArrayLiteral() {
}
NotArrayLiteral.decorators = [
{ type: Directive, args: () => [{ selector: '[ignored]' },] },
];
return NotArrayLiteral;
}());
`,
};
INVALID_PROP_DECORATORS_FILE = {
name: _('/invalid_prop_decorators.js'),
contents: `
import { Input } from '@angular/core';
var NotObjectLiteral = (function() {
function NotObjectLiteral() {
}
NotObjectLiteral.propDecorators = () => ({
"prop": [{ type: Input },]
});
return NotObjectLiteral;
}());
var NotObjectLiteralProp = (function() {
function NotObjectLiteralProp() {
}
NotObjectLiteralProp.propDecorators = {
"prop": [
"This is not an object literal",
{ type: Input },
]
};
return NotObjectLiteralProp;
}());
var NoTypeProperty = (function() {
function NoTypeProperty() {
}
NoTypeProperty.propDecorators = {
"prop": [
{ notType: Input },
{ type: Input },
]
};
return NoTypeProperty;
}());
var NotIdentifier = (function() {
function NotIdentifier() {
}
NotIdentifier.propDecorators = {
"prop": [
{ type: 'StringsLiteralsAreNotIdentifiers' },
{ type: Input },
]
};
return NotIdentifier;
}());
`,
};
INVALID_PROP_DECORATOR_ARGS_FILE = {
name: _('/invalid_prop_decorator_args.js'),
contents: `
import { Input } from '@angular/core';
var NoArgsProperty = (function() {
function NoArgsProperty() {
}
NoArgsProperty.propDecorators = {
"prop": [{ type: Input },]
};
return NoArgsProperty;
}());
var args = [{ selector: '[ignored]' },];
var NoPropertyAssignment = (function() {
function NoPropertyAssignment() {
}
NoPropertyAssignment.propDecorators = {
"prop": [{ type: Input, args },]
};
return NoPropertyAssignment;
}());
var NotArrayLiteral = (function() {
function NotArrayLiteral() {
}
NotArrayLiteral.propDecorators = {
"prop": [{ type: Input, args: () => [{ selector: '[ignored]' },] },],
};
return NotArrayLiteral;
}());
`,
};
INVALID_CTOR_DECORATORS_FILE = {
name: _('/invalid_ctor_decorators.js'),
contents: `
import { Inject } from '@angular/core';
var NoParametersDecorator = {};
var NoParameters = (function() {
function NoParameters() {}
return NoParameters;
}());
var NotArrayLiteral = (function() {
function NotArrayLiteral(arg1) {
}
NotArrayLiteral.ctorParameters = function() { return 'StringsAreNotArrayLiterals'; };
return NotArrayLiteral;
}());
var NotObjectLiteral = (function() {
function NotObjectLiteral(arg1, arg2) {
}
NotObjectLiteral.ctorParameters = function() { return [
"This is not an object literal",
{ type: 'ParamType', decorators: [{ type: Inject },] },
]; };
return NotObjectLiteral;
}());
var NoTypeProperty = (function() {
function NoTypeProperty(arg1, arg2) {
}
NoTypeProperty.ctorParameters = function() { return [
{
type: 'ParamType',
decorators: [
{ notType: Inject },
{ type: Inject },
]
},
]; };
return NoTypeProperty;
}());
var NotIdentifier = (function() {
function NotIdentifier(arg1, arg2) {
}
NotIdentifier.ctorParameters = function() { return [
{
type: 'ParamType',
decorators: [
{ type: 'StringsLiteralsAreNotIdentifiers' },
{ type: Inject },
]
},
]; };
return NotIdentifier;
}());
`,
};
INVALID_CTOR_DECORATOR_ARGS_FILE = {
name: _('/invalid_ctor_decorator_args.js'),
contents: `
import { Inject } from '@angular/core';
var NoArgsProperty = (function() {
function NoArgsProperty(arg1) {
}
NoArgsProperty.ctorParameters = function() { return [
{ type: 'ParamType', decorators: [{ type: Inject },] },
]; };
return NoArgsProperty;
}());
var args = [{ selector: '[ignored]' },];
var NoPropertyAssignment = (function() {
function NoPropertyAssignment(arg1) {
}
NoPropertyAssignment.ctorParameters = function() { return [
{ type: 'ParamType', decorators: [{ type: Inject, args },] },
]; };
return NoPropertyAssignment;
}());
var NotArrayLiteral = (function() {
function NotArrayLiteral(arg1) {
}
NotArrayLiteral.ctorParameters = function() { return [
{ type: 'ParamType', decorators: [{ type: Inject, args: () => [{ selector: '[ignored]' },] },] },
]; };
return NotArrayLiteral;
}());
`,
};
IMPORTS_FILES = [
{
name: _('/index.js'),
contents: `
import * as a from './a';
import * as b from './b';
import * as c from './c';
`
},
{
name: _('/a.js'),
contents: `
export var a = 'a';
`,
},
{
name: _('/b.js'),
contents: `
import {a} from './a.js';
import {a as foo} from './a.js';
var b = a;
var c = foo;
var d = b;
`,
},
];
EXPORTS_FILES = [
{
name: _('/index.js'),
contents: `
import * as a from './a';
import * as b from './b';
`
},
{
name: _('/a.js'),
contents: `
export var a = 'a';
`,
},
{
name: _('/b.js'),
contents: `
import {Directive} from '@angular/core';
import {a} from './a';
import {a as foo} from './a';
export {Directive} from '@angular/core';
export {a} from './a';
export var b = a;
export var c = foo;
export var d = b;
export var e = 'e';
export var DirectiveX = Directive;
export var SomeClass = (function() {
function SomeClass() {}
return SomeClass;
}());
`,
},
];
FUNCTION_BODY_FILE = {
name: _('/function_body.js'),
contents: `
function foo(x) {
return x;
}
function bar(x, y) {
if (y === void 0) { y = 42; }
return x + y;
}
function complex() {
var x = 42;
return 42;
}
function baz(x) {
var y;
if (x === void 0) { y = 42; }
return y;
}
var y;
function qux(x) {
if (x === void 0) { y = 42; }
return y;
}
function moo() {
var x;
if (x === void 0) { x = 42; }
return x;
}
var x;
function juu() {
if (x === void 0) { x = 42; }
return x;
}
`
};
DECORATED_FILES = [
{
name: _('/primary.js'),
contents: `
import {Directive} from '@angular/core';
import { D } from './secondary';
var A = (function() {
function A() {}
A.decorators = [
{ type: Directive, args: [{ selector: '[a]' }] }
];
return A;
}());
var B = (function() {
function B() {}
B.decorators = [
{ type: Directive, args: [{ selector: '[b]' }] }
];
return B;
}());
function x() {}
function y() {}
var C = (function() {
function C() {}
return C;
});
export { A, x, C };
`
},
{
name: _('/secondary.js'),
contents: `
import {Directive} from '@angular/core';
var D = (function() {
function D() {}
D.decorators = [
{ type: Directive, args: [{ selector: '[d]' }] }
];
return D;
}());
export { D };
`
}
];
UNWANTED_PROTOTYPE_EXPORT_FILE = {
name: _('/library.d.ts'),
contents: `
export declare class SomeParam {
someInstanceMethod(): void;
static someStaticProp: any;
}`
};
TYPINGS_SRC_FILES = [
{
name: _('/src/index.js'),
contents: `
import {InternalClass} from './internal';
import * as func1 from './func1';
import * as missing from './missing-class';
import * as flatFile from './flat-file';
export * from './class1';
export * from './class2';
`
},
{
name: _('/src/class1.js'),
contents: `
var Class1 = (function() {
function Class1() {}
return Class1;
}());
var MissingClass1 = (function() {
function MissingClass1() {}
return MissingClass1;
}());
export {Class1, MissingClass1};
`
},
{
name: _('/src/class2.js'),
contents: `
var Class2 = (function() {
function Class2() {}
return Class2;
}());
export {Class2};
`
},
{name: _('/src/func1.js'), contents: 'function mooFn() {} export {mooFn}'}, {
name: _('/src/internal.js'),
contents: `
var InternalClass = (function() {
function InternalClass() {}
return InternalClass;
}());
var Class2 = (function() {
function Class2() {}
return Class2;
}());
export {InternalClass, Class2};
`
},
{
name: _('/src/missing-class.js'),
contents: `
var MissingClass2 = (function() {
function MissingClass2() {}
return MissingClass2;
}());
export {MissingClass2};
`
},
{
name: _('/src/flat-file.js'),
contents: `
var Class1 = (function() {
function Class1() {}
return Class1;
}());
var MissingClass1 = (function() {
function MissingClass1() {}
return MissingClass1;
}());
var MissingClass2 = (function() {
function MissingClass2() {}
return MissingClass2;
}());
var Class3 = (function() {
function Class3() {}
return Class3;
}());
export {Class1, Class3 as xClass3, MissingClass1, MissingClass2};
`
}
];
TYPINGS_DTS_FILES = [
{
name: _('/typings/index.d.ts'),
contents: `
import {InternalClass} from './internal';
import {mooFn} from './func1';
export * from './class1';
export * from './class2';
`
},
{
name: _('/typings/class1.d.ts'),
contents: `export declare class Class1 {}\nexport declare class OtherClass {}`
},
{
name: _('/typings/class2.d.ts'),
contents:
`export declare class Class2 {}\nexport declare interface SomeInterface {}\nexport {Class3 as xClass3} from './class3';`
},
{name: _('/typings/func1.d.ts'), contents: 'export declare function mooFn(): void;'},
{
name: _('/typings/internal.d.ts'),
contents: `export declare class InternalClass {}\nexport declare class Class2 {}`
},
{name: _('/typings/class3.d.ts'), contents: `export declare class Class3 {}`},
];
MODULE_WITH_PROVIDERS_PROGRAM = [
{
name: _('/src/index.js'),
contents: `
import * as functions from './functions';
import * as methods from './methods';
import * as aliased_class from './aliased_class';
`
},
{
name: _('/src/functions.js'),
contents: `
import {ExternalModule} from './module';
import * as mod from './module';
var SomeService = (function() {
function SomeService() {}
return SomeService;
}());
var InternalModule = (function() {
function InternalModule() {}
return InternalModule;
}());
export function aNumber() { return 42; }
export function aString() { return 'foo'; }
export function emptyObject() { return {}; }
export function ngModuleIdentifier() { return { ngModule: InternalModule }; }
export function ngModuleWithEmptyProviders() { return { ngModule: InternalModule, providers: [] }; }
export function ngModuleWithProviders() { return { ngModule: InternalModule, providers: [SomeService] }; }
export function onlyProviders() { return { providers: [SomeService] }; }
export function ngModuleNumber() { return { ngModule: 42 }; }
export function ngModuleString() { return { ngModule: 'foo' }; }
export function ngModuleObject() { return { ngModule: { foo: 42 } }; }
export function externalNgModule() { return { ngModule: ExternalModule }; }
export function namespacedExternalNgModule() { return { ngModule: mod.ExternalModule }; }
export {SomeService, InternalModule};
`
},
{
name: _('/src/methods.js'),
contents: `
import {ExternalModule} from './module';
import * as mod from './module';
var SomeService = (function() {
function SomeService() {}
return SomeService;
}());
var InternalModule = (function() {
function InternalModule() {}
InternalModule.prototype = {
instanceNgModuleIdentifier: function() { return { ngModule: InternalModule }; },
instanceNgModuleWithEmptyProviders: function() { return { ngModule: InternalModule, providers: [] }; },
instanceNgModuleWithProviders: function() { return { ngModule: InternalModule, providers: [SomeService] }; },
instanceExternalNgModule: function() { return { ngModule: ExternalModule }; },
namespacedExternalNgModule = function() { return { ngModule: mod.ExternalModule }; },
};
InternalModule.aNumber = function() { return 42; };
InternalModule.aString = function() { return 'foo'; };
InternalModule.emptyObject = function() { return {}; };
InternalModule.ngModuleIdentifier = function() { return { ngModule: InternalModule }; };
InternalModule.ngModuleWithEmptyProviders = function() { return { ngModule: InternalModule, providers: [] }; };
InternalModule.ngModuleWithProviders = function() { return { ngModule: InternalModule, providers: [SomeService] }; };
InternalModule.onlyProviders = function() { return { providers: [SomeService] }; };
InternalModule.ngModuleNumber = function() { return { ngModule: 42 }; };
InternalModule.ngModuleString = function() { return { ngModule: 'foo' }; };
InternalModule.ngModuleObject = function() { return { ngModule: { foo: 42 } }; };
InternalModule.externalNgModule = function() { return { ngModule: ExternalModule }; };
InternalModule.namespacedExternalNgModule = function() { return { ngModule: mod.ExternalModule }; };
return InternalModule;
}());
export {SomeService, InternalModule};
`
},
{
name: _('/src/aliased_class.js'),
contents: `
var AliasedModule = (function() {
function AliasedModule() {}
AliasedModule_1 = AliasedModule;
AliasedModule.forRoot = function() { return { ngModule: AliasedModule_1 }; };
var AliasedModule_1;
return AliasedModule;
}());
export { AliasedModule };
`
},
{name: _('/src/module.js'), contents: 'export class ExternalModule {}'},
];
NAMESPACED_IMPORT_FILE = {
name: _('/some_directive.js'),
contents: `
import * as core from '@angular/core';
var SomeDirective = (function() {
function SomeDirective() {
}
SomeDirective.decorators = [
{ type: core.Directive, args: [{ selector: '[someDirective]' },] }
];
return SomeDirective;
}());
`
};
});
describe('getDecoratorsOfDeclaration()', () => {
it('should find the decorators on a class', () => {
loadTestFiles([SOME_DIRECTIVE_FILE]);
const {program} = makeTestBundleProgram(SOME_DIRECTIVE_FILE.name);
const host = new Esm5ReflectionHost(new MockLogger(), false, program.getTypeChecker());
const classNode = getDeclaration(
program, SOME_DIRECTIVE_FILE.name, 'SomeDirective', isNamedVariableDeclaration);
const decorators = host.getDecoratorsOfDeclaration(classNode) !;
expect(decorators).toBeDefined();
expect(decorators.length).toEqual(1);
const decorator = decorators[0];
expect(decorator.name).toEqual('Directive');
expect(decorator.import).toEqual({name: 'Directive', from: '@angular/core'});
expect(decorator.args !.map(arg => arg.getText())).toEqual([
'{ selector: \'[someDirective]\' }',
]);
});
it('should find the decorators on a class at the top level', () => {
loadTestFiles([TOPLEVEL_DECORATORS_FILE]);
const {program} = makeTestBundleProgram(TOPLEVEL_DECORATORS_FILE.name);
const host = new Esm5ReflectionHost(new MockLogger(), false, program.getTypeChecker());
const classNode = getDeclaration(
program, TOPLEVEL_DECORATORS_FILE.name, 'SomeDirective', isNamedVariableDeclaration);
const decorators = host.getDecoratorsOfDeclaration(classNode) !;
expect(decorators).toBeDefined();
expect(decorators.length).toEqual(1);
const decorator = decorators[0];
expect(decorator.name).toEqual('Directive');
expect(decorator.import).toEqual({name: 'Directive', from: '@angular/core'});
expect(decorator.args !.map(arg => arg.getText())).toEqual([
'{ selector: \'[someDirective]\' }',
]);
});
it('should return null if the symbol is not a class', () => {
loadTestFiles([FOO_FUNCTION_FILE]);
const {program} = makeTestBundleProgram(FOO_FUNCTION_FILE.name);
const host = new Esm5ReflectionHost(new MockLogger(), false, program.getTypeChecker());
const functionNode =
getDeclaration(program, FOO_FUNCTION_FILE.name, 'foo', isNamedFunctionDeclaration);
const decorators = host.getDecoratorsOfDeclaration(functionNode);
expect(decorators).toBe(null);
});
it('should return null if there are no decorators', () => {
loadTestFiles([SIMPLE_CLASS_FILE]);
const {program} = makeTestBundleProgram(SIMPLE_CLASS_FILE.name);
const host = new Esm5ReflectionHost(new MockLogger(), false, program.getTypeChecker());
const classNode = getDeclaration(
program, SIMPLE_CLASS_FILE.name, 'EmptyClass', isNamedVariableDeclaration);
const decorators = host.getDecoratorsOfDeclaration(classNode);
expect(decorators).toBe(null);
});
it('should ignore `decorators` if it is not an array literal', () => {
loadTestFiles([INVALID_DECORATORS_FILE]);
const {program} = makeTestBundleProgram(INVALID_DECORATORS_FILE.name);
const host = new Esm5ReflectionHost(new MockLogger(), false, program.getTypeChecker());
const classNode = getDeclaration(
program, INVALID_DECORATORS_FILE.name, 'NotArrayLiteral', isNamedVariableDeclaration);
const decorators = host.getDecoratorsOfDeclaration(classNode);
expect(decorators).toEqual([]);
});
it('should ignore decorator elements that are not object literals', () => {
loadTestFiles([INVALID_DECORATORS_FILE]);
const {program} = makeTestBundleProgram(INVALID_DECORATORS_FILE.name);
const host = new Esm5ReflectionHost(new MockLogger(), false, program.getTypeChecker());
const classNode = getDeclaration(
program, INVALID_DECORATORS_FILE.name, 'NotObjectLiteral', isNamedVariableDeclaration);
const decorators = host.getDecoratorsOfDeclaration(classNode) !;
expect(decorators.length).toBe(1);
expect(decorators[0]).toEqual(jasmine.objectContaining<Decorator>({name: 'Directive'}));
});
it('should ignore decorator elements that have no `type` property', () => {
loadTestFiles([INVALID_DECORATORS_FILE]);
const {program} = makeTestBundleProgram(INVALID_DECORATORS_FILE.name);
const host = new Esm5ReflectionHost(new MockLogger(), false, program.getTypeChecker());
const classNode = getDeclaration(
program, INVALID_DECORATORS_FILE.name, 'NoTypeProperty', isNamedVariableDeclaration);
const decorators = host.getDecoratorsOfDeclaration(classNode) !;
expect(decorators.length).toBe(1);
expect(decorators[0]).toEqual(jasmine.objectContaining({name: 'Directive'}));
});
it('should ignore decorator elements whose `type` value is not an identifier', () => {
loadTestFiles([INVALID_DECORATORS_FILE]);
const {program} = makeTestBundleProgram(INVALID_DECORATORS_FILE.name);
const host = new Esm5ReflectionHost(new MockLogger(), false, program.getTypeChecker());
const classNode = getDeclaration(
program, INVALID_DECORATORS_FILE.name, 'NotIdentifier', isNamedVariableDeclaration);
const decorators = host.getDecoratorsOfDeclaration(classNode) !;
expect(decorators.length).toBe(1);
expect(decorators[0]).toEqual(jasmine.objectContaining({name: 'Directive'}));
});
it('should have import information on decorators', () => {
loadTestFiles([SOME_DIRECTIVE_FILE]);
const {program} = makeTestBundleProgram(SOME_DIRECTIVE_FILE.name);
const host = new Esm5ReflectionHost(new MockLogger(), false, program.getTypeChecker());
const classNode = getDeclaration(
program, SOME_DIRECTIVE_FILE.name, 'SomeDirective', isNamedVariableDeclaration);
const decorators = host.getDecoratorsOfDeclaration(classNode) !;
expect(decorators.length).toEqual(1);
expect(decorators[0].import).toEqual({name: 'Directive', from: '@angular/core'});
});
describe('(returned decorators `args`)', () => {
it('should be an empty array if decorator has no `args` property', () => {
loadTestFiles([INVALID_DECORATOR_ARGS_FILE]);
const {program} = makeTestBundleProgram(INVALID_DECORATOR_ARGS_FILE.name);
const host = new Esm5ReflectionHost(new MockLogger(), false, program.getTypeChecker());
const classNode = getDeclaration(
program, INVALID_DECORATOR_ARGS_FILE.name, 'NoArgsProperty',
isNamedVariableDeclaration);
const decorators = host.getDecoratorsOfDeclaration(classNode) !;
expect(decorators.length).toBe(1);
expect(decorators[0].name).toBe('Directive');
expect(decorators[0].args).toEqual([]);
});
it('should be an empty array if decorator\'s `args` has no property assignment', () => {
loadTestFiles([INVALID_DECORATOR_ARGS_FILE]);
const {program} = makeTestBundleProgram(INVALID_DECORATOR_ARGS_FILE.name);
const host = new Esm5ReflectionHost(new MockLogger(), false, program.getTypeChecker());
const classNode = getDeclaration(
program, INVALID_DECORATOR_ARGS_FILE.name, 'NoPropertyAssignment',
isNamedVariableDeclaration);
const decorators = host.getDecoratorsOfDeclaration(classNode) !;
expect(decorators.length).toBe(1);
expect(decorators[0].name).toBe('Directive');