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
This commit is contained in:
parent
498a2ffba3
commit
6057c7a373
|
@ -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,
|
||||
|
|
|
@ -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,
|
||||
}));
|
||||
|
|
Loading…
Reference in New Issue