MCPcopy Index your code
hub / github.com/angular/angular / tcb

Function tcb

packages/compiler-cli/src/ngtsc/typecheck/testing/index.ts:359–477  ·  view source on GitHub ↗
(
  template: string,
  declarations: TestDeclaration[] = [],
  config?: Partial<TypeCheckingConfig>,
  options?: {emitSpans?: boolean},
  templateParserOptions?: ParseTemplateOptions,
)

Source from the content-addressed store, hash-verified

357export type TestDeclaration = TestDirective | TestPipe;
358
359export function tcb(
360 template: string,
361 declarations: TestDeclaration[] = [],
362 config?: Partial<TypeCheckingConfig>,
363 options?: {emitSpans?: boolean},
364 templateParserOptions?: ParseTemplateOptions,
365): string {
366 const codeLines = [
367 'declare const ɵNgFieldDirective: unique symbol;',
368 `export class Test<T extends string> {}`,
369 ];
370
371 (function addCodeLines(currentDeclarations) {
372 for (const decl of currentDeclarations) {
373 if (decl.type === 'directive' && decl.hostDirectives) {
374 addCodeLines(decl.hostDirectives.map((hostDir) => hostDir.directive));
375 }
376
377 codeLines.push(
378 decl.code ??
379 `export class ${decl.name}${decl.type === 'directive' || decl.isGeneric ? '<T extends string>' : ''} { ${
380 (decl as TestDirective).hasNgFieldDirective === true ? '[ɵNgFieldDirective]: any;' : ''
381 } }`,
382 );
383 }
384 })(declarations);
385
386 const rootFilePath = absoluteFrom('/synthetic.ts');
387 const {program, host} = makeProgram([
388 {name: rootFilePath, contents: codeLines.join('\n'), isRoot: true},
389 ]);
390
391 const sf = getSourceFileOrError(program, rootFilePath);
392 const clazz = getClass(sf, 'Test');
393 const templateUrl = 'synthetic.html';
394 const {nodes, errors} = parseTemplate(template, templateUrl, templateParserOptions);
395 const selectorlessEnabled = templateParserOptions?.enableSelectorless ?? false;
396
397 if (errors !== null) {
398 throw new Error('Template parse errors: \n' + errors.join('\n'));
399 }
400
401 const {matcher, pipes} = prepareDeclarations(
402 declarations,
403 (decl) => getClass(sf, decl.name),
404 new Map(),
405 selectorlessEnabled,
406 );
407 const binder = new R3TargetBinder<DirectiveMeta>(matcher);
408 const boundTarget = binder.bind({template: nodes});
409
410 const id = 'tcb' as TypeCheckId;
411 const meta: TypeCheckBlockMetadata = {
412 id,
413 boundTarget,
414 pipes,
415 schemas: [],
416 isStandalone: false,

Callers 3

tcbWithSpansFunction · 0.90
selectorlessTcbFunction · 0.90

Calls 14

bindMethod · 0.95
addTypeCheckBlockMethod · 0.95
renderMethod · 0.95
absoluteFromFunction · 0.90
makeProgramFunction · 0.90
getSourceFileOrErrorFunction · 0.90
parseTemplateFunction · 0.90
prepareDeclarationsFunction · 0.85
mapMethod · 0.80
getTypeCheckerMethod · 0.80
getClassFunction · 0.70
joinMethod · 0.65

Tested by

no test coverage detected