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

Function tcb

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

Source from the content-addressed store, hash-verified

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

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