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 {AbsoluteSourceSpan, ParseSourceSpan} from '@angular/compiler';
|
||||||
import * as ts from 'typescript';
|
import * as ts from 'typescript';
|
||||||
|
|
||||||
|
import {ErrorCode, ngErrorCode} from '../../diagnostics';
|
||||||
import {getTokenAtPosition} from '../../util/src/typescript';
|
import {getTokenAtPosition} from '../../util/src/typescript';
|
||||||
|
|
||||||
import {ExternalTemplateSourceMapping, TemplateId, TemplateSourceMapping} from './api';
|
import {ExternalTemplateSourceMapping, TemplateId, TemplateSourceMapping} from './api';
|
||||||
@ -135,7 +136,7 @@ export function translateDiagnostic(
|
|||||||
*/
|
*/
|
||||||
export function makeTemplateDiagnostic(
|
export function makeTemplateDiagnostic(
|
||||||
mapping: TemplateSourceMapping, span: ParseSourceSpan, category: ts.DiagnosticCategory,
|
mapping: TemplateSourceMapping, span: ParseSourceSpan, category: ts.DiagnosticCategory,
|
||||||
code: number, messageText: string | ts.DiagnosticMessageChain, relatedMessage?: {
|
code: ErrorCode, messageText: string | ts.DiagnosticMessageChain, relatedMessage?: {
|
||||||
text: string,
|
text: string,
|
||||||
span: ParseSourceSpan,
|
span: ParseSourceSpan,
|
||||||
}): ts.Diagnostic {
|
}): ts.Diagnostic {
|
||||||
@ -156,9 +157,7 @@ export function makeTemplateDiagnostic(
|
|||||||
// directly into the bytes of the source file.
|
// directly into the bytes of the source file.
|
||||||
return {
|
return {
|
||||||
source: 'ngtsc',
|
source: 'ngtsc',
|
||||||
code,
|
code: ngErrorCode(code), category, messageText,
|
||||||
category,
|
|
||||||
messageText,
|
|
||||||
file: mapping.node.getSourceFile(),
|
file: mapping.node.getSourceFile(),
|
||||||
start: span.start.offset,
|
start: span.start.offset,
|
||||||
length: span.end.offset - span.start.offset, relatedInformation,
|
length: span.end.offset - span.start.offset, relatedInformation,
|
||||||
@ -206,8 +205,7 @@ export function makeTemplateDiagnostic(
|
|||||||
return {
|
return {
|
||||||
source: 'ngtsc',
|
source: 'ngtsc',
|
||||||
category,
|
category,
|
||||||
code,
|
code: ngErrorCode(code), messageText,
|
||||||
messageText,
|
|
||||||
file: sf,
|
file: sf,
|
||||||
start: span.start.offset,
|
start: span.start.offset,
|
||||||
length: span.end.offset - 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}'.`;
|
const errorMsg = `No directive found with exportAs '${value}'.`;
|
||||||
this._diagnostics.push(makeTemplateDiagnostic(
|
this._diagnostics.push(makeTemplateDiagnostic(
|
||||||
mapping, ref.valueSpan || ref.sourceSpan, ts.DiagnosticCategory.Error,
|
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 {
|
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}'.`);
|
`Assertion failure: no SourceLocation found for usage of pipe '${ast.name}'.`);
|
||||||
}
|
}
|
||||||
this._diagnostics.push(makeTemplateDiagnostic(
|
this._diagnostics.push(makeTemplateDiagnostic(
|
||||||
mapping, sourceSpan, ts.DiagnosticCategory.Error, ngErrorCode(ErrorCode.MISSING_PIPE),
|
mapping, sourceSpan, ts.DiagnosticCategory.Error, ErrorCode.MISSING_PIPE, errorMsg));
|
||||||
errorMsg));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
illegalAssignmentToTemplateVar(
|
illegalAssignmentToTemplateVar(
|
||||||
@ -94,8 +93,8 @@ export class OutOfBandDiagnosticRecorderImpl implements OutOfBandDiagnosticRecor
|
|||||||
throw new Error(`Assertion failure: no SourceLocation found for property binding.`);
|
throw new Error(`Assertion failure: no SourceLocation found for property binding.`);
|
||||||
}
|
}
|
||||||
this._diagnostics.push(makeTemplateDiagnostic(
|
this._diagnostics.push(makeTemplateDiagnostic(
|
||||||
mapping, sourceSpan, ts.DiagnosticCategory.Error,
|
mapping, sourceSpan, ts.DiagnosticCategory.Error, ErrorCode.WRITE_TO_READ_ONLY_VARIABLE,
|
||||||
ngErrorCode(ErrorCode.WRITE_TO_READ_ONLY_VARIABLE), errorMsg, {
|
errorMsg, {
|
||||||
text: `The variable ${assignment.name} is declared here.`,
|
text: `The variable ${assignment.name} is declared here.`,
|
||||||
span: target.valueSpan || target.sourceSpan,
|
span: target.valueSpan || target.sourceSpan,
|
||||||
}));
|
}));
|
||||||
|
Loading…
x
Reference in New Issue
Block a user