Skip to content

Commit b2758d7

Browse files
JoostKdylhunn
authored andcommitted
refactor(compiler-cli): rename ShimLocation to TcbLocation (#45454)
Inline type check blocks (TCBs) are emitted into the original source file, but node positions would still be represented as a `ShimLocation` with a `shimPath` corresponding with the type-checking shim file. This results in inconsistencies, as the `positionInShimFile` field of `ShimLocation` would not correspond with the `shimPath` of that `ShimLocation`. This commit is a precursor to letting `ShimLocation` also represent the correct location for inline type check blocks, by renaming the interface to `TcbLocation`. A followup commit addresses the actual inconsistency. PR Close #45454
1 parent e55e98d commit b2758d7

File tree

16 files changed

+137
-137
lines changed

16 files changed

+137
-137
lines changed

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import {ErrorCode} from '../../diagnostics';
1515
import {FullTemplateMapping, NgTemplateDiagnostic, TypeCheckableDirectiveMeta} from './api';
1616
import {GlobalCompletion} from './completion';
1717
import {DirectiveInScope, PipeInScope} from './scope';
18-
import {ElementSymbol, ShimLocation, Symbol, TemplateSymbol} from './symbols';
18+
import {ElementSymbol, Symbol, TcbLocation, TemplateSymbol} from './symbols';
1919

2020
/**
2121
* Interface to the Angular Template Type Checker to extract diagnostics and intelligence from the
@@ -56,7 +56,7 @@ export interface TemplateTypeChecker {
5656
* Given a `shim` and position within the file, returns information for mapping back to a template
5757
* location.
5858
*/
59-
getTemplateMappingAtShimLocation(shimLocation: ShimLocation): FullTemplateMapping|null;
59+
getTemplateMappingAtTcbLocation(tcbLocation: TcbLocation): FullTemplateMapping|null;
6060

6161
/**
6262
* Get all `ts.Diagnostic`s currently available that pertain to the given component.
@@ -113,19 +113,19 @@ export interface TemplateTypeChecker {
113113

114114

115115
/**
116-
* For the given expression node, retrieve a `ShimLocation` that can be used to perform
116+
* For the given expression node, retrieve a `TcbLocation` that can be used to perform
117117
* autocompletion at that point in the expression, if such a location exists.
118118
*/
119119
getExpressionCompletionLocation(
120-
expr: PropertyRead|SafePropertyRead, component: ts.ClassDeclaration): ShimLocation|null;
120+
expr: PropertyRead|SafePropertyRead, component: ts.ClassDeclaration): TcbLocation|null;
121121

122122
/**
123123
* For the given node represents a `LiteralPrimitive`(the `TextAttribute` represents a string
124-
* literal), retrieve a `ShimLocation` that can be used to perform autocompletion at that point in
124+
* literal), retrieve a `TcbLocation` that can be used to perform autocompletion at that point in
125125
* the node, if such a location exists.
126126
*/
127127
getLiteralCompletionLocation(
128-
strNode: LiteralPrimitive|TmplAstTextAttribute, component: ts.ClassDeclaration): ShimLocation
128+
strNode: LiteralPrimitive|TmplAstTextAttribute, component: ts.ClassDeclaration): TcbLocation
129129
|null;
130130

131131
/**

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
import {TmplAstReference, TmplAstVariable} from '@angular/compiler';
1010

11-
import {ShimLocation} from './symbols';
11+
import {TcbLocation} from './symbols';
1212

1313
/**
1414
* An autocompletion source of any kind.
@@ -60,7 +60,7 @@ export interface GlobalCompletion {
6060
* A location within the type-checking shim where TypeScript's completion APIs can be used to
6161
* access completions for the template's component context (component class members).
6262
*/
63-
componentContext: ShimLocation;
63+
componentContext: TcbLocation;
6464

6565
/**
6666
* `Map` of local references and variables that are visible at the requested level of the
@@ -76,5 +76,5 @@ export interface GlobalCompletion {
7676
* A location within the type-checking shim where TypeScript's completion APIs can be used to
7777
* access completions for the AST node of the cursor position (primitive constants).
7878
*/
79-
nodeContext: ShimLocation|null;
79+
nodeContext: TcbLocation|null;
8080
}

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

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -39,16 +39,19 @@ export type Symbol = InputBindingSymbol|OutputBindingSymbol|ElementSymbol|Refere
3939
*/
4040
export type TemplateDeclarationSymbol = ReferenceSymbol|VariableSymbol;
4141

42-
/** Information about where a `ts.Node` can be found in the type check block shim file. */
43-
export interface ShimLocation {
42+
/**
43+
* Information about where a `ts.Node` can be found in the type check file. This can either be
44+
* a type-checking shim file, or an original source file for inline type check blocks.
45+
*/
46+
export interface TcbLocation {
4447
/**
4548
* The fully qualified path of the file which contains the generated TypeScript type check
4649
* code for the component's template.
4750
*/
48-
shimPath: AbsoluteFsPath;
51+
tcbPath: AbsoluteFsPath;
4952

50-
/** The location in the shim file where node appears. */
51-
positionInShimFile: number;
53+
/** The location in the file where node appears. */
54+
positionInFile: number;
5255
}
5356

5457
/**
@@ -62,7 +65,7 @@ export interface TsNodeSymbolInfo {
6265
tsSymbol: ts.Symbol|null;
6366

6467
/** The position of the most relevant part of the template node. */
65-
shimLocation: ShimLocation;
68+
tcbLocation: TcbLocation;
6669
}
6770

6871
/**
@@ -81,7 +84,7 @@ export interface ExpressionSymbol {
8184
tsSymbol: ts.Symbol|null;
8285

8386
/** The position of the most relevant part of the expression. */
84-
shimLocation: ShimLocation;
87+
tcbLocation: TcbLocation;
8588
}
8689

8790
/** Represents either an input or output binding in a template. */
@@ -101,7 +104,7 @@ export interface BindingSymbol {
101104
target: DirectiveSymbol|ElementSymbol|TemplateSymbol;
102105

103106
/** The location in the shim file where the field access for the binding appears. */
104-
shimLocation: ShimLocation;
107+
tcbLocation: TcbLocation;
105108
}
106109

107110
/**
@@ -171,15 +174,15 @@ export interface ReferenceSymbol {
171174
* ```
172175
* This `targetLocation` is `[_t1 variable declaration].getStart()`.
173176
*/
174-
targetLocation: ShimLocation;
177+
targetLocation: TcbLocation;
175178

176179
/**
177180
* The location in the TCB for the identifier node in the reference variable declaration.
178181
* For example, given a variable declaration statement for a template reference:
179182
* `var _t2 = _t1`, this location is `[_t2 node].getStart()`. This location can
180183
* be used to find references to the variable within the template.
181184
*/
182-
referenceVarLocation: ShimLocation;
185+
referenceVarLocation: TcbLocation;
183186
}
184187

185188
/**
@@ -211,13 +214,13 @@ export interface VariableSymbol {
211214
/**
212215
* The location in the shim file for the identifier that was declared for the template variable.
213216
*/
214-
localVarLocation: ShimLocation;
217+
localVarLocation: TcbLocation;
215218

216219
/**
217220
* The location in the shim file for the initializer node of the variable that represents the
218221
* template variable.
219222
*/
220-
initializerLocation: ShimLocation;
223+
initializerLocation: TcbLocation;
221224
}
222225

223226
/**
@@ -236,7 +239,7 @@ export interface ElementSymbol {
236239
directives: DirectiveSymbol[];
237240

238241
/** The location in the shim file for the variable that holds the type of the element. */
239-
shimLocation: ShimLocation;
242+
tcbLocation: TcbLocation;
240243

241244
templateNode: TmplAstElement;
242245
}
@@ -261,7 +264,7 @@ export interface DirectiveSymbol extends DirectiveInScope {
261264
tsType: ts.Type;
262265

263266
/** The location in the shim file for the variable that holds the type of the directive. */
264-
shimLocation: ShimLocation;
267+
tcbLocation: TcbLocation;
265268
}
266269

267270
/**
@@ -292,7 +295,7 @@ export interface PipeSymbol {
292295
tsSymbol: ts.Symbol|null;
293296

294297
/** The position of the transform call in the template. */
295-
shimLocation: ShimLocation;
298+
tcbLocation: TcbLocation;
296299

297300
/** The symbol for the pipe class as an instance that appears in the TCB. */
298301
classSymbol: ClassSymbol;
@@ -307,5 +310,5 @@ export interface ClassSymbol {
307310
tsSymbol: SymbolWithValueDeclaration;
308311

309312
/** The position for the variable declaration for the class instance. */
310-
shimLocation: ShimLocation;
313+
tcbLocation: TcbLocation;
311314
}

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,7 @@ class NullishCoalescingNotNullableCheck extends
5050
if (symbol.kind !== SymbolKind.Expression) {
5151
return [];
5252
}
53-
const span =
54-
ctx.templateTypeChecker.getTemplateMappingAtShimLocation(symbol.shimLocation)!.span;
53+
const span = ctx.templateTypeChecker.getTemplateMappingAtTcbLocation(symbol.tcbLocation)!.span;
5554
const diagnostic = ctx.makeTemplateDiagnostic(
5655
span,
5756
`The left side of this nullish coalescing operation does not include 'null' or 'undefined' in its type, therefore the '??' operator can be safely removed.`);

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

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import {ClassDeclaration, isNamedClassDeclaration, ReflectionHost} from '../../r
1919
import {ComponentScopeReader, TypeCheckScopeRegistry} from '../../scope';
2020
import {isShim} from '../../shims';
2121
import {getSourceFileOrNull, isSymbolWithValueDeclaration} from '../../util/src/typescript';
22-
import {DirectiveInScope, ElementSymbol, FullTemplateMapping, GlobalCompletion, NgTemplateDiagnostic, OptimizeFor, PipeInScope, ProgramTypeCheckAdapter, ShimLocation, Symbol, TemplateDiagnostic, TemplateId, TemplateSymbol, TemplateTypeChecker, TypeCheckableDirectiveMeta, TypeCheckingConfig} from '../api';
22+
import {DirectiveInScope, ElementSymbol, FullTemplateMapping, GlobalCompletion, NgTemplateDiagnostic, OptimizeFor, PipeInScope, ProgramTypeCheckAdapter, Symbol, TcbLocation, TemplateDiagnostic, TemplateId, TemplateSymbol, TemplateTypeChecker, TypeCheckableDirectiveMeta, TypeCheckingConfig} from '../api';
2323
import {makeTemplateDiagnostic} from '../diagnostics';
2424

2525
import {CompletionEngine} from './completion';
@@ -151,20 +151,20 @@ export class TemplateTypeCheckerImpl implements TemplateTypeChecker {
151151
return null;
152152
}
153153

154-
getTemplateMappingAtShimLocation({shimPath, positionInShimFile}: ShimLocation):
155-
FullTemplateMapping|null {
156-
const records = this.getFileAndShimRecordsForPath(absoluteFrom(shimPath));
154+
getTemplateMappingAtTcbLocation({tcbPath, positionInFile}: TcbLocation): FullTemplateMapping
155+
|null {
156+
const records = this.getFileAndShimRecordsForPath(absoluteFrom(tcbPath));
157157
if (records === null) {
158158
return null;
159159
}
160160
const {fileRecord} = records;
161161

162-
const shimSf = this.programDriver.getProgram().getSourceFile(absoluteFrom(shimPath));
162+
const shimSf = this.programDriver.getProgram().getSourceFile(absoluteFrom(tcbPath));
163163
if (shimSf === undefined) {
164164
return null;
165165
}
166166
return getTemplateMapping(
167-
shimSf, positionInShimFile, fileRecord.sourceManager, /*isDiagnosticsRequest*/ false);
167+
shimSf, positionInFile, fileRecord.sourceManager, /*isDiagnosticsRequest*/ false);
168168
}
169169

170170
generateAllTypeCheckBlocks() {
@@ -270,7 +270,7 @@ export class TemplateTypeCheckerImpl implements TemplateTypeChecker {
270270
}
271271

272272
getExpressionCompletionLocation(
273-
ast: PropertyRead|SafePropertyRead, component: ts.ClassDeclaration): ShimLocation|null {
273+
ast: PropertyRead|SafePropertyRead, component: ts.ClassDeclaration): TcbLocation|null {
274274
const engine = this.getOrCreateCompletionEngine(component);
275275
if (engine === null) {
276276
return null;
@@ -280,7 +280,7 @@ export class TemplateTypeCheckerImpl implements TemplateTypeChecker {
280280
}
281281

282282
getLiteralCompletionLocation(
283-
node: LiteralPrimitive|TmplAstTextAttribute, component: ts.ClassDeclaration): ShimLocation
283+
node: LiteralPrimitive|TmplAstTextAttribute, component: ts.ClassDeclaration): TcbLocation
284284
|null {
285285
const engine = this.getOrCreateCompletionEngine(component);
286286
if (engine === null) {

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

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import {AST, EmptyExpr, ImplicitReceiver, LiteralPrimitive, PropertyRead, Proper
1010
import ts from 'typescript';
1111

1212
import {AbsoluteFsPath} from '../../file_system';
13-
import {CompletionKind, GlobalCompletion, ReferenceCompletion, ShimLocation, VariableCompletion} from '../api';
13+
import {CompletionKind, GlobalCompletion, ReferenceCompletion, TcbLocation, VariableCompletion} from '../api';
1414

1515
import {ExpressionIdentifier, findFirstMatchingNode} from './comments';
1616
import {TemplateData} from './context';
@@ -22,7 +22,7 @@ import {TemplateData} from './context';
2222
* surrounding TS program have changed.
2323
*/
2424
export class CompletionEngine {
25-
private componentContext: ShimLocation|null;
25+
private componentContext: TcbLocation|null;
2626

2727
/**
2828
* Cache of completions for various levels of the template, including the root template (`null`).
@@ -32,10 +32,10 @@ export class CompletionEngine {
3232
new Map<TmplAstTemplate|null, Map<string, ReferenceCompletion|VariableCompletion>>();
3333

3434
private expressionCompletionCache =
35-
new Map<PropertyRead|SafePropertyRead|LiteralPrimitive|TmplAstTextAttribute, ShimLocation>();
35+
new Map<PropertyRead|SafePropertyRead|LiteralPrimitive|TmplAstTextAttribute, TcbLocation>();
3636

3737

38-
constructor(private tcb: ts.Node, private data: TemplateData, private shimPath: AbsoluteFsPath) {
38+
constructor(private tcb: ts.Node, private data: TemplateData, private tcbPath: AbsoluteFsPath) {
3939
// Find the component completion expression within the TCB. This looks like: `ctx. /* ... */;`
4040
const globalRead = findFirstMatchingNode(this.tcb, {
4141
filter: ts.isPropertyAccessExpression,
@@ -44,11 +44,11 @@ export class CompletionEngine {
4444

4545
if (globalRead !== null) {
4646
this.componentContext = {
47-
shimPath: this.shimPath,
47+
tcbPath: this.tcbPath,
4848
// `globalRead.name` is an empty `ts.Identifier`, so its start position immediately follows
4949
// the `.` in `ctx.`. TS autocompletion APIs can then be used to access completion results
5050
// for the component context.
51-
positionInShimFile: globalRead.name.getStart(),
51+
positionInFile: globalRead.name.getStart(),
5252
};
5353
} else {
5454
this.componentContext = null;
@@ -74,16 +74,16 @@ export class CompletionEngine {
7474
return null;
7575
}
7676

77-
let nodeContext: ShimLocation|null = null;
77+
let nodeContext: TcbLocation|null = null;
7878
if (node instanceof EmptyExpr) {
7979
const nodeLocation = findFirstMatchingNode(this.tcb, {
8080
filter: ts.isIdentifier,
8181
withSpan: node.sourceSpan,
8282
});
8383
if (nodeLocation !== null) {
8484
nodeContext = {
85-
shimPath: this.shimPath,
86-
positionInShimFile: nodeLocation.getStart(),
85+
tcbPath: this.tcbPath,
86+
positionInFile: nodeLocation.getStart(),
8787
};
8888
}
8989
}
@@ -95,8 +95,8 @@ export class CompletionEngine {
9595
});
9696
if (nodeLocation) {
9797
nodeContext = {
98-
shimPath: this.shimPath,
99-
positionInShimFile: nodeLocation.getStart(),
98+
tcbPath: this.tcbPath,
99+
positionInFile: nodeLocation.getStart(),
100100
};
101101
}
102102
}
@@ -108,7 +108,7 @@ export class CompletionEngine {
108108
};
109109
}
110110

111-
getExpressionCompletionLocation(expr: PropertyRead|PropertyWrite|SafePropertyRead): ShimLocation
111+
getExpressionCompletionLocation(expr: PropertyRead|PropertyWrite|SafePropertyRead): TcbLocation
112112
|null {
113113
if (this.expressionCompletionCache.has(expr)) {
114114
return this.expressionCompletionCache.get(expr)!;
@@ -146,15 +146,15 @@ export class CompletionEngine {
146146
return null;
147147
}
148148

149-
const res: ShimLocation = {
150-
shimPath: this.shimPath,
151-
positionInShimFile: tsExpr.name.getEnd(),
149+
const res: TcbLocation = {
150+
tcbPath: this.tcbPath,
151+
positionInFile: tsExpr.name.getEnd(),
152152
};
153153
this.expressionCompletionCache.set(expr, res);
154154
return res;
155155
}
156156

157-
getLiteralCompletionLocation(expr: LiteralPrimitive|TmplAstTextAttribute): ShimLocation|null {
157+
getLiteralCompletionLocation(expr: LiteralPrimitive|TmplAstTextAttribute): TcbLocation|null {
158158
if (this.expressionCompletionCache.has(expr)) {
159159
return this.expressionCompletionCache.get(expr)!;
160160
}
@@ -186,9 +186,9 @@ export class CompletionEngine {
186186
// In the shimFile, if `tsExpr` is a string, the position should be in the quotes.
187187
positionInShimFile -= 1;
188188
}
189-
const res: ShimLocation = {
190-
shimPath: this.shimPath,
191-
positionInShimFile,
189+
const res: TcbLocation = {
190+
tcbPath: this.tcbPath,
191+
positionInFile: positionInShimFile,
192192
};
193193
this.expressionCompletionCache.set(expr, res);
194194
return res;

0 commit comments

Comments
 (0)