From 6057c7a373016d42f80218bdc45602ea6108c73c Mon Sep 17 00:00:00 2001 From: Alex Rickabaugh Date: Tue, 17 Dec 2019 12:39:57 -0800 Subject: [PATCH] refactor(ivy): force NG-space error codes for template errors (#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 #34460 --- .../src/ngtsc/typecheck/src/diagnostics.ts | 10 ++++------ packages/compiler-cli/src/ngtsc/typecheck/src/oob.ts | 9 ++++----- 2 files changed, 8 insertions(+), 11 deletions(-) diff --git a/packages/compiler-cli/src/ngtsc/typecheck/src/diagnostics.ts b/packages/compiler-cli/src/ngtsc/typecheck/src/diagnostics.ts index 4e8b6f344c28b..b5b59204b352d 100644 --- a/packages/compiler-cli/src/ngtsc/typecheck/src/diagnostics.ts +++ b/packages/compiler-cli/src/ngtsc/typecheck/src/diagnostics.ts @@ -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'; @@ -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 { @@ -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, @@ -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, diff --git a/packages/compiler-cli/src/ngtsc/typecheck/src/oob.ts b/packages/compiler-cli/src/ngtsc/typecheck/src/oob.ts index 58096e4fdba65..6a5404d4c9862 100644 --- a/packages/compiler-cli/src/ngtsc/typecheck/src/oob.ts +++ b/packages/compiler-cli/src/ngtsc/typecheck/src/oob.ts @@ -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 { @@ -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( @@ -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, }));