Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(compiler-cli): nullish coalescing check fixes #44862

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,12 @@ class NullishCoalescingNotNullableCheck extends
return [];
}
const typeLeft = symbolLeft.tsType;
if (typeLeft.flags & (ts.TypeFlags.Any | ts.TypeFlags.Unknown)) {
// We should not make assumptions about the any and unknown types; using a nullish coalescing
// operator is acceptable for those.
return [];
}

// If the left operand's type is different from its non-nullable self, then it must
// contain a null or undefined so this nullish coalescing operator is useful. No diagnostic to
// report.
Expand All @@ -60,7 +66,9 @@ export const factory: TemplateCheckFactory<
name: ExtendedTemplateDiagnosticName.NULLISH_COALESCING_NOT_NULLABLE,
create: (options: NgCompilerOptions) => {
// Require `strictNullChecks` to be enabled.
if (options.strictNullChecks === false) {
const strictNullChecks =
options.strictNullChecks === undefined ? !!options.strict : !!options.strictNullChecks;
if (!strictNullChecks) {
return null;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,24 @@ runInEachFileSystem(() => {

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

it('should return a check if `strictNullChecks` is not configured but `strict` is enabled',
() => {
expect(nullishCoalescingNotNullableFactory.create({strict: true})).toBeDefined();
});

it('should not return a check if `strictNullChecks` is disabled', () => {
expect(nullishCoalescingNotNullableFactory.create({strictNullChecks: false})).toBeNull();
expect(nullishCoalescingNotNullableFactory.create({})).toBeNull(); // Defaults disabled.
});

it('should not return a check if `strict` is enabled but `strictNullChecks` is disabled',
() => {
expect(nullishCoalescingNotNullableFactory.create({strict: true, strictNullChecks: false}))
.toBeNull();
});

it('should produce nullish coalescing warning', () => {
const fileName = absoluteFrom('/main.ts');
const {program, templateTypeChecker} = setup([{
Expand All @@ -48,7 +59,7 @@ runInEachFileSystem(() => {
const component = getClass(sf, 'TestCmp');
const extendedTemplateChecker = new ExtendedTemplateCheckerImpl(
templateTypeChecker, program.getTypeChecker(), [nullishCoalescingNotNullableFactory],
{} /* options */);
{strictNullChecks: true} /* options */);
const diags = extendedTemplateChecker.getDiagnosticsForComponent(component);
expect(diags.length).toBe(1);
expect(diags[0].category).toBe(ts.DiagnosticCategory.Warning);
Expand All @@ -69,7 +80,43 @@ runInEachFileSystem(() => {
const component = getClass(sf, 'TestCmp');
const extendedTemplateChecker = new ExtendedTemplateCheckerImpl(
templateTypeChecker, program.getTypeChecker(), [nullishCoalescingNotNullableFactory],
{} /* options */);
{strictNullChecks: true} /* options */);
const diags = extendedTemplateChecker.getDiagnosticsForComponent(component);
expect(diags.length).toBe(0);
});

it('should not produce nullish coalescing warning for the any type', () => {
const fileName = absoluteFrom('/main.ts');
const {program, templateTypeChecker} = setup([{
fileName,
templates: {
'TestCmp': `{{ var1 ?? 'foo' }}`,
},
source: 'export class TestCmp { var1: any; }'
}]);
const sf = getSourceFileOrError(program, fileName);
const component = getClass(sf, 'TestCmp');
const extendedTemplateChecker = new ExtendedTemplateCheckerImpl(
templateTypeChecker, program.getTypeChecker(), [nullishCoalescingNotNullableFactory],
{strictNullChecks: true} /* options */);
const diags = extendedTemplateChecker.getDiagnosticsForComponent(component);
expect(diags.length).toBe(0);
});

it('should not produce nullish coalescing warning for the unknown type', () => {
const fileName = absoluteFrom('/main.ts');
const {program, templateTypeChecker} = setup([{
fileName,
templates: {
'TestCmp': `{{ var1 ?? 'foo' }}`,
},
source: 'export class TestCmp { var1: unknown; }'
}]);
const sf = getSourceFileOrError(program, fileName);
const component = getClass(sf, 'TestCmp');
const extendedTemplateChecker = new ExtendedTemplateCheckerImpl(
templateTypeChecker, program.getTypeChecker(), [nullishCoalescingNotNullableFactory],
{strictNullChecks: true} /* options */);
const diags = extendedTemplateChecker.getDiagnosticsForComponent(component);
expect(diags.length).toBe(0);
});
Expand All @@ -87,7 +134,7 @@ runInEachFileSystem(() => {
const component = getClass(sf, 'TestCmp');
const extendedTemplateChecker = new ExtendedTemplateCheckerImpl(
templateTypeChecker, program.getTypeChecker(), [nullishCoalescingNotNullableFactory],
{} /* options */);
{strictNullChecks: true} /* options */);
const diags = extendedTemplateChecker.getDiagnosticsForComponent(component);
expect(diags.length).toBe(0);
});
Expand Down Expand Up @@ -116,7 +163,7 @@ runInEachFileSystem(() => {
const component = getClass(sf, 'TestCmp');
const extendedTemplateChecker = new ExtendedTemplateCheckerImpl(
templateTypeChecker, program.getTypeChecker(), [nullishCoalescingNotNullableFactory],
{} /* options */);
{strictNullChecks: true} /* options */);
const diags = extendedTemplateChecker.getDiagnosticsForComponent(component);
expect(diags.length).toBe(1);
expect(diags[0].category).toBe(ts.DiagnosticCategory.Warning);
Expand Down Expand Up @@ -149,7 +196,7 @@ runInEachFileSystem(() => {
const component = getClass(sf, 'TestCmp');
const extendedTemplateChecker = new ExtendedTemplateCheckerImpl(
templateTypeChecker, program.getTypeChecker(), [nullishCoalescingNotNullableFactory],
{} /* options */);
{strictNullChecks: true} /* options */);
const diags = extendedTemplateChecker.getDiagnosticsForComponent(component);
expect(diags.length).toBe(0);
});
Expand All @@ -165,7 +212,7 @@ runInEachFileSystem(() => {
},
source: `
export class TestCmp {
func: (): string | null => null;
func = (): string | null => null;
}
`,
},
Expand All @@ -174,7 +221,7 @@ runInEachFileSystem(() => {
const component = getClass(sf, 'TestCmp');
const extendedTemplateChecker = new ExtendedTemplateCheckerImpl(
templateTypeChecker, program.getTypeChecker(), [nullishCoalescingNotNullableFactory],
{} /* options */);
{strictNullChecks: true} /* options */);
const diags = extendedTemplateChecker.getDiagnosticsForComponent(component);
expect(diags.length).toBe(0);
});
Expand All @@ -196,6 +243,7 @@ runInEachFileSystem(() => {
program.getTypeChecker(),
[nullishCoalescingNotNullableFactory],
{
strictNullChecks: true,
extendedDiagnostics: {
checks: {
nullishCoalescingNotNullable: DiagnosticCategoryLabel.Error,
Expand Down