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