Skip to content

Commit 1a71167

Browse files
crisbetoAndrewKushnir
authored andcommitted
refactor(compiler-cli): rework source manager to accommodate directives (#60191)
Currently the `TemplateSourceManager` is set up to specifically cater to component templates. These changes make it more generic so we can reuse it for directives. PR Close #60191
1 parent 83218bd commit 1a71167

File tree

16 files changed

+102
-108
lines changed

16 files changed

+102
-108
lines changed

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -416,11 +416,11 @@ export interface SourceLocation {
416416
}
417417

418418
/**
419-
* A representation of all a node's template mapping information we know. Useful for producing
419+
* A representation of all a node's type checking information we know. Useful for producing
420420
* diagnostics based on a TCB node or generally mapping from a TCB node back to a template location.
421421
*/
422-
export interface FullTemplateMapping {
422+
export interface FullSourceMapping {
423423
sourceLocation: SourceLocation;
424-
templateSourceMapping: SourceMapping;
424+
sourceMapping: SourceMapping;
425425
span: ParseSourceSpan;
426426
}

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ import {Reference} from '../../imports';
2626
import {NgModuleMeta, PipeMeta} from '../../metadata';
2727
import {ClassDeclaration} from '../../reflection';
2828

29-
import {FullTemplateMapping, NgTemplateDiagnostic, TypeCheckableDirectiveMeta} from './api';
29+
import {FullSourceMapping, NgTemplateDiagnostic, TypeCheckableDirectiveMeta} from './api';
3030
import {GlobalCompletion} from './completion';
3131
import {PotentialDirective, PotentialImport, PotentialImportMode, PotentialPipe} from './scope';
3232
import {ElementSymbol, Symbol, TcbLocation, TemplateSymbol} from './symbols';
@@ -67,10 +67,10 @@ export interface TemplateTypeChecker {
6767
getDiagnosticsForFile(sf: ts.SourceFile, optimizeFor: OptimizeFor): ts.Diagnostic[];
6868

6969
/**
70-
* Given a `shim` and position within the file, returns information for mapping back to a template
70+
* Given a `shim` and position within the file, returns information for mapping back to a source
7171
* location.
7272
*/
73-
getTemplateMappingAtTcbLocation(tcbLocation: TcbLocation): FullTemplateMapping | null;
73+
getSourceMappingAtTcbLocation(tcbLocation: TcbLocation): FullSourceMapping | null;
7474

7575
/**
7676
* Get all `ts.Diagnostic`s currently available that pertain to the given component.

packages/compiler-cli/src/ngtsc/typecheck/extended/checks/interpolated_signal_not_invoked/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ function buildDiagnosticForSignal(
9696
// check for `{{ mySignal }}`
9797
const symbol = ctx.templateTypeChecker.getSymbolOfNode(node, component);
9898
if (symbol !== null && symbol.kind === SymbolKind.Expression && isSignalReference(symbol)) {
99-
const templateMapping = ctx.templateTypeChecker.getTemplateMappingAtTcbLocation(
99+
const templateMapping = ctx.templateTypeChecker.getSourceMappingAtTcbLocation(
100100
symbol.tcbLocation,
101101
)!;
102102
const errorString = `${node.name} is a function and should be invoked: ${node.name}()`;
@@ -116,7 +116,7 @@ function buildDiagnosticForSignal(
116116
symbolOfReceiver.kind === SymbolKind.Expression &&
117117
isSignalReference(symbolOfReceiver)
118118
) {
119-
const templateMapping = ctx.templateTypeChecker.getTemplateMappingAtTcbLocation(
119+
const templateMapping = ctx.templateTypeChecker.getSourceMappingAtTcbLocation(
120120
symbolOfReceiver.tcbLocation,
121121
)!;
122122

packages/compiler-cli/src/ngtsc/typecheck/extended/checks/nullish_coalescing_not_nullable/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ class NullishCoalescingNotNullableCheck extends TemplateCheckWithVisitor<ErrorCo
5151
if (symbol.kind !== SymbolKind.Expression) {
5252
return [];
5353
}
54-
const templateMapping = ctx.templateTypeChecker.getTemplateMappingAtTcbLocation(
54+
const templateMapping = ctx.templateTypeChecker.getSourceMappingAtTcbLocation(
5555
symbol.tcbLocation,
5656
);
5757
if (templateMapping === null) {

packages/compiler-cli/src/ngtsc/typecheck/extended/checks/optional_chain_not_nullable/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ class OptionalChainNotNullableCheck extends TemplateCheckWithVisitor<ErrorCode.O
5656
if (symbol.kind !== SymbolKind.Expression) {
5757
return [];
5858
}
59-
const templateMapping = ctx.templateTypeChecker.getTemplateMappingAtTcbLocation(
59+
const templateMapping = ctx.templateTypeChecker.getSourceMappingAtTcbLocation(
6060
symbol.tcbLocation,
6161
);
6262
if (templateMapping === null) {

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

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ import {isShim} from '../../shims';
5050
import {getSourceFileOrNull, isSymbolWithValueDeclaration} from '../../util/src/typescript';
5151
import {
5252
ElementSymbol,
53-
FullTemplateMapping,
53+
FullSourceMapping,
5454
GlobalCompletion,
5555
NgTemplateDiagnostic,
5656
OptimizeFor,
@@ -80,8 +80,8 @@ import {
8080
} from './context';
8181
import {shouldReportDiagnostic, translateDiagnostic} from './diagnostics';
8282
import {TypeCheckShimGenerator} from './shim';
83-
import {TemplateSourceManager} from './source';
84-
import {findTypeCheckBlock, getTemplateMapping, TemplateSourceResolver} from './tcb_util';
83+
import {DirectiveSourceManager} from './source';
84+
import {findTypeCheckBlock, getSourceMapping, TypeCheckSourceResolver} from './tcb_util';
8585
import {SymbolBuilder} from './template_symbol_builder';
8686

8787
const REGISTRY = new DomElementSchemaRegistry();
@@ -262,7 +262,7 @@ export class TemplateTypeCheckerImpl implements TemplateTypeChecker {
262262
return null;
263263
}
264264

265-
getTemplateMappingAtTcbLocation(tcbLocation: TcbLocation): FullTemplateMapping | null {
265+
getSourceMappingAtTcbLocation(tcbLocation: TcbLocation): FullSourceMapping | null {
266266
const fileRecord = this.getFileRecordForTcbLocation(tcbLocation);
267267
if (fileRecord === null) {
268268
return null;
@@ -272,7 +272,7 @@ export class TemplateTypeCheckerImpl implements TemplateTypeChecker {
272272
if (shimSf === undefined) {
273273
return null;
274274
}
275-
return getTemplateMapping(
275+
return getSourceMapping(
276276
shimSf,
277277
tcbLocation.positionInFile,
278278
fileRecord.sourceManager,
@@ -466,7 +466,7 @@ export class TemplateTypeCheckerImpl implements TemplateTypeChecker {
466466
const sfPath = absoluteFromSourceFile(clazz.getSourceFile());
467467
const fileRecord = this.state.get(sfPath)!;
468468
const id = fileRecord.sourceManager.getTypeCheckId(clazz);
469-
const mapping = fileRecord.sourceManager.getSourceMapping(id);
469+
const mapping = fileRecord.sourceManager.getTemplateSourceMapping(id);
470470

471471
return {
472472
...makeTemplateDiagnostic(
@@ -648,7 +648,7 @@ export class TemplateTypeCheckerImpl implements TemplateTypeChecker {
648648
if (!this.state.has(path)) {
649649
this.state.set(path, {
650650
hasInlines: false,
651-
sourceManager: new TemplateSourceManager(),
651+
sourceManager: new DirectiveSourceManager(),
652652
isComplete: false,
653653
shimData: new Map(),
654654
});
@@ -1010,7 +1010,7 @@ export class TemplateTypeCheckerImpl implements TemplateTypeChecker {
10101010

10111011
function convertDiagnostic(
10121012
diag: ts.Diagnostic,
1013-
sourceResolver: TemplateSourceResolver,
1013+
sourceResolver: TypeCheckSourceResolver,
10141014
): TemplateDiagnostic | null {
10151015
if (!shouldReportDiagnostic(diag)) {
10161016
return null;
@@ -1030,10 +1030,10 @@ export interface FileTypeCheckingData {
10301030
hasInlines: boolean;
10311031

10321032
/**
1033-
* Source mapping information for mapping diagnostics from inlined type check blocks back to the
1034-
* original template.
1033+
* Information for mapping diagnostics from inlined type check blocks
1034+
* back to their original sources.
10351035
*/
1036-
sourceManager: TemplateSourceManager;
1036+
sourceManager: DirectiveSourceManager;
10371037

10381038
/**
10391039
* Data for each shim generated from this input file.
@@ -1056,7 +1056,7 @@ export interface FileTypeCheckingData {
10561056
class WholeProgramTypeCheckingHost implements TypeCheckingHost {
10571057
constructor(private impl: TemplateTypeCheckerImpl) {}
10581058

1059-
getSourceManager(sfPath: AbsoluteFsPath): TemplateSourceManager {
1059+
getSourceManager(sfPath: AbsoluteFsPath): DirectiveSourceManager {
10601060
return this.impl.getFileData(sfPath).sourceManager;
10611061
}
10621062

@@ -1099,7 +1099,7 @@ class SingleFileTypeCheckingHost implements TypeCheckingHost {
10991099
}
11001100
}
11011101

1102-
getSourceManager(sfPath: AbsoluteFsPath): TemplateSourceManager {
1102+
getSourceManager(sfPath: AbsoluteFsPath): DirectiveSourceManager {
11031103
this.assertPath(sfPath);
11041104
return this.fileData.sourceManager;
11051105
}

packages/compiler-cli/src/ngtsc/typecheck/src/context.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ import {Environment} from './environment';
4141
import {OutOfBandDiagnosticRecorder, OutOfBandDiagnosticRecorderImpl} from './oob';
4242
import {ReferenceEmitEnvironment} from './reference_emit_environment';
4343
import {TypeCheckShimGenerator} from './shim';
44-
import {TemplateSourceManager} from './source';
44+
import {DirectiveSourceManager} from './source';
4545
import {requiresInlineTypeCheckBlock, TcbInliningRequirement} from './tcb_util';
4646
import {generateTypeCheckBlock, TcbGenericContextBehavior} from './type_check_block';
4747
import {TypeCheckFile} from './type_check_file';
@@ -106,7 +106,7 @@ export interface PendingFileTypeCheckingData {
106106
* Source mapping information for mapping diagnostics from inlined type check blocks back to the
107107
* original template.
108108
*/
109-
sourceManager: TemplateSourceManager;
109+
sourceManager: DirectiveSourceManager;
110110

111111
/**
112112
* Map of in-progress shim data for shims generated from this input file.
@@ -145,9 +145,9 @@ export interface PendingShimData {
145145
*/
146146
export interface TypeCheckingHost {
147147
/**
148-
* Retrieve the `TemplateSourceManager` responsible for directives in the given input file path.
148+
* Retrieve the `DirectiveSourceManager` responsible for directives in the given input file path.
149149
*/
150-
getSourceManager(sfPath: AbsoluteFsPath): TemplateSourceManager;
150+
getSourceManager(sfPath: AbsoluteFsPath): DirectiveSourceManager;
151151

152152
/**
153153
* Whether a particular class should be included in the current type-checking pass.
@@ -322,8 +322,8 @@ export class TypeCheckContextImpl implements TypeCheckContext {
322322
}
323323

324324
if (templateContext !== null) {
325-
fileData.sourceManager.captureSource(
326-
ref.node,
325+
fileData.sourceManager.captureTemplateSource(
326+
id,
327327
templateContext.sourceMapping,
328328
templateContext.file,
329329
);

packages/compiler-cli/src/ngtsc/typecheck/src/diagnostics.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import ts from 'typescript';
1111
import {TemplateDiagnostic, TypeCheckId} from '../api';
1212
import {makeTemplateDiagnostic} from '../diagnostics';
1313

14-
import {getTemplateMapping, TemplateSourceResolver} from './tcb_util';
14+
import {getSourceMapping, TypeCheckSourceResolver} from './tcb_util';
1515

1616
/**
1717
* Wraps the node in parenthesis such that inserted span comments become attached to the proper
@@ -94,12 +94,12 @@ export function shouldReportDiagnostic(diagnostic: ts.Diagnostic): boolean {
9494
*/
9595
export function translateDiagnostic(
9696
diagnostic: ts.Diagnostic,
97-
resolver: TemplateSourceResolver,
97+
resolver: TypeCheckSourceResolver,
9898
): TemplateDiagnostic | null {
9999
if (diagnostic.file === undefined || diagnostic.start === undefined) {
100100
return null;
101101
}
102-
const fullMapping = getTemplateMapping(
102+
const fullMapping = getSourceMapping(
103103
diagnostic.file,
104104
diagnostic.start,
105105
resolver,
@@ -109,7 +109,7 @@ export function translateDiagnostic(
109109
return null;
110110
}
111111

112-
const {sourceLocation, templateSourceMapping, span} = fullMapping;
112+
const {sourceLocation, sourceMapping: templateSourceMapping, span} = fullMapping;
113113
return makeTemplateDiagnostic(
114114
sourceLocation.id,
115115
templateSourceMapping,

packages/compiler-cli/src/ngtsc/typecheck/src/dom.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import {ErrorCode, ngErrorCode} from '../../diagnostics';
1818
import {TemplateDiagnostic, TypeCheckId} from '../api';
1919
import {makeTemplateDiagnostic} from '../diagnostics';
2020

21-
import {TemplateSourceResolver} from './tcb_util';
21+
import {TypeCheckSourceResolver} from './tcb_util';
2222

2323
const REGISTRY = new DomElementSchemaRegistry();
2424
const REMOVE_XHTML_REGEX = /^:xhtml:/;
@@ -88,7 +88,7 @@ export class RegistryDomSchemaChecker implements DomSchemaChecker {
8888
return this._diagnostics;
8989
}
9090

91-
constructor(private resolver: TemplateSourceResolver) {}
91+
constructor(private resolver: TypeCheckSourceResolver) {}
9292

9393
checkElement(
9494
id: TypeCheckId,
@@ -102,7 +102,7 @@ export class RegistryDomSchemaChecker implements DomSchemaChecker {
102102
const name = element.name.replace(REMOVE_XHTML_REGEX, '');
103103

104104
if (!REGISTRY.hasElement(name, schemas)) {
105-
const mapping = this.resolver.getSourceMapping(id);
105+
const mapping = this.resolver.getTemplateSourceMapping(id);
106106

107107
const schemas = `'${hostIsStandalone ? '@Component' : '@NgModule'}.schemas'`;
108108
let errorMsg = `'${name}' is not a known element:\n`;
@@ -138,7 +138,7 @@ export class RegistryDomSchemaChecker implements DomSchemaChecker {
138138
hostIsStandalone: boolean,
139139
): void {
140140
if (!REGISTRY.hasProperty(element.name, name, schemas)) {
141-
const mapping = this.resolver.getSourceMapping(id);
141+
const mapping = this.resolver.getTemplateSourceMapping(id);
142142

143143
const decorator = hostIsStandalone ? '@Component' : '@NgModule';
144144
const schemas = `'${decorator}.schemas'`;

0 commit comments

Comments
 (0)