fix(language-service): use host.error() instead of console.error() (#34114)

`host.error()` would log to file, and makes error messages much easier
to inspect because entries are time-stamped.

PR Close #34114
This commit is contained in:
Keen Yee Liau 2019-11-27 16:03:20 -08:00 committed by Miško Hevery
parent 39722df41e
commit 99320e1ffc

View File

@ -53,13 +53,6 @@ function missingDirective(name: string, isComponent: boolean) {
'available inside a template. Consider adding it to a NgModule declaration.'; 'available inside a template. Consider adding it to a NgModule declaration.';
} }
/**
* Logs an error for an impossible state with a certain message.
*/
function logImpossibleState(message: string) {
console.error(`Impossible state: ${message}`);
}
/** /**
* Performs a variety diagnostics on directive declarations. * Performs a variety diagnostics on directive declarations.
* *
@ -85,14 +78,14 @@ export function getDeclarationDiagnostics(
const sf = host.getSourceFile(type.filePath); const sf = host.getSourceFile(type.filePath);
if (!sf) { if (!sf) {
logImpossibleState(`directive ${type.name} exists but has no source file`); host.error(`directive ${type.name} exists but has no source file`);
return []; return [];
} }
// TypeScript identifier of the directive declaration annotation (e.g. "Component" or // TypeScript identifier of the directive declaration annotation (e.g. "Component" or
// "Directive") on a directive class. // "Directive") on a directive class.
const directiveIdentifier = findTightestNode(sf, declarationSpan.start); const directiveIdentifier = findTightestNode(sf, declarationSpan.start);
if (!directiveIdentifier) { if (!directiveIdentifier) {
logImpossibleState(`directive ${type.name} exists but has no identifier`); host.error(`directive ${type.name} exists but has no identifier`);
return []; return [];
} }
@ -138,7 +131,7 @@ export function getDeclarationDiagnostics(
const templateUrlNode = findPropertyValueOfType( const templateUrlNode = findPropertyValueOfType(
directiveIdentifier.parent, 'templateUrl', ts.isLiteralExpression); directiveIdentifier.parent, 'templateUrl', ts.isLiteralExpression);
if (!templateUrlNode) { if (!templateUrlNode) {
logImpossibleState(`templateUrl ${templateUrl} exists but its TypeScript node doesn't`); host.error(`templateUrl ${templateUrl} exists but its TypeScript node doesn't`);
return []; return [];
} }
@ -151,7 +144,7 @@ export function getDeclarationDiagnostics(
const styleUrlsNode = findPropertyValueOfType( const styleUrlsNode = findPropertyValueOfType(
directiveIdentifier.parent, 'styleUrls', ts.isArrayLiteralExpression); directiveIdentifier.parent, 'styleUrls', ts.isArrayLiteralExpression);
if (!styleUrlsNode) { if (!styleUrlsNode) {
logImpossibleState(`styleUrls property exists but its TypeScript node doesn't'`); host.error(`styleUrls property exists but its TypeScript node doesn't'`);
return []; return [];
} }