feat(language-service): specific suggestions for template context diags (#34751)

This commit elaborates diagnostics produced for invalid template
contexts by including the name of the embedded template type using the
template context, and in the common case that the implicity property is
being referenced (e.g. in a `for .. of ..` expression), suggesting to
refine the type of the context. This suggestion is provided because
users will sometimes use a base class as the type of the context in the
embedded view, and a more specific context later on (e.g. in an
`ngOnChanges` method).

Closes https://github.com/angular/vscode-ng-language-service/issues/251

PR Close #34751
This commit is contained in:
ayazhafiz
2020-01-12 14:15:50 -08:00
committed by Andrew Kushnir
parent eb4b7d2d8b
commit 4ba478267e
4 changed files with 55 additions and 13 deletions

View File

@ -269,10 +269,12 @@ class ExpressionDiagnosticsVisitor extends RecursiveTemplateAstVisitor {
if (context && !context.has(ast.value)) {
if (ast.value === '$implicit') {
this.reportError(
'The template context does not have an implicit value', spanOf(ast.sourceSpan));
`The template context of '${directive.type.reference.name}' does not define an implicit value.\n` +
`If the context type is a base type, consider refining it to a more specific type.`,
spanOf(ast.sourceSpan));
} else {
this.reportError(
`The template context does not define a member called '${ast.value}'`,
`The template context of '${directive.type.reference.name}' does not define a member called '${ast.value}'`,
spanOf(ast.sourceSpan));
}
}