Skip to content

Commit

Permalink
refactor(ivy): force NG-space error codes for template errors (angula…
Browse files Browse the repository at this point in the history
…r#34460)

The function `makeTemplateDiagnostic` was accepting an error code of type
`number`, making it easy to accidentally pass an `ErrorCode` directly and
not convert it to an Angular diagnostic code first.

This commit refactors `makeTemplateDiagnostic` to accept `ErrorCode` up
front, and convert it internally. This is less error-prone.

PR Close angular#34460
  • Loading branch information
alxhub authored and kara committed Dec 18, 2019
1 parent 498a2ff commit 6057c7a
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 11 deletions.
10 changes: 4 additions & 6 deletions packages/compiler-cli/src/ngtsc/typecheck/src/diagnostics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import {AbsoluteSourceSpan, ParseSourceSpan} from '@angular/compiler';
import * as ts from 'typescript';

import {ErrorCode, ngErrorCode} from '../../diagnostics';
import {getTokenAtPosition} from '../../util/src/typescript';

import {ExternalTemplateSourceMapping, TemplateId, TemplateSourceMapping} from './api';
Expand Down Expand Up @@ -135,7 +136,7 @@ export function translateDiagnostic(
*/
export function makeTemplateDiagnostic(
mapping: TemplateSourceMapping, span: ParseSourceSpan, category: ts.DiagnosticCategory,
code: number, messageText: string | ts.DiagnosticMessageChain, relatedMessage?: {
code: ErrorCode, messageText: string | ts.DiagnosticMessageChain, relatedMessage?: {
text: string,
span: ParseSourceSpan,
}): ts.Diagnostic {
Expand All @@ -156,9 +157,7 @@ export function makeTemplateDiagnostic(
// directly into the bytes of the source file.
return {
source: 'ngtsc',
code,
category,
messageText,
code: ngErrorCode(code), category, messageText,
file: mapping.node.getSourceFile(),
start: span.start.offset,
length: span.end.offset - span.start.offset, relatedInformation,
Expand Down Expand Up @@ -206,8 +205,7 @@ export function makeTemplateDiagnostic(
return {
source: 'ngtsc',
category,
code,
messageText,
code: ngErrorCode(code), messageText,
file: sf,
start: span.start.offset,
length: span.end.offset - span.start.offset,
Expand Down
9 changes: 4 additions & 5 deletions packages/compiler-cli/src/ngtsc/typecheck/src/oob.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ export class OutOfBandDiagnosticRecorderImpl implements OutOfBandDiagnosticRecor
const errorMsg = `No directive found with exportAs '${value}'.`;
this._diagnostics.push(makeTemplateDiagnostic(
mapping, ref.valueSpan || ref.sourceSpan, ts.DiagnosticCategory.Error,
ngErrorCode(ErrorCode.MISSING_REFERENCE_TARGET), errorMsg));
ErrorCode.MISSING_REFERENCE_TARGET, errorMsg));
}

missingPipe(templateId: TemplateId, ast: BindingPipe): void {
Expand All @@ -79,8 +79,7 @@ export class OutOfBandDiagnosticRecorderImpl implements OutOfBandDiagnosticRecor
`Assertion failure: no SourceLocation found for usage of pipe '${ast.name}'.`);
}
this._diagnostics.push(makeTemplateDiagnostic(
mapping, sourceSpan, ts.DiagnosticCategory.Error, ngErrorCode(ErrorCode.MISSING_PIPE),
errorMsg));
mapping, sourceSpan, ts.DiagnosticCategory.Error, ErrorCode.MISSING_PIPE, errorMsg));
}

illegalAssignmentToTemplateVar(
Expand All @@ -94,8 +93,8 @@ export class OutOfBandDiagnosticRecorderImpl implements OutOfBandDiagnosticRecor
throw new Error(`Assertion failure: no SourceLocation found for property binding.`);
}
this._diagnostics.push(makeTemplateDiagnostic(
mapping, sourceSpan, ts.DiagnosticCategory.Error,
ngErrorCode(ErrorCode.WRITE_TO_READ_ONLY_VARIABLE), errorMsg, {
mapping, sourceSpan, ts.DiagnosticCategory.Error, ErrorCode.WRITE_TO_READ_ONLY_VARIABLE,
errorMsg, {
text: `The variable ${assignment.name} is declared here.`,
span: target.valueSpan || target.sourceSpan,
}));
Expand Down

0 comments on commit 6057c7a

Please sign in to comment.