Skip to content

Commit 2e82357

Browse files
JoostKalxhub
authored andcommitted
refactor(ivy): verify template type check options are compatible (#34195)
It is now an error if '"fullTemplateTypeCheck"' is disabled while `"strictTemplates"` is enabled, as enabling the latter implies that the former is also enabled. PR Close #34195
1 parent 1de49ba commit 2e82357

File tree

4 files changed

+74
-0
lines changed

4 files changed

+74
-0
lines changed

packages/compiler-cli/src/ngtsc/diagnostics/src/code.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ export enum ErrorCode {
3939
SYMBOL_EXPORTED_UNDER_DIFFERENT_NAME = 3002,
4040

4141
CONFIG_FLAT_MODULE_NO_INDEX = 4001,
42+
CONFIG_STRICT_TEMPLATES_IMPLIES_FULL_TEMPLATE_TYPECHECK = 4002,
4243

4344
/**
4445
* Raised when a host expression has a parse error, such as a host listener or host binding

packages/compiler-cli/src/ngtsc/program.ts

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,11 @@ export class NgtscProgram implements api.Program {
8282
verifySupportedTypeScriptVersion();
8383
}
8484

85+
const incompatibleTypeCheckOptionsDiagnostic = verifyCompatibleTypeCheckOptions(options);
86+
if (incompatibleTypeCheckOptionsDiagnostic !== null) {
87+
this.constructionDiagnostics.push(incompatibleTypeCheckOptionsDiagnostic);
88+
}
89+
8590
if (shouldEnablePerfTracing(options)) {
8691
this.perfTracker = PerfTracker.zeroedToNow();
8792
this.perfRecorder = this.perfTracker;
@@ -829,6 +834,37 @@ function isAngularCorePackage(program: ts.Program): boolean {
829834
});
830835
}
831836

837+
/**
838+
* Since "strictTemplates" is a true superset of type checking capabilities compared to
839+
* "strictTemplateTypeCheck", it is required that the latter is not explicitly disabled if the
840+
* former is enabled.
841+
*/
842+
function verifyCompatibleTypeCheckOptions(options: api.CompilerOptions): ts.Diagnostic|null {
843+
if (options.fullTemplateTypeCheck === false && options.strictTemplates === true) {
844+
return {
845+
category: ts.DiagnosticCategory.Error,
846+
code: ngErrorCode(ErrorCode.CONFIG_STRICT_TEMPLATES_IMPLIES_FULL_TEMPLATE_TYPECHECK),
847+
file: undefined,
848+
start: undefined,
849+
length: undefined,
850+
messageText:
851+
`Angular compiler option "strictTemplates" is enabled, however "fullTemplateTypeCheck" is disabled.
852+
853+
Having the "strictTemplates" flag enabled implies that "fullTemplateTypeCheck" is also enabled, so
854+
the latter can not be explicitly disabled.
855+
856+
One of the following actions is required:
857+
1. Remove the "fullTemplateTypeCheck" option.
858+
2. Remove "strictTemplates" or set it to 'false'.
859+
860+
More information about the template type checking compiler options can be found in the documentation:
861+
https://v9.angular.io/guide/template-typecheck#template-type-checking`,
862+
};
863+
}
864+
865+
return null;
866+
}
867+
832868
export class ReferenceGraphAdapter implements ReferencesRegistry {
833869
constructor(private graph: ReferenceGraph) {}
834870

packages/compiler-cli/src/transformers/api.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,8 @@ export interface CompilerOptions extends ts.CompilerOptions {
114114
* whether embedded views are checked.
115115
*
116116
* For maximum type-checking, set this to `true`, and set `strictTemplates` to `true`.
117+
*
118+
* It is an error for this flag to be `false`, while `strictTemplates` is set to `true`.
117119
*/
118120
fullTemplateTypeCheck?: boolean;
119121

packages/compiler-cli/test/ngtsc/template_typecheck_spec.ts

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1552,6 +1552,41 @@ export declare class AnimationEvent {
15521552
});
15531553
});
15541554
});
1555+
1556+
describe('option compatibility verification', () => {
1557+
beforeEach(() => env.write('index.ts', `export const a = 1;`));
1558+
1559+
it('should error if "fullTemplateTypeCheck" is false when "strictTemplates" is true', () => {
1560+
env.tsconfig({fullTemplateTypeCheck: false, strictTemplates: true});
1561+
1562+
const diags = env.driveDiagnostics();
1563+
expect(diags.length).toBe(1);
1564+
expect(diags[0].messageText)
1565+
.toContain(
1566+
'Angular compiler option "strictTemplates" is enabled, however "fullTemplateTypeCheck" is disabled.');
1567+
});
1568+
it('should not error if "fullTemplateTypeCheck" is false when "strictTemplates" is false',
1569+
() => {
1570+
env.tsconfig({fullTemplateTypeCheck: false, strictTemplates: false});
1571+
1572+
const diags = env.driveDiagnostics();
1573+
expect(diags.length).toBe(0);
1574+
});
1575+
it('should not error if "fullTemplateTypeCheck" is not set when "strictTemplates" is true',
1576+
() => {
1577+
env.tsconfig({strictTemplates: true});
1578+
1579+
const diags = env.driveDiagnostics();
1580+
expect(diags.length).toBe(0);
1581+
});
1582+
it('should not error if "fullTemplateTypeCheck" is true set when "strictTemplates" is true',
1583+
() => {
1584+
env.tsconfig({strictTemplates: true});
1585+
1586+
const diags = env.driveDiagnostics();
1587+
expect(diags.length).toBe(0);
1588+
});
1589+
});
15551590
});
15561591
});
15571592

0 commit comments

Comments
 (0)