fix(language-service): warn, not error, on missing context members (#35036)
The language service reports an error when a directive's template context is missing a member that is being used in a template (e.g. if `$implicit` is being used with a template context typed as `any`). While this diagnostic message is valuable, typing template contexts loosely as `any` or `object` is very widespread in community packages, and often still compiles correctly, so reporting the diagnostic as an error may be misleading to users. This commit changes the diagnostic to be a warning, and adds additional information about how the user can eliminate the warning entirely -- by refining the template context type. Closes https://github.com/angular/vscode-ng-language-service/issues/572 PR Close #35036
This commit is contained in:

committed by
Andrew Kushnir

parent
b64ead5cb8
commit
b608fa55f0
@ -292,16 +292,12 @@ class ExpressionDiagnosticsVisitor extends RecursiveTemplateAstVisitor {
|
||||
if (directive && ast.value) {
|
||||
const context = this.info.query.getTemplateContext(directive.type.reference) !;
|
||||
if (context && !context.has(ast.value)) {
|
||||
if (ast.value === '$implicit') {
|
||||
this.reportError(
|
||||
`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 of '${directive.type.reference.name}' does not define a member called '${ast.value}'`,
|
||||
spanOf(ast.sourceSpan));
|
||||
}
|
||||
const missingMember =
|
||||
ast.value === '$implicit' ? 'an implicit value' : `a member called '${ast.value}'`;
|
||||
this.reportDiagnostic(
|
||||
`The template context of '${directive.type.reference.name}' does not define ${missingMember}.\n` +
|
||||
`If the context type is a base type or 'any', consider refining it to a more specific type.`,
|
||||
spanOf(ast.sourceSpan), ts.DiagnosticCategory.Suggestion);
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -353,11 +349,9 @@ class ExpressionDiagnosticsVisitor extends RecursiveTemplateAstVisitor {
|
||||
|
||||
private pop() { this.path.pop(); }
|
||||
|
||||
private reportError(message: string, span: Span|undefined) {
|
||||
if (span) {
|
||||
this.diagnostics.push(
|
||||
{span: offsetSpan(span, this.info.offset), kind: ts.DiagnosticCategory.Error, message});
|
||||
}
|
||||
private reportDiagnostic(
|
||||
message: string, span: Span, kind: ts.DiagnosticCategory = ts.DiagnosticCategory.Error) {
|
||||
this.diagnostics.push({span: offsetSpan(span, this.info.offset), kind, message});
|
||||
}
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user