Skip to content

Commit d1c1bc3

Browse files
crisbetopkozlowski-opensource
authored andcommitted
refactor(compiler-cli): decouple schema checker from typescript
Makes the `DomSchemaChecker` from TypeScript APIs.
1 parent 682aaf9 commit d1c1bc3

9 files changed

Lines changed: 120 additions & 99 deletions

File tree

packages/compiler-cli/private/hybrid_analysis.ts

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,20 +8,21 @@
88

99
// TCB generation exports for ng-hybrid-preprocessor
1010
export {generateTypeCheckBlock} from '../src/ngtsc/typecheck/src/type_check_block';
11-
export type {
12-
TypeCheckingConfig,
13-
TcbComponentMetadata,
14-
TcbTypeCheckBlockMetadata,
15-
TcbTypeParameter,
16-
TypeCheckId,
17-
TcbDirectiveMetadata,
18-
TemplateDiagnostic,
19-
TcbReferenceMetadata,
20-
SourceMapping,
21-
OutOfBandDiagnosticRecorder,
11+
export {
12+
type TypeCheckingConfig,
13+
type TcbComponentMetadata,
14+
type TcbTypeCheckBlockMetadata,
15+
type TcbTypeParameter,
16+
type TypeCheckId,
17+
type TcbDirectiveMetadata,
18+
type TemplateDiagnostic,
19+
type TcbReferenceMetadata,
20+
type SourceMapping,
21+
type OutOfBandDiagnosticRecorder,
22+
type DomSchemaChecker,
2223
OutOfBadDiagnosticCategory,
2324
} from '../src/ngtsc/typecheck/api';
24-
export {DomSchemaChecker, RegistryDomSchemaChecker} from '../src/ngtsc/typecheck/src/dom';
25+
export {RegistryDomSchemaChecker} from '../src/ngtsc/typecheck/src/dom';
2526
export {Environment} from '../src/ngtsc/typecheck/src/environment';
2627
export {TcbGenericContextBehavior} from '../src/ngtsc/typecheck/src/ops/context';
2728
export {ImportManager} from '../src/ngtsc/translator';

packages/compiler-cli/src/ngtsc/typecheck/api/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,4 @@ export * from './context';
1313
export * from './scope';
1414
export * from './symbols';
1515
export * from './oob';
16+
export * from './schema';
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
/*!
2+
* @license
3+
* Copyright Google LLC All Rights Reserved.
4+
*
5+
* Use of this source code is governed by an MIT-style license that can be
6+
* found in the LICENSE file at https://angular.dev/license
7+
*/
8+
9+
import {ParseSourceSpan, SchemaMetadata, TmplAstHostElement} from '@angular/compiler';
10+
import {TypeCheckId} from './api';
11+
12+
/**
13+
* Checks every non-Angular element/property processed in a template and potentially produces
14+
* diagnostics related to improper usage.
15+
*
16+
* A `DomSchemaChecker`'s job is to check DOM nodes and their attributes written used in templates
17+
* and produce diagnostics if the nodes don't conform to the DOM specification. It acts as a
18+
* collector for these diagnostics, and can be queried later to retrieve the list of any that have
19+
* been generated.
20+
*/
21+
export interface DomSchemaChecker<T> {
22+
/**
23+
* Get the diagnostics that have been generated via `checkElement` and `checkProperty` calls
24+
* thus far.
25+
*/
26+
readonly diagnostics: ReadonlyArray<T>;
27+
28+
/**
29+
* Check a non-Angular element and record any diagnostics about it.
30+
*
31+
* @param id Template ID, suitable for resolution with a `TcbSourceResolver`.
32+
* @param tagName Tag name of the element in question
33+
* @param sourceSpanForDiagnostics Span that should be used when reporting diagnostics.
34+
* @param schemas Any active schemas for the template, which might affect the validity of the
35+
* element.
36+
* @param hostIsStandalone Indicates whether the element's host is a standalone component.
37+
*/
38+
checkElement(
39+
id: TypeCheckId,
40+
tagName: string,
41+
sourceSpanForDiagnostics: ParseSourceSpan,
42+
schemas: SchemaMetadata[],
43+
hostIsStandalone: boolean,
44+
): void;
45+
46+
/**
47+
* Check a property binding on an element and record any diagnostics about it.
48+
*
49+
* @param id the type check ID, suitable for resolution with a `TcbSourceResolver`.
50+
* @param tagName tag name of the element.
51+
* @param name the name of the property being checked.
52+
* @param span the source span of the binding. This is redundant with `element.attributes` but is
53+
* passed separately to avoid having to look up the particular property name.
54+
* @param schemas any active schemas for the template, which might affect the validity of the
55+
* property.
56+
*/
57+
checkTemplateElementProperty(
58+
id: string,
59+
tagName: string,
60+
name: string,
61+
span: ParseSourceSpan,
62+
schemas: SchemaMetadata[],
63+
hostIsStandalone: boolean,
64+
): void;
65+
66+
/**
67+
* Check a property binding on a host element and record any diagnostics about it.
68+
* @param id the type check ID, suitable for resolution with a `TcbSourceResolver`.
69+
* @param element the element node in question.
70+
* @param name the name of the property being checked.
71+
* @param span the source span of the binding.
72+
* @param schemas any active schemas for the template, which might affect the validity of the
73+
* property.
74+
*/
75+
checkHostElementProperty(
76+
id: string,
77+
element: TmplAstHostElement,
78+
name: string,
79+
span: ParseSourceSpan,
80+
schemas: SchemaMetadata[],
81+
): void;
82+
}

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,12 @@ import {
3737
TypeCtorMetadata,
3838
TemplateContext,
3939
OutOfBandDiagnosticRecorder,
40+
DomSchemaChecker,
4041
} from '../api';
4142
import {makeTemplateDiagnostic} from '../diagnostics';
4243

4344
import {adaptTypeCheckBlockMetadata} from './tcb_adapter';
44-
import {DomSchemaChecker, RegistryDomSchemaChecker} from './dom';
45+
import {RegistryDomSchemaChecker} from './dom';
4546
import {Environment} from './environment';
4647
import {OutOfBandDiagnosticRecorderImpl} from './oob';
4748
import {ReferenceEmitEnvironment} from './reference_emit_environment';
@@ -134,7 +135,7 @@ export interface PendingShimData {
134135
/**
135136
* The `DomSchemaChecker` in use for this template, which records any schema-related diagnostics.
136137
*/
137-
domSchemaChecker: DomSchemaChecker;
138+
domSchemaChecker: DomSchemaChecker<TemplateDiagnostic>;
138139

139140
/**
140141
* Shim file in the process of being generated.
@@ -678,7 +679,7 @@ class InlineTcbOp implements Op {
678679
readonly meta: TypeCheckBlockMetadata,
679680
readonly config: TypeCheckingConfig,
680681
readonly reflector: ReflectionHost,
681-
readonly domSchemaChecker: DomSchemaChecker,
682+
readonly domSchemaChecker: DomSchemaChecker<unknown>,
682683
readonly oobRecorder: OutOfBandDiagnosticRecorder<unknown>,
683684
) {}
684685

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

Lines changed: 2 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -15,91 +15,19 @@ import {
1515
import ts from 'typescript';
1616

1717
import {ErrorCode, ngErrorCode} from '../../diagnostics';
18-
import {TemplateDiagnostic, TypeCheckId} from '../api';
18+
import {DomSchemaChecker, TemplateDiagnostic, TypeCheckId} from '../api';
1919
import {makeTemplateDiagnostic} from '../diagnostics';
2020

2121
import {TypeCheckSourceResolver} from './tcb_util';
2222

2323
export const REGISTRY = new DomElementSchemaRegistry();
2424
const REMOVE_XHTML_REGEX = /^:xhtml:/;
2525

26-
/**
27-
* Checks every non-Angular element/property processed in a template and potentially produces
28-
* `ts.Diagnostic`s related to improper usage.
29-
*
30-
* A `DomSchemaChecker`'s job is to check DOM nodes and their attributes written used in templates
31-
* and produce `ts.Diagnostic`s if the nodes don't conform to the DOM specification. It acts as a
32-
* collector for these diagnostics, and can be queried later to retrieve the list of any that have
33-
* been generated.
34-
*/
35-
export interface DomSchemaChecker {
36-
/**
37-
* Get the `ts.Diagnostic`s that have been generated via `checkElement` and `checkProperty` calls
38-
* thus far.
39-
*/
40-
readonly diagnostics: ReadonlyArray<TemplateDiagnostic>;
41-
42-
/**
43-
* Check a non-Angular element and record any diagnostics about it.
44-
*
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
49-
* element.
50-
* @param hostIsStandalone Indicates whether the element's host is a standalone component.
51-
*/
52-
checkElement(
53-
id: TypeCheckId,
54-
tagName: string,
55-
sourceSpanForDiagnostics: ParseSourceSpan,
56-
schemas: SchemaMetadata[],
57-
hostIsStandalone: boolean,
58-
): void;
59-
60-
/**
61-
* Check a property binding on an element and record any diagnostics about it.
62-
*
63-
* @param id the type check ID, suitable for resolution with a `TcbSourceResolver`.
64-
* @param tagName tag name of the element.
65-
* @param name the name of the property being checked.
66-
* @param span the source span of the binding. This is redundant with `element.attributes` but is
67-
* passed separately to avoid having to look up the particular property name.
68-
* @param schemas any active schemas for the template, which might affect the validity of the
69-
* property.
70-
*/
71-
checkTemplateElementProperty(
72-
id: string,
73-
tagName: string,
74-
name: string,
75-
span: ParseSourceSpan,
76-
schemas: SchemaMetadata[],
77-
hostIsStandalone: boolean,
78-
): void;
79-
80-
/**
81-
* Check a property binding on a host element and record any diagnostics about it.
82-
* @param id the type check ID, suitable for resolution with a `TcbSourceResolver`.
83-
* @param element the element node in question.
84-
* @param name the name of the property being checked.
85-
* @param span the source span of the binding.
86-
* @param schemas any active schemas for the template, which might affect the validity of the
87-
* property.
88-
*/
89-
checkHostElementProperty(
90-
id: string,
91-
element: TmplAstHostElement,
92-
name: string,
93-
span: ParseSourceSpan,
94-
schemas: SchemaMetadata[],
95-
): void;
96-
}
97-
9826
/**
9927
* Checks non-Angular elements and properties against the `DomElementSchemaRegistry`, a schema
10028
* maintained by the Angular team via extraction from a browser IDL.
10129
*/
102-
export class RegistryDomSchemaChecker implements DomSchemaChecker {
30+
export class RegistryDomSchemaChecker implements DomSchemaChecker<TemplateDiagnostic> {
10331
private _diagnostics: TemplateDiagnostic[] = [];
10432

10533
get diagnostics(): ReadonlyArray<TemplateDiagnostic> {

packages/compiler-cli/src/ngtsc/typecheck/src/ops/context.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,12 @@
77
*/
88

99
import {BoundTarget, SchemaMetadata} from '@angular/compiler';
10-
import {DomSchemaChecker} from '../dom';
1110
import {
1211
TypeCheckId,
1312
TcbDirectiveMetadata,
1413
TcbPipeMetadata,
1514
OutOfBandDiagnosticRecorder,
15+
DomSchemaChecker,
1616
} from '../../api';
1717
import {Environment} from '../environment';
1818

@@ -56,7 +56,7 @@ export class Context {
5656

5757
constructor(
5858
readonly env: Environment,
59-
readonly domSchemaChecker: DomSchemaChecker,
59+
readonly domSchemaChecker: DomSchemaChecker<unknown>,
6060
readonly oobRecorder: OutOfBandDiagnosticRecorder<unknown>,
6161
readonly id: TypeCheckId,
6262
readonly boundTarget: BoundTarget<TcbDirectiveMetadata>,

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

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,12 @@
66
* found in the LICENSE file at https://angular.dev/license
77
*/
88

9-
import {OutOfBandDiagnosticRecorder, TcbComponentMetadata, TcbTypeCheckBlockMetadata} from '../api';
10-
import {DomSchemaChecker} from './dom';
9+
import {
10+
DomSchemaChecker,
11+
OutOfBandDiagnosticRecorder,
12+
TcbComponentMetadata,
13+
TcbTypeCheckBlockMetadata,
14+
} from '../api';
1115
import {Environment} from './environment';
1216
import {createHostBindingsBlockGuard} from './host_bindings';
1317
import {Context} from './ops/context';
@@ -43,7 +47,7 @@ export function generateTypeCheckBlock(
4347
component: TcbComponentMetadata,
4448
name: string,
4549
meta: TcbTypeCheckBlockMetadata,
46-
domSchemaChecker: DomSchemaChecker,
50+
domSchemaChecker: DomSchemaChecker<unknown>,
4751
oobRecorder: OutOfBandDiagnosticRecorder<unknown>,
4852
): string {
4953
const tcb = new Context(

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

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,13 @@ import {AbsoluteFsPath} from '../../file_system';
1111
import {Reference, ReferenceEmitter} from '../../imports';
1212
import {ClassDeclaration, ReflectionHost} from '../../reflection';
1313
import {ImportManager} from '../../translator';
14-
import {OutOfBandDiagnosticRecorder, TypeCheckBlockMetadata, TypeCheckingConfig} from '../api';
14+
import {
15+
DomSchemaChecker,
16+
OutOfBandDiagnosticRecorder,
17+
TypeCheckBlockMetadata,
18+
TypeCheckingConfig,
19+
} from '../api';
1520

16-
import {DomSchemaChecker} from './dom';
1721
import {Environment} from './environment';
1822
import {ensureTypeCheckFilePreparationImports} from './tcb_util';
1923
import {generateTypeCheckBlock} from './type_check_block';
@@ -64,7 +68,7 @@ export class TypeCheckFile extends Environment {
6468
addTypeCheckBlock(
6569
ref: Reference<ClassDeclaration<ts.ClassDeclaration>>,
6670
meta: TypeCheckBlockMetadata,
67-
domSchemaChecker: DomSchemaChecker,
71+
domSchemaChecker: DomSchemaChecker<unknown>,
6872
oobRecorder: OutOfBandDiagnosticRecorder<unknown>,
6973
genericContextBehavior: TcbGenericContextBehavior,
7074
): void {

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,12 +99,12 @@ import {
9999
TypeCheckingConfig,
100100
} from '../api/api';
101101
import {TemplateTypeCheckerImpl} from '../src/checker';
102-
import {DomSchemaChecker} from '../src/dom';
103102
import {TypeCheckShimGenerator} from '../src/shim';
104103
import {TypeCheckFile} from '../src/type_check_file';
105104
import {sfExtensionData} from '../../shims';
106105
import {freshCompilationTicket, NgCompiler, NgCompilerHost} from '../../core';
107106
import {TcbGenericContextBehavior} from '../src/ops/context';
107+
import {DomSchemaChecker} from '../api/schema';
108108

109109
export function typescriptLibDts(): TestFile {
110110
return {
@@ -1027,7 +1027,7 @@ function parseInputOutputMappingArray(values: string[]) {
10271027
);
10281028
}
10291029

1030-
export class NoopSchemaChecker implements DomSchemaChecker {
1030+
export class NoopSchemaChecker implements DomSchemaChecker<TemplateDiagnostic> {
10311031
get diagnostics(): ReadonlyArray<TemplateDiagnostic> {
10321032
return [];
10331033
}

0 commit comments

Comments
 (0)