Skip to content

Commit bb5facb

Browse files
danieltre23dylhunn
authored andcommitted
refactor(compiler-cli): add makeTemplateDiagnostic wrapper (#42937)
Add a `makeTemplateDiagnostic` wrapper in the `TemplateTypeChecker`. This requiers less parameters to create template diagnostics, since the `TemplateTypeChecker` can get the templateId and mapping from it's scope with the `ts.ClassDeclartion`. The `TemplateTypeChecker` is often used to determine if a diagnostic should be produced, so it makes sense to have a function in it that helps create them. Refs #42966 PR Close #42937
1 parent 77ae445 commit bb5facb

3 files changed

Lines changed: 35 additions & 3 deletions

File tree

‎packages/compiler-cli/src/ngtsc/typecheck/api/BUILD.bazel‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ ts_library(
99
deps = [
1010
"//packages:types",
1111
"//packages/compiler",
12+
"//packages/compiler-cli/src/ngtsc/diagnostics",
1213
"//packages/compiler-cli/src/ngtsc/file_system",
1314
"//packages/compiler-cli/src/ngtsc/imports",
1415
"//packages/compiler-cli/src/ngtsc/metadata",

‎packages/compiler-cli/src/ngtsc/typecheck/api/checker.ts‎

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,10 @@
66
* found in the LICENSE file at https://angular.io/license
77
*/
88

9-
import {AST, MethodCall, ParseError, PropertyRead, SafeMethodCall, SafePropertyRead, TmplAstElement, TmplAstNode, TmplAstTemplate} from '@angular/compiler';
9+
import {AST, MethodCall, ParseError, ParseSourceSpan, PropertyRead, SafeMethodCall, SafePropertyRead, TmplAstElement, TmplAstNode, TmplAstTemplate} from '@angular/compiler';
1010
import {AbsoluteFsPath} from '@angular/compiler-cli/src/ngtsc/file_system';
1111
import * as ts from 'typescript';
12+
import {ErrorCode} from '../../diagnostics';
1213

1314
import {FullTemplateMapping, TypeCheckableDirectiveMeta} from './api';
1415
import {GlobalCompletion} from './completion';
@@ -154,6 +155,18 @@ export interface TemplateTypeChecker {
154155
* the next request.
155156
*/
156157
invalidateClass(clazz: ts.ClassDeclaration): void;
158+
159+
/**
160+
* Constructs a `ts.Diagnostic` for a given `ParseSourceSpan` within a template.
161+
*/
162+
makeTemplateDiagnostic(
163+
clazz: ts.ClassDeclaration, sourceSpan: ParseSourceSpan, category: ts.DiagnosticCategory,
164+
errorCode: ErrorCode, message: string, relatedInformation?: {
165+
text: string,
166+
start: number,
167+
end: number,
168+
sourceFile: ts.SourceFile,
169+
}[]): ts.Diagnostic;
157170
}
158171

159172
/**

‎packages/compiler-cli/src/ngtsc/typecheck/src/checker.ts‎

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,9 @@
66
* found in the LICENSE file at https://angular.io/license
77
*/
88

9-
import {AST, CssSelector, DomElementSchemaRegistry, MethodCall, ParseError, parseTemplate, PropertyRead, SafeMethodCall, SafePropertyRead, TmplAstElement, TmplAstNode, TmplAstReference, TmplAstTemplate, TmplAstVariable} from '@angular/compiler';
9+
import {AST, CssSelector, DomElementSchemaRegistry, MethodCall, ParseError, ParseSourceSpan, parseTemplate, PropertyRead, SafeMethodCall, SafePropertyRead, TmplAstElement, TmplAstNode, TmplAstReference, TmplAstTemplate, TmplAstVariable} from '@angular/compiler';
1010
import * as ts from 'typescript';
11+
import {ErrorCode} from '../../diagnostics';
1112

1213
import {absoluteFrom, absoluteFromSourceFile, AbsoluteFsPath, getSourceFileOrError} from '../../file_system';
1314
import {Reference, ReferenceEmitter} from '../../imports';
@@ -19,7 +20,7 @@ import {ComponentScopeReader, TypeCheckScopeRegistry} from '../../scope';
1920
import {isShim} from '../../shims';
2021
import {getSourceFileOrNull, isSymbolWithValueDeclaration} from '../../util/src/typescript';
2122
import {DirectiveInScope, ElementSymbol, FullTemplateMapping, GlobalCompletion, OptimizeFor, PipeInScope, ProgramTypeCheckAdapter, ShimLocation, Symbol, TemplateId, TemplateSymbol, TemplateTypeChecker, TypeCheckableDirectiveMeta, TypeCheckingConfig} from '../api';
22-
import {TemplateDiagnostic} from '../diagnostics';
23+
import {makeTemplateDiagnostic, TemplateDiagnostic} from '../diagnostics';
2324

2425
import {CompletionEngine} from './completion';
2526
import {InliningMode, ShimTypeCheckingData, TemplateData, TypeCheckContextImpl, TypeCheckingHost} from './context';
@@ -297,6 +298,23 @@ export class TemplateTypeCheckerImpl implements TemplateTypeChecker {
297298
this.isComplete = false;
298299
}
299300

301+
makeTemplateDiagnostic(
302+
clazz: ts.ClassDeclaration, sourceSpan: ParseSourceSpan, category: ts.DiagnosticCategory,
303+
errorCode: ErrorCode, message: string, relatedInformation?: {
304+
text: string,
305+
start: number,
306+
end: number,
307+
sourceFile: ts.SourceFile,
308+
}[]): TemplateDiagnostic {
309+
const sfPath = absoluteFromSourceFile(clazz.getSourceFile());
310+
const fileRecord = this.state.get(sfPath)!;
311+
const templateId = fileRecord.sourceManager.getTemplateId(clazz);
312+
const mapping = fileRecord.sourceManager.getSourceMapping(templateId);
313+
314+
return makeTemplateDiagnostic(
315+
templateId, mapping, sourceSpan, category, errorCode, message, relatedInformation);
316+
}
317+
300318
private getOrCreateCompletionEngine(component: ts.ClassDeclaration): CompletionEngine|null {
301319
if (this.completionCache.has(component)) {
302320
return this.completionCache.get(component)!;

0 commit comments

Comments
 (0)