Skip to content

Commit

Permalink
fix(ivy): template type-check errors from TS should not use NG error …
Browse files Browse the repository at this point in the history
…codes (#35146)

A bug previously caused the template type-checking diagnostics produced by
TypeScript for template expressions to use -99-prefixed error codes. These
codes are converted to "NG" errors instead of "TS" errors during diagnostic
printing. This commit fixes the issue.

PR Close #35146
  • Loading branch information
alxhub authored and mhevery committed Feb 4, 2020
1 parent cf1fa60 commit cf3071f
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 11 deletions.
10 changes: 6 additions & 4 deletions packages/compiler-cli/src/ngtsc/typecheck/src/diagnostics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
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 @@ -146,7 +145,7 @@ export function translateDiagnostic(
*/
export function makeTemplateDiagnostic(
mapping: TemplateSourceMapping, span: ParseSourceSpan, category: ts.DiagnosticCategory,
code: ErrorCode, messageText: string | ts.DiagnosticMessageChain, relatedMessage?: {
code: number, messageText: string | ts.DiagnosticMessageChain, relatedMessage?: {
text: string,
span: ParseSourceSpan,
}): TemplateDiagnostic {
Expand All @@ -167,7 +166,9 @@ export function makeTemplateDiagnostic(
// directly into the bytes of the source file.
return {
source: 'ngtsc',
code: ngErrorCode(code), category, messageText,
code,
category,
messageText,
file: mapping.node.getSourceFile(),
componentFile: mapping.node.getSourceFile(),
start: span.start.offset,
Expand Down Expand Up @@ -216,7 +217,8 @@ export function makeTemplateDiagnostic(
return {
source: 'ngtsc',
category,
code: ngErrorCode(code), messageText,
code,
messageText,
file: sf,
componentFile: componentSf,
start: span.start.offset,
Expand Down
7 changes: 4 additions & 3 deletions packages/compiler-cli/src/ngtsc/typecheck/src/dom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import {DomElementSchemaRegistry, ParseSourceSpan, SchemaMetadata, TmplAstElement} from '@angular/compiler';
import * as ts from 'typescript';

import {ErrorCode} from '../../diagnostics';
import {ErrorCode, ngErrorCode} from '../../diagnostics';

import {TemplateId} from './api';
import {TemplateSourceResolver, makeTemplateDiagnostic} from './diagnostics';
Expand Down Expand Up @@ -92,7 +92,7 @@ export class RegistryDomSchemaChecker implements DomSchemaChecker {

const diag = makeTemplateDiagnostic(
mapping, element.sourceSpan, ts.DiagnosticCategory.Error,
ErrorCode.SCHEMA_INVALID_ELEMENT, errorMsg);
ngErrorCode(ErrorCode.SCHEMA_INVALID_ELEMENT), errorMsg);
this._diagnostics.push(diag);
}
}
Expand All @@ -117,7 +117,8 @@ export class RegistryDomSchemaChecker implements DomSchemaChecker {
}

const diag = makeTemplateDiagnostic(
mapping, span, ts.DiagnosticCategory.Error, ErrorCode.SCHEMA_INVALID_ATTRIBUTE, errorMsg);
mapping, span, ts.DiagnosticCategory.Error,
ngErrorCode(ErrorCode.SCHEMA_INVALID_ATTRIBUTE), errorMsg);
this._diagnostics.push(diag);
}
}
Expand Down
9 changes: 5 additions & 4 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,
ErrorCode.MISSING_REFERENCE_TARGET, errorMsg));
ngErrorCode(ErrorCode.MISSING_REFERENCE_TARGET), errorMsg));
}

missingPipe(templateId: TemplateId, ast: BindingPipe): void {
Expand All @@ -79,7 +79,8 @@ 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, ErrorCode.MISSING_PIPE, errorMsg));
mapping, sourceSpan, ts.DiagnosticCategory.Error, ngErrorCode(ErrorCode.MISSING_PIPE),
errorMsg));
}

illegalAssignmentToTemplateVar(
Expand All @@ -93,8 +94,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, ErrorCode.WRITE_TO_READ_ONLY_VARIABLE,
errorMsg, {
mapping, sourceSpan, ts.DiagnosticCategory.Error,
ngErrorCode(ErrorCode.WRITE_TO_READ_ONLY_VARIABLE), errorMsg, {
text: `The variable ${assignment.name} is declared here.`,
span: target.valueSpan || target.sourceSpan,
}));
Expand Down
2 changes: 2 additions & 0 deletions packages/compiler-cli/test/ngtsc/template_typecheck_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,8 @@ export declare class AnimationEvent {
const diags = env.driveDiagnostics();
expect(diags.length).toBe(1);
expect(diags[0].messageText).toEqual(`Type 'string' is not assignable to type 'number'.`);
// The reported error code should be in the TS error space, not a -99 "NG" code.
expect(diags[0].code).toBeGreaterThan(0);
});

it('should support inputs and outputs with names that are not JavaScript identifiers', () => {
Expand Down

0 comments on commit cf3071f

Please sign in to comment.