Skip to content

Commit 26cba08

Browse files
crisbetommalerba
authored andcommitted
refactor(compiler-cli): pass in tag name to schema checker (angular#60977)
Updates the `DomSchemaChecker` to require the tag name as a string, rather than the entire DOM node. This makes selectorless a bit easier to intergrate. PR Close angular#60977
1 parent e89ebf3 commit 26cba08

File tree

2 files changed

+24
-22
lines changed

2 files changed

+24
-22
lines changed

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

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import {
1010
DomElementSchemaRegistry,
1111
ParseSourceSpan,
1212
SchemaMetadata,
13-
TmplAstElement,
1413
TmplAstHostElement,
1514
} from '@angular/compiler';
1615
import ts from 'typescript';
@@ -43,16 +42,17 @@ export interface DomSchemaChecker {
4342
/**
4443
* Check a non-Angular element and record any diagnostics about it.
4544
*
46-
* @param id the template ID, suitable for resolution with a `TcbSourceResolver`.
47-
* @param element the element node in question.
48-
* @param schemas any active schemas for the template, which might affect the validity of the
45+
* @param id Template ID, suitable for resolution with a `TcbSourceResolver`.
46+
* @param tagName Tag name of the element in question
47+
* @param sourceSpanForDiagnostics Span that should be used when reporting diagnostics.
48+
* @param schemas Any active schemas for the template, which might affect the validity of the
4949
* element.
50-
* @param hostIsStandalone boolean indicating whether the element's host is a standalone
51-
* component.
50+
* @param hostIsStandalone Indicates whether the element's host is a standalone component.
5251
*/
5352
checkElement(
54-
id: string,
55-
element: TmplAstElement,
53+
id: TypeCheckId,
54+
tagName: string,
55+
sourceSpanForDiagnostics: ParseSourceSpan,
5656
schemas: SchemaMetadata[],
5757
hostIsStandalone: boolean,
5858
): void;
@@ -61,7 +61,7 @@ export interface DomSchemaChecker {
6161
* Check a property binding on an element and record any diagnostics about it.
6262
*
6363
* @param id the type check ID, suitable for resolution with a `TcbSourceResolver`.
64-
* @param element the element node in question.
64+
* @param tagName tag name of the element.
6565
* @param name the name of the property being checked.
6666
* @param span the source span of the binding. This is redundant with `element.attributes` but is
6767
* passed separately to avoid having to look up the particular property name.
@@ -70,7 +70,7 @@ export interface DomSchemaChecker {
7070
*/
7171
checkTemplateElementProperty(
7272
id: string,
73-
element: TmplAstElement,
73+
tagName: string,
7474
name: string,
7575
span: ParseSourceSpan,
7676
schemas: SchemaMetadata[],
@@ -110,14 +110,15 @@ export class RegistryDomSchemaChecker implements DomSchemaChecker {
110110

111111
checkElement(
112112
id: TypeCheckId,
113-
element: TmplAstElement,
113+
tagName: string,
114+
sourceSpanForDiagnostics: ParseSourceSpan,
114115
schemas: SchemaMetadata[],
115116
hostIsStandalone: boolean,
116117
): void {
117118
// HTML elements inside an SVG `foreignObject` are declared in the `xhtml` namespace.
118119
// We need to strip it before handing it over to the registry because all HTML tag names
119120
// in the registry are without a namespace.
120-
const name = element.name.replace(REMOVE_XHTML_REGEX, '');
121+
const name = tagName.replace(REMOVE_XHTML_REGEX, '');
121122

122123
if (!REGISTRY.hasElement(name, schemas)) {
123124
const mapping = this.resolver.getTemplateSourceMapping(id);
@@ -138,7 +139,7 @@ export class RegistryDomSchemaChecker implements DomSchemaChecker {
138139
const diag = makeTemplateDiagnostic(
139140
id,
140141
mapping,
141-
element.startSourceSpan,
142+
sourceSpanForDiagnostics,
142143
ts.DiagnosticCategory.Error,
143144
ngErrorCode(ErrorCode.SCHEMA_INVALID_ELEMENT),
144145
errorMsg,
@@ -149,32 +150,32 @@ export class RegistryDomSchemaChecker implements DomSchemaChecker {
149150

150151
checkTemplateElementProperty(
151152
id: TypeCheckId,
152-
element: TmplAstElement,
153+
tagName: string,
153154
name: string,
154155
span: ParseSourceSpan,
155156
schemas: SchemaMetadata[],
156157
hostIsStandalone: boolean,
157158
): void {
158-
if (!REGISTRY.hasProperty(element.name, name, schemas)) {
159+
if (!REGISTRY.hasProperty(tagName, name, schemas)) {
159160
const mapping = this.resolver.getTemplateSourceMapping(id);
160161

161162
const decorator = hostIsStandalone ? '@Component' : '@NgModule';
162163
const schemas = `'${decorator}.schemas'`;
163-
let errorMsg = `Can't bind to '${name}' since it isn't a known property of '${element.name}'.`;
164-
if (element.name.startsWith('ng-')) {
164+
let errorMsg = `Can't bind to '${name}' since it isn't a known property of '${tagName}'.`;
165+
if (tagName.startsWith('ng-')) {
165166
errorMsg +=
166167
`\n1. If '${name}' is an Angular directive, then add 'CommonModule' to the '${decorator}.imports' of this component.` +
167168
`\n2. To allow any property add 'NO_ERRORS_SCHEMA' to the ${schemas} of this component.`;
168-
} else if (element.name.indexOf('-') > -1) {
169+
} else if (tagName.indexOf('-') > -1) {
169170
errorMsg +=
170171
`\n1. If '${
171-
element.name
172+
tagName
172173
}' is an Angular component and it has '${name}' input, then verify that it is ${
173174
hostIsStandalone
174175
? "included in the '@Component.imports' of this component"
175176
: 'part of this module'
176177
}.` +
177-
`\n2. If '${element.name}' is a Web Component then add 'CUSTOM_ELEMENTS_SCHEMA' to the ${schemas} of this component to suppress this message.` +
178+
`\n2. If '${tagName}' is a Web Component then add 'CUSTOM_ELEMENTS_SCHEMA' to the ${schemas} of this component to suppress this message.` +
178179
`\n3. To allow any property add 'NO_ERRORS_SCHEMA' to the ${schemas} of this component.`;
179180
}
180181

packages/compiler-cli/src/ngtsc/typecheck/src/type_check_block.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1152,7 +1152,8 @@ class TcbDomSchemaCheckerOp extends TcbOp {
11521152
if (this.checkElement && isTemplateElement) {
11531153
this.tcb.domSchemaChecker.checkElement(
11541154
this.tcb.id,
1155-
element,
1155+
element.name,
1156+
element.startSourceSpan,
11561157
this.tcb.schemas,
11571158
this.tcb.hostIsStandalone,
11581159
);
@@ -1175,7 +1176,7 @@ class TcbDomSchemaCheckerOp extends TcbOp {
11751176
if (isTemplateElement) {
11761177
this.tcb.domSchemaChecker.checkTemplateElementProperty(
11771178
this.tcb.id,
1178-
element,
1179+
element.name,
11791180
propertyName,
11801181
binding.sourceSpan,
11811182
this.tcb.schemas,

0 commit comments

Comments
 (0)