Skip to content

Commit e4f6744

Browse files
aparziJeanMeche
authored andcommitted
feat(compiler): report unknown property bindings on explicit ng-template elements
Previously, property bindings on `<ng-template>` that were not claimed by a matched directive were silently ignored by the type checker. This was inconsistent with how bindings on regular elements are handled. This check is added behind a temporary compiler flag `checkTypeOfNgTemplateBindings` which will be removed in the next breaking change window. When the flag is enabled, the type checker will report an error for any property binding on an `<ng-template>` that is not matched by a directive. Fixes #69322 Co-authored-by: Matthieu Riegler <[email protected]>
1 parent effbe56 commit e4f6744

18 files changed

Lines changed: 498 additions & 20 deletions

File tree

‎goldens/public-api/compiler-cli/compiler_options.api.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ export interface TypeCheckingOptions {
7777
strictInputAccessModifiers?: boolean;
7878
strictInputTypes?: boolean;
7979
strictLiteralTypes?: boolean;
80+
strictNgTemplateTypes?: boolean;
8081
strictNullInputTypes?: boolean;
8182
strictOutputEventTypes?: boolean;
8283
strictSafeNavigationTypes?: boolean;

‎packages/compiler-cli/preprocessor/src/compiler_options.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@ export interface NgpCompilerOptions {
103103
strictLiteralTypes?: boolean;
104104
strictUnclaimedEventNames?: boolean;
105105
strictUnknownElements?: boolean;
106+
strictNgTemplateTypes?: boolean;
106107
typeCheckHostBindings?: boolean;
107108

108109
// Diagnostics (`DiagnosticOptions`).

‎packages/compiler-cli/preprocessor/src/tcb.ts‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,7 @@ export function buildTypeCheckingConfig(
226226
allowDomEventAssertion: true,
227227
checkUnclaimedEventNames: false,
228228
checkUnknownElements: false,
229+
checkTypeOfNgTemplateBindings: false,
229230
};
230231
} else {
231232
typeCheckingConfig = {
@@ -264,6 +265,7 @@ export function buildTypeCheckingConfig(
264265
allowDomEventAssertion: true,
265266
checkUnclaimedEventNames: false,
266267
checkUnknownElements: false,
268+
checkTypeOfNgTemplateBindings: false,
267269
};
268270
}
269271

@@ -305,6 +307,9 @@ export function buildTypeCheckingConfig(
305307
if (options.strictUnknownElements !== undefined) {
306308
typeCheckingConfig.checkUnknownElements = options.strictUnknownElements;
307309
}
310+
if (options.strictNgTemplateTypes !== undefined) {
311+
typeCheckingConfig.checkTypeOfNgTemplateBindings = options.strictNgTemplateTypes;
312+
}
308313
typeCheckingConfig.typeCheckHostBindings = options.typeCheckHostBindings ?? true;
309314
typeCheckingConfig.usePoisonedData =
310315
usePoisonedData || Boolean(options._compilePoisonedComponents);

‎packages/compiler-cli/src/ngtsc/core/api/src/public_options.ts‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,15 @@ export interface TypeCheckingOptions {
235235
* Defaults to `false`.
236236
*/
237237
strictUnknownElements?: boolean;
238+
239+
/**
240+
* Whether to check if unknown properties are bound to `<ng-template>`.
241+
*
242+
* If this is `true`, an error will be reported for unknown properties bound to an `<ng-template>`.
243+
*
244+
* Defaults to `false`.
245+
*/
246+
strictNgTemplateTypes?: boolean;
238247
}
239248

240249
/**

‎packages/compiler-cli/src/ngtsc/core/src/compiler.ts‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1131,6 +1131,8 @@ export class NgCompiler {
11311131
this.options.extendedDiagnostics?.defaultCategory || DiagnosticCategoryLabel.Warning,
11321132
allowSignalsInTwoWayBindings,
11331133
allowDomEventAssertion,
1134+
checkTypeOfNgTemplateBindings: false, // 3p-only
1135+
// g3-only checkTypeOfNgTemplateBindings: true,
11341136
};
11351137
} else {
11361138
typeCheckingConfig = {
@@ -1164,6 +1166,8 @@ export class NgCompiler {
11641166
allowSignalsInTwoWayBindings,
11651167
allowDomEventAssertion,
11661168
checkUnknownElements: false,
1169+
checkTypeOfNgTemplateBindings: false, // 3p-only
1170+
// g3-only checkTypeOfNgTemplateBindings: true,
11671171
};
11681172
}
11691173

@@ -1205,6 +1209,9 @@ export class NgCompiler {
12051209
if (this.options.strictLiteralTypes !== undefined) {
12061210
typeCheckingConfig.strictLiteralTypes = this.options.strictLiteralTypes;
12071211
}
1212+
if (this.options.strictNgTemplateTypes !== undefined) {
1213+
typeCheckingConfig.checkTypeOfNgTemplateBindings = this.options.strictNgTemplateTypes;
1214+
}
12081215
if (
12091216
this.options.extendedDiagnostics?.checks?.controlFlowPreventingContentProjection !== undefined
12101217
) {

‎packages/compiler-cli/src/ngtsc/typecheck/src/dom.ts‎

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -115,21 +115,24 @@ export class RegistryDomSchemaChecker implements DomSchemaChecker<TemplateDiagno
115115

116116
const decorator = hostIsStandalone ? '@Component' : '@NgModule';
117117
const schemas = `'${decorator}.schemas'`;
118-
let errorMsg = `Can't bind to '${name}' since it isn't a known property of '${tagName}'.`;
119-
if (tagName.startsWith('ng-')) {
118+
const localTagName = tagName.startsWith(':')
119+
? tagName.slice(tagName.indexOf(':', 1) + 1)
120+
: tagName;
121+
let errorMsg = `Can't bind to '${name}' since it isn't a known property of '${localTagName}'.`;
122+
if (localTagName.startsWith('ng-')) {
120123
errorMsg +=
121124
`\n1. If '${name}' is an Angular directive, then add 'CommonModule' to the '${decorator}.imports' of this component.` +
122125
`\n2. To allow any property add 'NO_ERRORS_SCHEMA' to the ${schemas} of this component.`;
123-
} else if (tagName.indexOf('-') > -1) {
126+
} else if (localTagName.indexOf('-') > -1) {
124127
errorMsg +=
125128
`\n1. If '${
126-
tagName
129+
localTagName
127130
}' is an Angular component and it has '${name}' input, then verify that it is ${
128131
hostIsStandalone
129132
? "included in the '@Component.imports' of this component"
130133
: 'part of this module'
131134
}.` +
132-
`\n2. If '${tagName}' is a Web Component then add 'CUSTOM_ELEMENTS_SCHEMA' to the ${schemas} of this component to suppress this message.` +
135+
`\n2. If '${localTagName}' is a Web Component then add 'CUSTOM_ELEMENTS_SCHEMA' to the ${schemas} of this component to suppress this message.` +
133136
`\n3. To allow any property add 'NO_ERRORS_SCHEMA' to the ${schemas} of this component.`;
134137
}
135138

‎packages/compiler-cli/src/ngtsc/typecheck/testing/index.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,7 @@ export function tcb(
197197
allowSignalsInTwoWayBindings: true,
198198
allowDomEventAssertion: true,
199199
checkUnknownElements: true,
200+
checkTypeOfNgTemplateBindings: true,
200201
...config,
201202
};
202203
options = options || {emitSpans: false};

‎packages/compiler-cli/src/ngtsc/typecheck/testing/src/test_support.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,7 @@ export const ALL_ENABLED_CONFIG: Readonly<TypeCheckingConfig> = {
223223
allowSignalsInTwoWayBindings: true,
224224
allowDomEventAssertion: true,
225225
checkUnknownElements: true,
226+
checkTypeOfNgTemplateBindings: true,
226227
};
227228

228229
// Remove 'ref' from TypeCheckableDirectiveMeta and add a 'selector' instead.

‎packages/compiler-cli/test/ngtsc/template_mapping_spec.ts‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -800,13 +800,15 @@ runInEachFileSystem((os) => {
800800
import {Component, Directive, Input, Output, EventEmitter, Pipe, NgModule} from '@angular/core';
801801
802802
@Directive({
803-
selector: '[ngModel],[attr],[ngModelChange]',
803+
selector: '[ngModel],[attr],[ngModelChange],[ngIf],[ngForOf]',
804804
standalone: false,
805805
})
806806
export class AllDirective {
807807
@Input() ngModel!: any;
808808
@Output() ngModelChange = new EventEmitter<any>();
809809
@Input() attr!: any;
810+
@Input() ngIf!: any;
811+
@Input() ngForOf!: any;
810812
}
811813
812814
@Pipe({
@@ -826,6 +828,7 @@ runInEachFileSystem((os) => {
826828
name = '';
827829
isInitial = false;
828830
doSomething() {}
831+
showMessage() { return true; }
829832
items: any[] = [];
830833
greeting = '';
831834
}

0 commit comments

Comments
 (0)