-
Notifications
You must be signed in to change notification settings - Fork 24.8k
/
component_spec.ts
713 lines (653 loc) Β· 26.8 KB
/
component_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
/**
* @license
* Copyright Google LLC 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 {ConstantPool} from '@angular/compiler';
import ts from 'typescript';
import {CycleAnalyzer, CycleHandlingStrategy, ImportGraph} from '../../../cycles';
import {ErrorCode, FatalDiagnosticError, ngErrorCode} from '../../../diagnostics';
import {absoluteFrom} from '../../../file_system';
import {runInEachFileSystem} from '../../../file_system/testing';
import {DeferredSymbolTracker, ImportedSymbolsTracker, ModuleResolver, Reference, ReferenceEmitter} from '../../../imports';
import {CompoundMetadataReader, DtsMetadataReader, HostDirectivesResolver, LocalMetadataRegistry, ResourceRegistry} from '../../../metadata';
import {PartialEvaluator} from '../../../partial_evaluator';
import {NOOP_PERF_RECORDER} from '../../../perf';
import {isNamedClassDeclaration, TypeScriptReflectionHost} from '../../../reflection';
import {LocalModuleScopeRegistry, MetadataDtsModuleScopeResolver, TypeCheckScopeRegistry} from '../../../scope';
import {getDeclaration, makeProgram} from '../../../testing';
import {CompilationMode} from '../../../transform';
import {InjectableClassRegistry, NoopReferencesRegistry, ResourceLoader, ResourceLoaderContext} from '../../common';
import {ComponentDecoratorHandler} from '../src/handler';
export class StubResourceLoader implements ResourceLoader {
resolve(v: string): string {
return v;
}
canPreload = false;
canPreprocess = false;
load(v: string): string {
return '';
}
preload(): Promise<void>|undefined {
throw new Error('Not implemented');
}
preprocessInline(_data: string, _context: ResourceLoaderContext): Promise<string> {
throw new Error('Not implemented');
}
}
function setup(
program: ts.Program, options: ts.CompilerOptions, host: ts.CompilerHost,
opts: {compilationMode: CompilationMode, usePoisonedData?: boolean} = {
compilationMode: CompilationMode.FULL
}) {
const {compilationMode, usePoisonedData} = opts;
const checker = program.getTypeChecker();
const reflectionHost = new TypeScriptReflectionHost(checker);
const evaluator = new PartialEvaluator(reflectionHost, checker, /* dependencyTracker */ null);
const moduleResolver =
new ModuleResolver(program, options, host, /* moduleResolutionCache */ null);
const importGraph = new ImportGraph(checker, NOOP_PERF_RECORDER);
const cycleAnalyzer = new CycleAnalyzer(importGraph);
const metaRegistry = new LocalMetadataRegistry();
const dtsReader = new DtsMetadataReader(checker, reflectionHost);
const dtsResolver = new MetadataDtsModuleScopeResolver(dtsReader, null);
const metaReader = new CompoundMetadataReader([metaRegistry, dtsReader]);
const scopeRegistry = new LocalModuleScopeRegistry(
metaRegistry, metaReader, dtsResolver, new ReferenceEmitter([]), null);
const refEmitter = new ReferenceEmitter([]);
const referencesRegistry = new NoopReferencesRegistry();
const injectableRegistry = new InjectableClassRegistry(reflectionHost, /* isCore */ false);
const resourceRegistry = new ResourceRegistry();
const hostDirectivesResolver = new HostDirectivesResolver(metaReader);
const typeCheckScopeRegistry =
new TypeCheckScopeRegistry(scopeRegistry, metaReader, hostDirectivesResolver);
const resourceLoader = new StubResourceLoader();
const importTracker = new ImportedSymbolsTracker();
const handler = new ComponentDecoratorHandler(
reflectionHost,
evaluator,
metaRegistry,
metaReader,
scopeRegistry,
dtsResolver,
scopeRegistry,
typeCheckScopeRegistry,
resourceRegistry,
/* isCore */ false,
/* strictCtorDeps */ false,
resourceLoader,
/* rootDirs */['/'],
/* defaultPreserveWhitespaces */ false,
/* i18nUseExternalIds */ true,
/* enableI18nLegacyMessageIdFormat */ false,
!!usePoisonedData,
/* i18nNormalizeLineEndingsInICUs */ false,
moduleResolver,
cycleAnalyzer,
CycleHandlingStrategy.UseRemoteScoping,
refEmitter,
referencesRegistry,
/* depTracker */ null,
injectableRegistry,
/* semanticDepGraphUpdater */ null,
/* annotateForClosureCompiler */ false,
NOOP_PERF_RECORDER,
hostDirectivesResolver,
importTracker,
true,
compilationMode,
new DeferredSymbolTracker(checker, /* onlyExplicitDeferDependencyImports */ false),
/* forbidOrphanRenderering */ false,
/* enableBlockSyntax */ true,
/* useTemplatePipeline */ true,
/* localCompilationExtraImportsTracker */ null,
);
return {reflectionHost, handler, resourceLoader, metaRegistry};
}
runInEachFileSystem(() => {
describe('ComponentDecoratorHandler', () => {
let _: typeof absoluteFrom;
beforeEach(() => _ = absoluteFrom);
it('should produce a diagnostic when @Component has non-literal argument', () => {
const {program, options, host} = makeProgram([
{
name: _('/node_modules/@angular/core/index.d.ts'),
contents: 'export const Component: any;',
},
{
name: _('/entry.ts'),
contents: `
import {Component} from '@angular/core';
const TEST = '';
@Component(TEST) class TestCmp {}
`
},
]);
const {reflectionHost, handler} = setup(program, options, host);
const TestCmp = getDeclaration(program, _('/entry.ts'), 'TestCmp', isNamedClassDeclaration);
const detected = handler.detect(TestCmp, reflectionHost.getDecoratorsOfDeclaration(TestCmp));
if (detected === undefined) {
return fail('Failed to recognize @Component');
}
try {
handler.analyze(TestCmp, detected.metadata);
return fail('Analysis should have failed');
} catch (err) {
if (!(err instanceof FatalDiagnosticError)) {
return fail('Error should be a FatalDiagnosticError');
}
const diag = err.toDiagnostic();
expect(diag.code).toEqual(ivyCode(ErrorCode.DECORATOR_ARG_NOT_LITERAL));
expect(diag.file.fileName.endsWith('entry.ts')).toBe(true);
expect(diag.start).toBe(detected.metadata.args![0].getStart());
}
});
it('should keep track of inline template', () => {
const template = '<span>inline</span>';
const {program, options, host} = makeProgram([
{
name: _('/node_modules/@angular/core/index.d.ts'),
contents: 'export const Component: any;',
},
{
name: _('/entry.ts'),
contents: `
import {Component} from '@angular/core';
@Component({
template: '${template}',
}) class TestCmp {}
`
},
]);
const {reflectionHost, handler} = setup(program, options, host);
const TestCmp = getDeclaration(program, _('/entry.ts'), 'TestCmp', isNamedClassDeclaration);
const detected = handler.detect(TestCmp, reflectionHost.getDecoratorsOfDeclaration(TestCmp));
if (detected === undefined) {
return fail('Failed to recognize @Component');
}
const {analysis} = handler.analyze(TestCmp, detected.metadata);
expect(analysis?.resources.template.path).toBeNull();
expect(analysis?.resources.template.expression.getText()).toEqual(`'${template}'`);
});
it('should keep track of external template', () => {
const templateUrl = '/myTemplate.ng.html';
const {program, options, host} = makeProgram([
{
name: _('/node_modules/@angular/core/index.d.ts'),
contents: 'export const Component: any;',
},
{
name: _(templateUrl),
contents: '<div>hello world</div>',
},
{
name: _('/entry.ts'),
contents: `
import {Component} from '@angular/core';
@Component({
templateUrl: '${templateUrl}',
}) class TestCmp {}
`
},
]);
const {reflectionHost, handler} = setup(program, options, host);
const TestCmp = getDeclaration(program, _('/entry.ts'), 'TestCmp', isNamedClassDeclaration);
const detected = handler.detect(TestCmp, reflectionHost.getDecoratorsOfDeclaration(TestCmp));
if (detected === undefined) {
return fail('Failed to recognize @Component');
}
const {analysis} = handler.analyze(TestCmp, detected.metadata);
expect(analysis?.resources.template.path).toContain(templateUrl);
expect(analysis?.resources.template.expression.getText()).toContain(`'${templateUrl}'`);
});
it('should keep track of internal and external styles', () => {
const {program, options, host} = makeProgram([
{
name: _('/node_modules/@angular/core/index.d.ts'),
contents: 'export const Component: any;',
},
{
name: _('/myStyle.css'),
contents: '<div>hello world</div>',
},
{
name: _('/entry.ts'),
contents: `
import {Component} from '@angular/core';
// These are ignored because we only attempt to store string literals
const ignoredStyleUrl = 'asdlfkj';
const ignoredStyle = '';
@Component({
template: '',
styleUrls: ['/myStyle.css', ignoredStyleUrl],
styles: ['a { color: red; }', 'b { color: blue; }', ignoredStyle, ...[ignoredStyle]],
}) class TestCmp {}
`
},
]);
const {reflectionHost, handler} = setup(program, options, host);
const TestCmp = getDeclaration(program, _('/entry.ts'), 'TestCmp', isNamedClassDeclaration);
const detected = handler.detect(TestCmp, reflectionHost.getDecoratorsOfDeclaration(TestCmp));
if (detected === undefined) {
return fail('Failed to recognize @Component');
}
const {analysis} = handler.analyze(TestCmp, detected.metadata);
expect(analysis?.resources.styles.size).toBe(3);
});
it('should use an empty source map URL for an indirect template', () => {
const template = '<span>indirect</span>';
const {program, options, host} = makeProgram([
{
name: _('/node_modules/@angular/core/index.d.ts'),
contents: 'export const Component: any;',
},
{
name: _('/entry.ts'),
contents: `
import {Component} from '@angular/core';
const TEMPLATE = '${template}';
@Component({
template: TEMPLATE,
}) class TestCmp {}
`
},
]);
const {reflectionHost, handler} = setup(program, options, host);
const TestCmp = getDeclaration(program, _('/entry.ts'), 'TestCmp', isNamedClassDeclaration);
const detected = handler.detect(TestCmp, reflectionHost.getDecoratorsOfDeclaration(TestCmp));
if (detected === undefined) {
return fail('Failed to recognize @Component');
}
const {analysis} = handler.analyze(TestCmp, detected.metadata);
expect(analysis?.template.file?.url).toEqual('');
});
it('does not emit a program with template parse errors', () => {
const template = '{{x ? y }}';
const {program, options, host} = makeProgram([
{
name: _('/node_modules/@angular/core/index.d.ts'),
contents: 'export const Component: any;',
},
{
name: _('/entry.ts'),
contents: `
import {Component} from '@angular/core';
@Component({
template: '${template}',
}) class TestCmp {}
`
},
]);
const {reflectionHost, handler} = setup(program, options, host);
const TestCmp = getDeclaration(program, _('/entry.ts'), 'TestCmp', isNamedClassDeclaration);
const detected = handler.detect(TestCmp, reflectionHost.getDecoratorsOfDeclaration(TestCmp));
if (detected === undefined) {
return fail('Failed to recognize @Component');
}
const {analysis} = handler.analyze(TestCmp, detected.metadata);
const symbol = handler.symbol(TestCmp, analysis!);
const resolution = handler.resolve(TestCmp, analysis!, symbol);
const compileResult =
handler.compileFull(TestCmp, analysis!, resolution.data!, new ConstantPool());
expect(compileResult).toEqual([]);
});
it('should replace inline style content with transformed content', async () => {
const {program, options, host} = makeProgram([
{
name: _('/node_modules/@angular/core/index.d.ts'),
contents: 'export const Component: any;',
},
{
name: _('/entry.ts'),
contents: `
import {Component} from '@angular/core';
@Component({
template: '',
styles: ['.abc {}']
}) class TestCmp {}
`
},
]);
const {reflectionHost, handler, resourceLoader} = setup(program, options, host);
resourceLoader.canPreload = true;
resourceLoader.canPreprocess = true;
resourceLoader.preprocessInline = async function(data, context) {
expect(data).toBe('.abc {}');
expect(context.containingFile).toBe(_('/entry.ts').toLowerCase());
expect(context.type).toBe('style');
return '.xyz {}';
};
const TestCmp = getDeclaration(program, _('/entry.ts'), 'TestCmp', isNamedClassDeclaration);
const detected = handler.detect(TestCmp, reflectionHost.getDecoratorsOfDeclaration(TestCmp));
if (detected === undefined) {
return fail('Failed to recognize @Component');
}
await handler.preanalyze(TestCmp, detected.metadata);
const {analysis} = handler.analyze(TestCmp, detected.metadata);
expect(analysis?.inlineStyles).toEqual(jasmine.arrayWithExactContents(['.xyz {}']));
});
it('should error if canPreprocess is true and async analyze is not used', async () => {
const {program, options, host} = makeProgram([
{
name: _('/node_modules/@angular/core/index.d.ts'),
contents: 'export const Component: any;',
},
{
name: _('/entry.ts'),
contents: `
import {Component} from '@angular/core';
@Component({
template: '',
styles: ['.abc {}']
}) class TestCmp {}
`
},
]);
const {reflectionHost, handler, resourceLoader} = setup(program, options, host);
resourceLoader.canPreload = true;
resourceLoader.canPreprocess = true;
const TestCmp = getDeclaration(program, _('/entry.ts'), 'TestCmp', isNamedClassDeclaration);
const detected = handler.detect(TestCmp, reflectionHost.getDecoratorsOfDeclaration(TestCmp));
if (detected === undefined) {
return fail('Failed to recognize @Component');
}
expect(() => handler.analyze(TestCmp, detected.metadata))
.toThrowError('Inline resource processing requires asynchronous preanalyze.');
});
it('should not error if component has no inline styles and canPreprocess is true', async () => {
const {program, options, host} = makeProgram([
{
name: _('/node_modules/@angular/core/index.d.ts'),
contents: 'export const Component: any;',
},
{
name: _('/entry.ts'),
contents: `
import {Component} from '@angular/core';
@Component({
template: '',
}) class TestCmp {}
`
},
]);
const {reflectionHost, handler, resourceLoader} = setup(program, options, host);
resourceLoader.canPreload = true;
resourceLoader.canPreprocess = true;
resourceLoader.preprocessInline = async function(data, context) {
fail('preprocessInline should not have been called.');
return data;
};
const TestCmp = getDeclaration(program, _('/entry.ts'), 'TestCmp', isNamedClassDeclaration);
const detected = handler.detect(TestCmp, reflectionHost.getDecoratorsOfDeclaration(TestCmp));
if (detected === undefined) {
return fail('Failed to recognize @Component');
}
await handler.preanalyze(TestCmp, detected.metadata);
expect(() => handler.analyze(TestCmp, detected.metadata)).not.toThrow();
});
it('should evaluate the name of animations', () => {
const {program, options, host} = makeProgram([
{
name: _('/node_modules/@angular/core/index.d.ts'),
contents: 'export const Component: any;',
},
{
name: _('/node_modules/@angular/animations/index.d.ts'),
contents: 'export declare function trigger(name: any): any',
},
{
name: _('/entry.ts'),
contents: `
import {Component} from '@angular/core';
import {trigger} from '@angular/animations';
@Component({
template: '',
animations: [
trigger('animationName'),
[trigger('nestedAnimationName')],
],
})
class TestCmp {}
`
},
]);
const {reflectionHost, handler, metaRegistry} = setup(program, options, host);
const TestCmp = getDeclaration(program, _('/entry.ts'), 'TestCmp', isNamedClassDeclaration);
const detected = handler.detect(TestCmp, reflectionHost.getDecoratorsOfDeclaration(TestCmp));
if (detected === undefined) {
return fail('Failed to recognize @Component');
}
const {analysis} = handler.analyze(TestCmp, detected.metadata);
handler.register(TestCmp, analysis!);
const meta = metaRegistry.getDirectiveMetadata(new Reference(TestCmp));
expect(meta?.animationTriggerNames?.staticTriggerNames).toEqual([
'animationName', 'nestedAnimationName'
]);
expect(meta?.animationTriggerNames?.includesDynamicAnimations).toBeFalse();
});
it('should tell if the animations include a dynamic value', () => {
const {program, options, host} = makeProgram([
{
name: _('/node_modules/@angular/core/index.d.ts'),
contents: 'export const Component: any;',
},
{
name: _('/node_modules/@angular/animations/index.d.ts'),
contents: 'export declare function trigger(name: any): any',
},
{
name: _('/entry.ts'),
contents: `
import {Component} from '@angular/core';
import {trigger} from '@angular/animations';
function buildComplexAnimations() {
const name = 'complex';
return [trigger(name)];
}
@Component({
template: '',
animations: [
trigger('animationName'),
buildComplexAnimations(),
],
})
class TestCmp {}
`
},
]);
const {reflectionHost, handler, metaRegistry} = setup(program, options, host);
const TestCmp = getDeclaration(program, _('/entry.ts'), 'TestCmp', isNamedClassDeclaration);
const detected = handler.detect(TestCmp, reflectionHost.getDecoratorsOfDeclaration(TestCmp));
if (detected === undefined) {
return fail('Failed to recognize @Component');
}
const {analysis} = handler.analyze(TestCmp, detected.metadata);
handler.register(TestCmp, analysis!);
const meta = metaRegistry.getDirectiveMetadata(new Reference(TestCmp));
expect(meta?.animationTriggerNames?.staticTriggerNames).toEqual(['animationName']);
expect(meta?.animationTriggerNames?.includesDynamicAnimations).toBeTrue();
});
it('should treat complex animations expressions as dynamic', () => {
const {program, options, host} = makeProgram([
{
name: _('/node_modules/@angular/core/index.d.ts'),
contents: 'export const Component: any;',
},
{
name: _('/node_modules/@angular/animations/index.d.ts'),
contents: 'export declare function trigger(name: any): any',
},
{
name: _('/entry.ts'),
contents: `
import {Component} from '@angular/core';
import {trigger} from '@angular/animations';
function buildComplexAnimations() {
const name = 'complex';
return [trigger(name)];
}
@Component({
template: '',
animations: buildComplexAnimations(),
})
class TestCmp {}
`
},
]);
const {reflectionHost, handler, metaRegistry} = setup(program, options, host);
const TestCmp = getDeclaration(program, _('/entry.ts'), 'TestCmp', isNamedClassDeclaration);
const detected = handler.detect(TestCmp, reflectionHost.getDecoratorsOfDeclaration(TestCmp));
if (detected === undefined) {
return fail('Failed to recognize @Component');
}
const {analysis} = handler.analyze(TestCmp, detected.metadata);
handler.register(TestCmp, analysis!);
const meta = metaRegistry.getDirectiveMetadata(new Reference(TestCmp));
expect(meta?.animationTriggerNames?.includesDynamicAnimations).toBeTrue();
expect(meta?.animationTriggerNames?.staticTriggerNames.length).toBe(0);
});
describe('localCompilation', () => {
it('should not produce diagnostic for cross-file imports in standalone component', () => {
const {program, options, host} = makeProgram(
[
{
name: _('/node_modules/@angular/core/index.d.ts'),
contents: 'export const Component: any;',
},
{
name: _('/entry.ts'),
contents: `
import {Component} from '@angular/core';
import {SomeModule} from './some_where';
@Component({
standalone: true,
selector: 'main',
template: '<span>Hi!</span>',
imports: [SomeModule],
}) class TestCmp {}
`
},
],
undefined, undefined, false);
const {reflectionHost, handler} =
setup(program, options, host, {compilationMode: CompilationMode.LOCAL});
const TestCmp = getDeclaration(program, _('/entry.ts'), 'TestCmp', isNamedClassDeclaration);
const detected =
handler.detect(TestCmp, reflectionHost.getDecoratorsOfDeclaration(TestCmp));
if (detected === undefined) {
return fail('Failed to recognize @Component');
}
const {diagnostics} = handler.analyze(TestCmp, detected.metadata);
expect(diagnostics).toBeUndefined();
});
it('should produce diagnostic for imports in non-standalone component', () => {
const {program, options, host} = makeProgram(
[
{
name: _('/node_modules/@angular/core/index.d.ts'),
contents: 'export const Component: any;',
},
{
name: _('/entry.ts'),
contents: `
import {Component} from '@angular/core';
import {SomeModule} from './some_where';
@Component({
selector: 'main',
template: '<span>Hi!</span>',
imports: [SomeModule],
}) class TestCmp {}
`
},
],
undefined, undefined, false);
const {reflectionHost, handler} =
setup(program, options, host, {compilationMode: CompilationMode.LOCAL});
const TestCmp = getDeclaration(program, _('/entry.ts'), 'TestCmp', isNamedClassDeclaration);
const detected =
handler.detect(TestCmp, reflectionHost.getDecoratorsOfDeclaration(TestCmp));
if (detected === undefined) {
return fail('Failed to recognize @Component');
}
const {diagnostics} = handler.analyze(TestCmp, detected.metadata);
expect(diagnostics).toContain(jasmine.objectContaining({
code: ngErrorCode(ErrorCode.COMPONENT_NOT_STANDALONE),
messageText: jasmine.stringContaining(`'imports' is only valid`),
}));
});
it('should not produce diagnostic for cross-file schemas in standalone component', () => {
const {program, options, host} = makeProgram(
[
{
name: _('/node_modules/@angular/core/index.d.ts'),
contents: 'export const Component: any; export const CUSTOM_ELEMENTS_SCHEMA: any;',
},
{
name: _('/entry.ts'),
contents: `
import {Component, CUSTOM_ELEMENTS_SCHEMA} from '@angular/core';
import {SomeModule} from './some_where';
@Component({
standalone: true,
selector: 'main',
template: '<span>Hi!</span>',
schemas: [CUSTOM_ELEMENTS_SCHEMA],
}) class TestCmp {}
`
},
],
undefined, undefined, false);
const {reflectionHost, handler} =
setup(program, options, host, {compilationMode: CompilationMode.LOCAL});
const TestCmp = getDeclaration(program, _('/entry.ts'), 'TestCmp', isNamedClassDeclaration);
const detected =
handler.detect(TestCmp, reflectionHost.getDecoratorsOfDeclaration(TestCmp));
if (detected === undefined) {
return fail('Failed to recognize @Component');
}
const {diagnostics} = handler.analyze(TestCmp, detected.metadata);
expect(diagnostics).toBeUndefined();
});
it('should produce diagnostic for schemas in non-standalone component', () => {
const {program, options, host} = makeProgram(
[
{
name: _('/node_modules/@angular/core/index.d.ts'),
contents: 'export const Component: any; export const CUSTOM_ELEMENTS_SCHEMA: any;',
},
{
name: _('/entry.ts'),
contents: `
import {Component, CUSTOM_ELEMENTS_SCHEMA} from '@angular/core';
import {SomeModule} from './some_where';
@Component({
selector: 'main',
template: '<span>Hi!</span>',
schemas: [CUSTOM_ELEMENTS_SCHEMA],
}) class TestCmp {}
`
},
],
undefined, undefined, false);
const {reflectionHost, handler} =
setup(program, options, host, {compilationMode: CompilationMode.LOCAL});
const TestCmp = getDeclaration(program, _('/entry.ts'), 'TestCmp', isNamedClassDeclaration);
const detected =
handler.detect(TestCmp, reflectionHost.getDecoratorsOfDeclaration(TestCmp));
if (detected === undefined) {
return fail('Failed to recognize @Component');
}
const {diagnostics} = handler.analyze(TestCmp, detected.metadata);
expect(diagnostics).toContain(jasmine.objectContaining({
code: ngErrorCode(ErrorCode.COMPONENT_NOT_STANDALONE),
messageText: jasmine.stringContaining(`'schemas' is only valid`),
}));
});
});
});
function ivyCode(code: ErrorCode): number {
return Number('-99' + code.valueOf());
}
});