fix(compiler-cli): ensure that a declaration is available in type-to-value conversion (#38684)

The type-to-value conversion could previously crash if a symbol was
resolved that does not have any declarations, e.g. because it's imported
from a missing module. This would typically result in a semantic
TypeScript diagnostic and halt further compilation, therefore not
reaching the type-to-value conversion logic. In Bazel however, it turns
out that Angular semantic diagnostics are requested even if there are
semantic TypeScript errors in the program, so it would then reach the
type-to-value conversation and crash.

This commit fixes the unsafe access and adds a test that ignores the
TypeScript semantic error, effectively replicating the situation as
experienced under Bazel.

Fixes #38670

PR Close #38684
This commit is contained in:
JoostK
2020-09-02 20:59:57 +02:00
committed by atscott
parent bfb7eec698
commit a32a317ea1
4 changed files with 39 additions and 4 deletions

View File

@ -2214,6 +2214,35 @@ runInEachFileSystem(os => {
expect(diags[0].relatedInformation![1].messageText).toBe('The type is declared here.');
});
it('should report an error when using a missing type as injection token', () => {
// This test replicates the situation where a symbol does not have any declarations at
// all, e.g. because it's imported from a missing module. This would result in a
// semantic TypeScript diagnostic which we ignore in this test to verify that ngtsc's
// analysis is able to operate in this situation.
env.tsconfig({strictInjectionParameters: true});
env.write(`test.ts`, `
import {Injectable} from '@angular/core';
// @ts-expect-error
import {Interface} from 'missing';
@Injectable()
export class MyService {
constructor(param: Interface) {}
}
`);
const diags = env.driveDiagnostics();
expect(diags.length).toBe(1);
expect(ts.flattenDiagnosticMessageText(diags[0].messageText, '\n'))
.toBe(
`No suitable injection token for parameter 'param' of ` +
`class 'MyService'.\n` +
` Consider using the @Inject decorator to specify an injection token.`);
expect(diags[0].relatedInformation!.length).toBe(1);
expect(diags[0].relatedInformation![0].messageText)
.toBe('This type does not have a value, so it cannot be used as injection token.');
});
it('should report an error when no type is present', () => {
env.tsconfig({strictInjectionParameters: true, noImplicitAny: false});
env.write(`test.ts`, `