mirror of
https://github.com/angular/angular.git
synced 2026-09-14 13:54:52 +08:00
fix(compiler-cli): detect uninvoked functions in defer trigger expressions
Wrap `@defer` trigger expressions (`when`, `prefetch when`, `hydrate when`) in a conditional context within the TCB to enable TypeScript's TS2774 diagnostic for detecting functions used without invocation. Previously, signals and functions passed to `when` triggers without parentheses would silently evaluate to truthy, causing unexpected behavior. Now the compiler reports an error when a function is used as a condition without being called.
This commit is contained in:
committed by
Jessica Janiuk
parent
57edfaf435
commit
f90e5565e0
@@ -4957,6 +4957,66 @@ suppress
|
||||
]);
|
||||
});
|
||||
|
||||
it('should check that functions are invoked in `when` trigger', () => {
|
||||
env.write(
|
||||
'test.ts',
|
||||
`
|
||||
import {Component, signal} from '@angular/core';
|
||||
|
||||
@Component({
|
||||
template: \`@defer (when flag) {Hello}\`,
|
||||
})
|
||||
export class Main {
|
||||
flag = signal(false);
|
||||
}
|
||||
`,
|
||||
);
|
||||
|
||||
const diags = env.driveDiagnostics();
|
||||
expect(diags.length).toBe(1);
|
||||
expect(diags[0].messageText).toContain('always return true');
|
||||
});
|
||||
|
||||
it('should check that functions are invoked in `prefetch when` trigger', () => {
|
||||
env.write(
|
||||
'test.ts',
|
||||
`
|
||||
import {Component, signal} from '@angular/core';
|
||||
|
||||
@Component({
|
||||
template: \`@defer (prefetch when flag) {Hello}\`,
|
||||
})
|
||||
export class Main {
|
||||
flag = signal(false);
|
||||
}
|
||||
`,
|
||||
);
|
||||
|
||||
const diags = env.driveDiagnostics();
|
||||
expect(diags.length).toBe(1);
|
||||
expect(diags[0].messageText).toContain('always return true');
|
||||
});
|
||||
|
||||
it('should check that functions are invoked in `hydrate when` trigger', () => {
|
||||
env.write(
|
||||
'test.ts',
|
||||
`
|
||||
import {Component, signal} from '@angular/core';
|
||||
|
||||
@Component({
|
||||
template: \`@defer (hydrate when flag) {Hello}\`,
|
||||
})
|
||||
export class Main {
|
||||
flag = signal(false);
|
||||
}
|
||||
`,
|
||||
);
|
||||
|
||||
const diags = env.driveDiagnostics();
|
||||
expect(diags.length).toBe(1);
|
||||
expect(diags[0].messageText).toContain('always return true');
|
||||
});
|
||||
|
||||
it('should report if a deferred trigger reference does not exist', () => {
|
||||
env.write(
|
||||
'test.ts',
|
||||
|
||||
Reference in New Issue
Block a user