Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit ff858ac

Browse filesBrowse files
danieltre23alxhub
authored andcommitted
refactor(compiler-cli): add extendedTemplateCheck phase to compiler (#43107)
This commit integrates extended template checks with the compiler, by adding another phase of diagnostics generation. This integration is under the `_extendedTemplateDiagnostics` flag. Refs #42966 PR Close #43107
1 parent cb654d1 commit ff858ac
Copy full SHA for ff858ac

File tree

Expand file treeCollapse file tree

9 files changed

+99
-9
lines changed
Filter options
Expand file treeCollapse file tree

9 files changed

+99
-9
lines changed

‎packages/compiler-cli/src/ngtsc/annotations/BUILD.bazel

Copy file name to clipboardExpand all lines: packages/compiler-cli/src/ngtsc/annotations/BUILD.bazel
+1Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ ts_library(
2626
"//packages/compiler-cli/src/ngtsc/transform",
2727
"//packages/compiler-cli/src/ngtsc/typecheck/api",
2828
"//packages/compiler-cli/src/ngtsc/typecheck/diagnostics",
29+
"//packages/compiler-cli/src/ngtsc/typecheck/extended/api",
2930
"//packages/compiler-cli/src/ngtsc/util",
3031
"//packages/compiler-cli/src/ngtsc/xi18n",
3132
"@npm//@types/node",

‎packages/compiler-cli/src/ngtsc/annotations/src/component.ts

Copy file name to clipboardExpand all lines: packages/compiler-cli/src/ngtsc/annotations/src/component.ts
+7Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import {ClassDeclaration, DeclarationNode, Decorator, ReflectionHost, reflectObj
2424
import {ComponentScopeReader, LocalModuleScopeRegistry, TypeCheckScopeRegistry} from '../../scope';
2525
import {AnalysisOutput, CompileResult, DecoratorHandler, DetectResult, HandlerFlags, HandlerPrecedence, ResolveResult} from '../../transform';
2626
import {TemplateSourceMapping, TypeCheckContext} from '../../typecheck/api';
27+
import {ExtendedTemplateChecker} from '../../typecheck/extended/api';
2728
import {SubsetOfKeys} from '../../util/src/typescript';
2829
import {Xi18nContext} from '../../xi18n';
2930

@@ -613,6 +614,12 @@ export class ComponentDecoratorHandler implements
613614
meta.template.sourceMapping, meta.template.file, meta.template.errors);
614615
}
615616

617+
extendedTemplateCheck(
618+
component: ts.ClassDeclaration,
619+
extendedTemplateChecker: ExtendedTemplateChecker): ts.Diagnostic[] {
620+
return extendedTemplateChecker.getExtendedTemplateDiagnosticsForComponent(component);
621+
}
622+
616623
resolve(
617624
node: ClassDeclaration, analysis: Readonly<ComponentAnalysisData>,
618625
symbol: ComponentSymbol): ResolveResult<ComponentResolutionData> {

‎packages/compiler-cli/src/ngtsc/core/BUILD.bazel

Copy file name to clipboardExpand all lines: packages/compiler-cli/src/ngtsc/core/BUILD.bazel
+2Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ ts_library(
3737
"//packages/compiler-cli/src/ngtsc/typecheck",
3838
"//packages/compiler-cli/src/ngtsc/typecheck/api",
3939
"//packages/compiler-cli/src/ngtsc/typecheck/diagnostics",
40+
"//packages/compiler-cli/src/ngtsc/typecheck/extended",
41+
"//packages/compiler-cli/src/ngtsc/typecheck/extended/src/template_checks/invalid_banana_in_box",
4042
"//packages/compiler-cli/src/ngtsc/util",
4143
"//packages/compiler-cli/src/ngtsc/xi18n",
4244
"@npm//typescript",

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

Copy file name to clipboardExpand all lines: packages/compiler-cli/src/ngtsc/core/api/src/options.ts
+11-1Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,16 @@ export interface TestOnlyOptions {
3636
tracePerformance?: string;
3737
}
3838

39+
/**
40+
* Internal only options for compiler.
41+
*/
42+
export interface InternalOptions {
43+
/**
44+
* Whether to run all template checks and generate extended template diagnostics.
45+
*/
46+
_extendedTemplateDiagnostics?: boolean;
47+
}
48+
3949
/**
4050
* A merged interface of all of the various Angular compiler options, as well as the standard
4151
* `ts.CompilerOptions`.
@@ -45,4 +55,4 @@ export interface TestOnlyOptions {
4555
export interface NgCompilerOptions extends ts.CompilerOptions, LegacyNgcOptions, BazelAndG3Options,
4656
NgcCompatibilityOptions, StrictTemplateOptions,
4757
TestOnlyOptions, I18nOptions, TargetOptions,
48-
MiscOptions {}
58+
InternalOptions, MiscOptions {}

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

Copy file name to clipboardExpand all lines: packages/compiler-cli/src/ngtsc/core/src/compiler.ts
+46-6Lines changed: 46 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ import {ivySwitchTransform} from '../../switch';
3030
import {aliasTransformFactory, CompilationMode, declarationTransformFactory, DecoratorHandler, DtsTransformRegistry, ivyTransformFactory, TraitCompiler} from '../../transform';
3131
import {TemplateTypeCheckerImpl} from '../../typecheck';
3232
import {OptimizeFor, TemplateTypeChecker, TypeCheckingConfig} from '../../typecheck/api';
33+
import {ExtendedTemplateCheckerImpl} from '../../typecheck/extended';
34+
import {InvalidBananaInBoxCheck} from '../../typecheck/extended/src/template_checks/invalid_banana_in_box';
3335
import {getSourceFileOrNull, isDtsPath, resolveModuleName, toUnredirectedSourceFile} from '../../util/src/typescript';
3436
import {Xi18nContext} from '../../xi18n';
3537
import {LazyRoute, NgCompilerAdapter, NgCompilerOptions} from '../api';
@@ -316,6 +318,12 @@ export class NgCompiler {
316318
readonly usePoisonedData: boolean,
317319
private livePerfRecorder: ActivePerfRecorder,
318320
) {
321+
if (this.options._extendedTemplateDiagnostics === true &&
322+
this.options.strictTemplates === false) {
323+
throw new Error(
324+
'The \'_extendedTemplateDiagnostics\' option requires \'strictTemplates\' to also be enabled.');
325+
}
326+
319327
this.constructionDiagnostics.push(...this.adapter.constructionDiagnostics);
320328
const incompatibleTypeCheckOptionsDiagnostic = verifyCompatibleTypeCheckOptions(this.options);
321329
if (incompatibleTypeCheckOptionsDiagnostic !== null) {
@@ -425,8 +433,12 @@ export class NgCompiler {
425433
* Get all Angular-related diagnostics for this compilation.
426434
*/
427435
getDiagnostics(): ts.Diagnostic[] {
428-
return this.addMessageTextDetails(
429-
[...this.getNonTemplateDiagnostics(), ...this.getTemplateDiagnostics()]);
436+
const diagnostics: ts.Diagnostic[] = [];
437+
diagnostics.push(...this.getNonTemplateDiagnostics(), ...this.getTemplateDiagnostics());
438+
if (this.options._extendedTemplateDiagnostics) {
439+
diagnostics.push(...this.getExtendedTemplateDiagnostics());
440+
}
441+
return this.addMessageTextDetails(diagnostics);
430442
}
431443

432444
/**
@@ -435,10 +447,14 @@ export class NgCompiler {
435447
* If a `ts.SourceFile` is passed, only diagnostics related to that file are returned.
436448
*/
437449
getDiagnosticsForFile(file: ts.SourceFile, optimizeFor: OptimizeFor): ts.Diagnostic[] {
438-
return this.addMessageTextDetails([
439-
...this.getNonTemplateDiagnostics().filter(diag => diag.file === file),
440-
...this.getTemplateDiagnosticsForFile(file, optimizeFor)
441-
]);
450+
const diagnostics: ts.Diagnostic[] = [];
451+
diagnostics.push(
452+
...this.getNonTemplateDiagnostics().filter(diag => diag.file === file),
453+
...this.getTemplateDiagnosticsForFile(file, optimizeFor));
454+
if (this.options._extendedTemplateDiagnostics) {
455+
diagnostics.push(...this.getExtendedTemplateDiagnostics(file));
456+
}
457+
return this.addMessageTextDetails(diagnostics);
442458
}
443459

444460
/**
@@ -892,6 +908,30 @@ export class NgCompiler {
892908
return this.nonTemplateDiagnostics;
893909
}
894910

911+
/**
912+
* Calls the `extendedTemplateCheck` phase of the trait compiler
913+
* @param sf optional parameter to get diagnostics for a certain file
914+
* or all files in the program if `sf` is undefined
915+
* @returns generated extended template diagnostics
916+
*/
917+
private getExtendedTemplateDiagnostics(sf?: ts.SourceFile): ts.Diagnostic[] {
918+
const diagnostics: ts.Diagnostic[] = [];
919+
const compilation = this.ensureAnalyzed();
920+
const typeChecker = this.inputProgram.getTypeChecker();
921+
const templateChecks = [new InvalidBananaInBoxCheck()];
922+
const extendedTemplateChecker = new ExtendedTemplateCheckerImpl(
923+
compilation.templateTypeChecker, typeChecker, templateChecks);
924+
if (sf !== undefined) {
925+
return compilation.traitCompiler.extendedTemplateCheck(sf, extendedTemplateChecker);
926+
}
927+
for (const sf of this.inputProgram.getSourceFiles()) {
928+
diagnostics.push(
929+
...compilation.traitCompiler.extendedTemplateCheck(sf, extendedTemplateChecker));
930+
}
931+
932+
return diagnostics;
933+
}
934+
895935
private makeCompilation(): LazyCompilationState {
896936
const checker = this.inputProgram.getTypeChecker();
897937

‎packages/compiler-cli/src/ngtsc/transform/BUILD.bazel

Copy file name to clipboardExpand all lines: packages/compiler-cli/src/ngtsc/transform/BUILD.bazel
+1Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ ts_library(
1919
"//packages/compiler-cli/src/ngtsc/reflection",
2020
"//packages/compiler-cli/src/ngtsc/translator",
2121
"//packages/compiler-cli/src/ngtsc/typecheck/api",
22+
"//packages/compiler-cli/src/ngtsc/typecheck/extended/api",
2223
"//packages/compiler-cli/src/ngtsc/util",
2324
"//packages/compiler-cli/src/ngtsc/xi18n",
2425
"@npm//typescript",

‎packages/compiler-cli/src/ngtsc/transform/src/api.ts

Copy file name to clipboardExpand all lines: packages/compiler-cli/src/ngtsc/transform/src/api.ts
+5Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import {IndexingContext} from '../../indexer';
1515
import {ClassDeclaration, Decorator} from '../../reflection';
1616
import {ImportManager} from '../../translator';
1717
import {TypeCheckContext} from '../../typecheck/api';
18+
import {ExtendedTemplateChecker} from '../../typecheck/extended/api';
1819
import {Xi18nContext} from '../../xi18n';
1920

2021
/**
@@ -187,6 +188,10 @@ export interface DecoratorHandler<D, A, S extends SemanticSymbol|null, R> {
187188
(ctx: TypeCheckContext, node: ClassDeclaration, analysis: Readonly<A>,
188189
resolution: Readonly<R>): void;
189190

191+
extendedTemplateCheck?
192+
(component: ts.ClassDeclaration, extendedTemplateChecker: ExtendedTemplateChecker):
193+
ts.Diagnostic[];
194+
190195
/**
191196
* Generate a description of the field which should be added to the class, including any
192197
* initialization code to be generated.

‎packages/compiler-cli/src/ngtsc/transform/src/compilation.ts

Copy file name to clipboardExpand all lines: packages/compiler-cli/src/ngtsc/transform/src/compilation.ts
+25-1Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,9 @@ import {IncrementalBuild} from '../../incremental/api';
1414
import {SemanticDepGraphUpdater, SemanticSymbol} from '../../incremental/semantic_graph';
1515
import {IndexingContext} from '../../indexer';
1616
import {PerfEvent, PerfRecorder} from '../../perf';
17-
import {ClassDeclaration, DeclarationNode, Decorator, ReflectionHost} from '../../reflection';
17+
import {ClassDeclaration, DeclarationNode, Decorator, isNamedClassDeclaration, ReflectionHost} from '../../reflection';
1818
import {ProgramTypeCheckAdapter, TypeCheckContext} from '../../typecheck/api';
19+
import {ExtendedTemplateChecker} from '../../typecheck/extended/api';
1920
import {getSourceFile, isExported} from '../../util/src/typescript';
2021
import {Xi18nContext} from '../../xi18n';
2122

@@ -490,6 +491,29 @@ export class TraitCompiler implements ProgramTypeCheckAdapter {
490491
}
491492
}
492493

494+
extendedTemplateCheck(sf: ts.SourceFile, extendedTemplateChecker: ExtendedTemplateChecker):
495+
ts.Diagnostic[] {
496+
const classes = this.fileToClasses.get(sf);
497+
if (classes === undefined) {
498+
return [];
499+
}
500+
501+
const diagnostics: ts.Diagnostic[] = [];
502+
for (const clazz of classes) {
503+
if (!isNamedClassDeclaration(clazz)) {
504+
continue;
505+
}
506+
const record = this.classes.get(clazz)!;
507+
for (const trait of record.traits) {
508+
if (trait.handler.extendedTemplateCheck === undefined) {
509+
continue;
510+
}
511+
diagnostics.push(...trait.handler.extendedTemplateCheck(clazz, extendedTemplateChecker));
512+
}
513+
}
514+
return diagnostics;
515+
}
516+
493517
index(ctx: IndexingContext): void {
494518
for (const clazz of this.classes.keys()) {
495519
const record = this.classes.get(clazz)!;

‎packages/compiler-cli/src/ngtsc/typecheck/extended/src/template_checks/invalid_banana_in_box/BUILD.bazel

Copy file name to clipboardExpand all lines: packages/compiler-cli/src/ngtsc/typecheck/extended/src/template_checks/invalid_banana_in_box/BUILD.bazel
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ load("//tools:defaults.bzl", "ts_library")
33
ts_library(
44
name = "invalid_banana_in_box",
55
srcs = ["index.ts"],
6-
visibility = ["//packages/compiler-cli/src/ngtsc/typecheck/extended:__subpackages__"],
6+
visibility = ["//packages/compiler-cli/src/ngtsc:__subpackages__"],
77
deps = [
88
"//packages/compiler",
99
"//packages/compiler-cli/src/ngtsc/diagnostics",

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.