(
template: string,
declarations: TestDeclaration[] = [],
config?: Partial<TypeCheckingConfig>,
options?: {emitSpans?: boolean},
templateParserOptions?: ParseTemplateOptions,
foreignComponents: string[] = [],
)
| 374 | export type TestDeclaration = TestDirective | TestPipe; |
| 375 | |
| 376 | export 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, |
no test coverage detected