Skip to content

Commit 07185f4

Browse files
JoostKthePunderWoman
authored andcommittedJan 31, 2022
fix(compiler-cli): enable nullish coalescing check only with strictNullChecks (#44862)
TypeScript configures `strictNullChecks` to be disabled by default, so the nullish coalescing check should follow the same default. The rule actively depends on `strictNullChecks`, as TypeScript doesn't include `null`/`undefined` in its types otherwise so the check wouldn't have a way to differentiate between them. This commit also takes the `strict` flag into account when `strictNullChecks` itself is not configured. PR Close #44862
1 parent 0778e6f commit 07185f4

File tree

2 files changed

+24
-10
lines changed

2 files changed

+24
-10
lines changed
 

‎packages/compiler-cli/src/ngtsc/typecheck/extended/checks/nullish_coalescing_not_nullable/index.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,9 @@ export const factory: TemplateCheckFactory<
6666
name: ExtendedTemplateDiagnosticName.NULLISH_COALESCING_NOT_NULLABLE,
6767
create: (options: NgCompilerOptions) => {
6868
// Require `strictNullChecks` to be enabled.
69-
if (options.strictNullChecks === false) {
69+
const strictNullChecks =
70+
options.strictNullChecks === undefined ? !!options.strict : !!options.strictNullChecks;
71+
if (!strictNullChecks) {
7072
return null;
7173
}
7274

‎packages/compiler-cli/src/ngtsc/typecheck/extended/test/checks/nullish_coalescing_not_nullable/nullish_coalescing_not_nullable_spec.ts

+21-9
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,24 @@ runInEachFileSystem(() => {
2828

2929
it('should return a check if `strictNullChecks` is enabled', () => {
3030
expect(nullishCoalescingNotNullableFactory.create({strictNullChecks: true})).toBeDefined();
31-
expect(nullishCoalescingNotNullableFactory.create({})).not.toBeNull(); // Defaults enabled.
3231
});
3332

33+
it('should return a check if `strictNullChecks` is not configured but `strict` is enabled',
34+
() => {
35+
expect(nullishCoalescingNotNullableFactory.create({strict: true})).toBeDefined();
36+
});
37+
3438
it('should not return a check if `strictNullChecks` is disabled', () => {
3539
expect(nullishCoalescingNotNullableFactory.create({strictNullChecks: false})).toBeNull();
40+
expect(nullishCoalescingNotNullableFactory.create({})).toBeNull(); // Defaults disabled.
3641
});
3742

43+
it('should not return a check if `strict` is enabled but `strictNullChecks` is disabled',
44+
() => {
45+
expect(nullishCoalescingNotNullableFactory.create({strict: true, strictNullChecks: false}))
46+
.toBeNull();
47+
});
48+
3849
it('should produce nullish coalescing warning', () => {
3950
const fileName = absoluteFrom('/main.ts');
4051
const {program, templateTypeChecker} = setup([{
@@ -48,7 +59,7 @@ runInEachFileSystem(() => {
4859
const component = getClass(sf, 'TestCmp');
4960
const extendedTemplateChecker = new ExtendedTemplateCheckerImpl(
5061
templateTypeChecker, program.getTypeChecker(), [nullishCoalescingNotNullableFactory],
51-
{} /* options */);
62+
{strictNullChecks: true} /* options */);
5263
const diags = extendedTemplateChecker.getDiagnosticsForComponent(component);
5364
expect(diags.length).toBe(1);
5465
expect(diags[0].category).toBe(ts.DiagnosticCategory.Warning);
@@ -69,7 +80,7 @@ runInEachFileSystem(() => {
6980
const component = getClass(sf, 'TestCmp');
7081
const extendedTemplateChecker = new ExtendedTemplateCheckerImpl(
7182
templateTypeChecker, program.getTypeChecker(), [nullishCoalescingNotNullableFactory],
72-
{} /* options */);
83+
{strictNullChecks: true} /* options */);
7384
const diags = extendedTemplateChecker.getDiagnosticsForComponent(component);
7485
expect(diags.length).toBe(0);
7586
});
@@ -87,7 +98,7 @@ runInEachFileSystem(() => {
8798
const component = getClass(sf, 'TestCmp');
8899
const extendedTemplateChecker = new ExtendedTemplateCheckerImpl(
89100
templateTypeChecker, program.getTypeChecker(), [nullishCoalescingNotNullableFactory],
90-
{} /* options */);
101+
{strictNullChecks: true} /* options */);
91102
const diags = extendedTemplateChecker.getDiagnosticsForComponent(component);
92103
expect(diags.length).toBe(0);
93104
});
@@ -105,7 +116,7 @@ runInEachFileSystem(() => {
105116
const component = getClass(sf, 'TestCmp');
106117
const extendedTemplateChecker = new ExtendedTemplateCheckerImpl(
107118
templateTypeChecker, program.getTypeChecker(), [nullishCoalescingNotNullableFactory],
108-
{} /* options */);
119+
{strictNullChecks: true} /* options */);
109120
const diags = extendedTemplateChecker.getDiagnosticsForComponent(component);
110121
expect(diags.length).toBe(0);
111122
});
@@ -123,7 +134,7 @@ runInEachFileSystem(() => {
123134
const component = getClass(sf, 'TestCmp');
124135
const extendedTemplateChecker = new ExtendedTemplateCheckerImpl(
125136
templateTypeChecker, program.getTypeChecker(), [nullishCoalescingNotNullableFactory],
126-
{} /* options */);
137+
{strictNullChecks: true} /* options */);
127138
const diags = extendedTemplateChecker.getDiagnosticsForComponent(component);
128139
expect(diags.length).toBe(0);
129140
});
@@ -152,7 +163,7 @@ runInEachFileSystem(() => {
152163
const component = getClass(sf, 'TestCmp');
153164
const extendedTemplateChecker = new ExtendedTemplateCheckerImpl(
154165
templateTypeChecker, program.getTypeChecker(), [nullishCoalescingNotNullableFactory],
155-
{} /* options */);
166+
{strictNullChecks: true} /* options */);
156167
const diags = extendedTemplateChecker.getDiagnosticsForComponent(component);
157168
expect(diags.length).toBe(1);
158169
expect(diags[0].category).toBe(ts.DiagnosticCategory.Warning);
@@ -185,7 +196,7 @@ runInEachFileSystem(() => {
185196
const component = getClass(sf, 'TestCmp');
186197
const extendedTemplateChecker = new ExtendedTemplateCheckerImpl(
187198
templateTypeChecker, program.getTypeChecker(), [nullishCoalescingNotNullableFactory],
188-
{} /* options */);
199+
{strictNullChecks: true} /* options */);
189200
const diags = extendedTemplateChecker.getDiagnosticsForComponent(component);
190201
expect(diags.length).toBe(0);
191202
});
@@ -210,7 +221,7 @@ runInEachFileSystem(() => {
210221
const component = getClass(sf, 'TestCmp');
211222
const extendedTemplateChecker = new ExtendedTemplateCheckerImpl(
212223
templateTypeChecker, program.getTypeChecker(), [nullishCoalescingNotNullableFactory],
213-
{} /* options */);
224+
{strictNullChecks: true} /* options */);
214225
const diags = extendedTemplateChecker.getDiagnosticsForComponent(component);
215226
expect(diags.length).toBe(0);
216227
});
@@ -232,6 +243,7 @@ runInEachFileSystem(() => {
232243
program.getTypeChecker(),
233244
[nullishCoalescingNotNullableFactory],
234245
{
246+
strictNullChecks: true,
235247
extendedDiagnostics: {
236248
checks: {
237249
nullishCoalescingNotNullable: DiagnosticCategoryLabel.Error,

0 commit comments

Comments
 (0)
Please sign in to comment.