@@ -274,5 +274,197 @@ runInEachFileSystem(() => {
274274 'Unsupported call to the input function. This function can only be used as the initializer of a property on a @Component or @Directive class.' ,
275275 ) ;
276276 } ) ;
277+
278+ describe ( 'required initializer functions validation' , ( ) => {
279+ it ( 'should report required input being used as property initialized' , ( ) => {
280+ env . write (
281+ 'test.ts' ,
282+ `
283+ import {input, Component} from '@angular/core';
284+
285+ function foobar(x:any) {}
286+
287+ @Component({template: \`\`})
288+ export class Test {
289+ reqInp = input.required<string>();
290+ reqInp2 = input.required<boolean>();
291+ smthg = this.reqInp();
292+ smthg2 = foobar(this.reqInp2());
293+ }
294+ ` ,
295+ ) ;
296+
297+ const diags = env . driveDiagnostics ( ) ;
298+ expect ( diags . length ) . toBe ( 2 ) ;
299+ expect ( diags [ 0 ] . messageText ) . toContain ( '`reqInp` is a required `input`' ) ;
300+ expect ( diags [ 1 ] . messageText ) . toContain ( '`reqInp2` is a required `input`' ) ;
301+ } ) ;
302+
303+ it ( 'should report required input being invoked inside a constructor' , ( ) => {
304+ env . write (
305+ 'test.ts' ,
306+ `
307+ import {input, Component} from '@angular/core';
308+
309+ function foobar(x:any) {}
310+
311+ @Component({template: \`\`})
312+ export class Test {
313+ reqInp = input.required<string>();
314+ reqInp2 = input.required<boolean>();
315+
316+ constructor() {
317+ const smthg = this.reqInp();
318+ const smthg2 = foobar(this.reqInp2());
319+ }
320+ }
321+ ` ,
322+ ) ;
323+
324+ const diags = env . driveDiagnostics ( ) ;
325+ expect ( diags . length ) . toBe ( 2 ) ;
326+ expect ( diags [ 0 ] . messageText ) . toContain ( '`reqInp` is a required `input`' ) ;
327+ expect ( diags [ 1 ] . messageText ) . toContain ( '`reqInp2` is a required `input`' ) ;
328+ } ) ;
329+
330+ it ( 'should report required model being used as property initialized' , ( ) => {
331+ env . write (
332+ 'test.ts' ,
333+ `
334+ import {model, Component} from '@angular/core';
335+
336+ @Component({template: \`\`})
337+ export class Test {
338+ reqModel = model.required<string>();
339+ smthg = this.reqModel();
340+ }
341+ ` ,
342+ ) ;
343+
344+ const diags = env . driveDiagnostics ( ) ;
345+ expect ( diags . length ) . toBe ( 1 ) ;
346+ expect ( diags [ 0 ] . messageText ) . toContain ( '`reqModel` is a required `model`' ) ;
347+ } ) ;
348+
349+ it ( 'should report required model being invoked in a constructor despite being set before hand' , ( ) => {
350+ // While this code might be valid at runtime, we still prefer to flag any required model reads.
351+ env . write (
352+ 'test.ts' ,
353+ `
354+ import {model, Component} from '@angular/core';
355+
356+ @Component({template: \`\`})
357+ export class Test {
358+ reqModel = model.required<string>();
359+ constructor() {
360+ this.reqModel.set('foobar');
361+ const smthg = this.reqModel();
362+ }
363+ }
364+ ` ,
365+ ) ;
366+
367+ const diags = env . driveDiagnostics ( ) ;
368+ expect ( diags . length ) . toBe ( 1 ) ;
369+ expect ( diags [ 0 ] . messageText ) . toContain ( '`reqModel` is a required `model`' ) ;
370+ } ) ;
371+
372+ it ( 'should report required viewChild being used as property initialized' , ( ) => {
373+ env . write (
374+ 'test.ts' ,
375+ `
376+ import {viewChild, Component} from '@angular/core';
377+
378+ @Component({template: \`\`})
379+ export class Test {
380+ reqChild = viewChild.required('someRef');
381+ smthg = this.reqChild();
382+ }
383+ ` ,
384+ ) ;
385+
386+ const diags = env . driveDiagnostics ( ) ;
387+ expect ( diags . length ) . toBe ( 1 ) ;
388+ expect ( diags [ 0 ] . messageText ) . toContain ( '`reqChild` is a required `viewChild`' ) ;
389+ } ) ;
390+
391+ it ( 'should not report required input being invoked inside an effect or a computed' , ( ) => {
392+ env . write (
393+ 'test.ts' ,
394+ `
395+ import {input, Component, effect, computed} from '@angular/core';
396+
397+ function foobar(x:any) {}
398+
399+ @Component({template: \`\`})
400+ export class Test {
401+ reqInp = input.required<string>();
402+ reqInp2 = input.required<boolean>();
403+ _ = effect(() => { this.reqInp(); foobar(this.reqInp2()); });
404+ __ = effect(function() { this.reqInp(); foobar(this.reqInp2()); });
405+ comp = computed(() => this.reqInp());
406+
407+ constructor() {
408+ effect(() => { this.reqInp(); foobar(this.reqInp2()); });
409+ }
410+ }
411+ ` ,
412+ ) ;
413+
414+ const diags = env . driveDiagnostics ( ) ;
415+ expect ( diags . length ) . toBe ( 0 ) ;
416+ } ) ;
417+
418+ it ( 'should not report non-required input being invoked in contructrors or in property initialization' , ( ) => {
419+ env . write (
420+ 'test.ts' ,
421+ `
422+ import {input, Component, effect, computed} from '@angular/core';
423+
424+ function foobar(x:any) {}
425+
426+ @Component({template: \`\`})
427+ export class Test {
428+ reqInp = input<string>('');
429+ reqInp2 = input<boolean>(true);
430+ smthg = this.reqInp();
431+ smthg2 = foobar(this.reqInp2());
432+
433+ constructor() {
434+ const smthg = this.reqInp();
435+ const smthg2 = foobar(this.reqInp2());
436+ }
437+ }
438+ ` ,
439+ ) ;
440+
441+ const diags = env . driveDiagnostics ( ) ;
442+ expect ( diags . length ) . toBe ( 0 ) ;
443+ } ) ;
444+
445+ it ( 'should not report non-invocations of required inputs' , ( ) => {
446+ env . write (
447+ 'test.ts' ,
448+ `
449+ import {input, Component, effect, computed} from '@angular/core';
450+ import {toObservable} from '@angular/core/rxjs-interop';
451+
452+
453+ @Component({template: \`\`})
454+ export class Test {
455+ reqInp = input.required<string>();
456+ obs$ = toObservable(this.reqInp);
457+
458+ constructor() {
459+ const obs$ = toObservable(this.reqInp);
460+ }
461+ }
462+ ` ,
463+ ) ;
464+
465+ const diags = env . driveDiagnostics ( ) ;
466+ expect ( diags . length ) . toBe ( 0 ) ;
467+ } ) ;
468+ } ) ;
277469 } ) ;
278470} ) ;
0 commit comments