MCPcopy Create free account
hub / github.com/angular/angular / TestTcbEnvironment

Class TestTcbEnvironment

packages/compiler/test/typecheck/util.ts:236–336  ·  view source on GitHub ↗

* Minimal, TypeScript-free `TcbEnvironment` that mimics a type-check file: shared declarations * (pipe instances and type constructors) are hoisted to the top of the file and every symbol * from another module is referenced through a namespace import (`i0`, `i1`, ...), allocated in the * order in

Source from the content-addressed store, hash-verified

234 * order in which modules are first requested.
235 */
236class TestTcbEnvironment implements TcbEnvironment {
237 private readonly imports = new Map<string, string>();
238 private readonly typeCtors = new Map<TcbReferenceKey, string>();
239 private readonly typeCtorStatements: TcbExpr[] = [];
240 private readonly pipeInsts = new Map<TcbReferenceKey, string>();
241 private readonly pipeInstStatements: TcbExpr[] = [];
242 private readonly tcbStatements: string[] = [];
243 private nextTypeCtorId = 1;
244 private nextPipeInstId = 1;
245
246 constructor(readonly config: TypeCheckingConfig) {}
247
248 referenceTcbValue(ref: TcbReferenceMetadata): TcbExpr {
249 if (ref.unexportedDiagnostic !== null) {
250 throw new Error(`Unable to import symbol ${ref.name}: ${ref.unexportedDiagnostic}`);
251 }
252 if (ref.isLocal || ref.moduleName === null) {
253 return new TcbExpr(ref.name);
254 }
255 return this.referenceExternalSymbol(ref.moduleName, ref.name);
256 }
257
258 referenceExternalSymbol(moduleName: string, name: string): TcbExpr {
259 let alias = this.imports.get(moduleName);
260 if (alias === undefined) {
261 alias = `i${this.imports.size}`;
262 this.imports.set(moduleName, alias);
263 }
264 return new TcbExpr(`${alias}.${name}`);
265 }
266
267 typeCtorFor(dir: TcbDirectiveMetadata): TcbExpr {
268 const existing = this.typeCtors.get(dir.ref.key);
269 if (existing !== undefined) {
270 return new TcbExpr(existing);
271 }
272
273 if (dir.requiresInlineTypeCtor) {
274 // The type constructor is declared inline in the directive class.
275 const expr = `${this.referenceTcbValue(dir.ref).print()}.ngTypeCtor`;
276 this.typeCtors.set(dir.ref.key, expr);
277 return new TcbExpr(expr);
278 }
279
280 const fnName = `_ctor${this.nextTypeCtorId++}`;
281 const meta: TypeCtorMetadata = {
282 fnName,
283 body: true,
284 fields: {inputs: dir.inputs},
285 coercedInputFields: dir.coercedInputFields,
286 };
287 this.typeCtorStatements.push(
288 generateTypeCtorDeclaration(
289 this,
290 meta,
291 this.referenceTcbValue(dir.ref),
292 dir.typeParameters ?? undefined,
293 ),

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected