(
template: string,
declarations: TestDeclaration[] = [],
config?: Partial<TypeCheckingConfig>,
options?: {emitSpans?: boolean},
templateParserOptions?: ParseTemplateOptions,
foreignComponents: string[] = [],
)
| 110 | export * from './src/test_support'; |
| 111 | |
| 112 | export function tcb( |
| 113 | template: string, |
| 114 | declarations: TestDeclaration[] = [], |
| 115 | config?: Partial<TypeCheckingConfig>, |
| 116 | options?: {emitSpans?: boolean}, |
| 117 | templateParserOptions?: ParseTemplateOptions, |
| 118 | foreignComponents: string[] = [], |
| 119 | ): string { |
| 120 | const codeLines = [ |
| 121 | 'declare const ɵNgFieldDirective: unique symbol;', |
| 122 | `export class Test<T extends string> {}`, |
| 123 | ]; |
| 124 | |
| 125 | (function addCodeLines(currentDeclarations) { |
| 126 | for (const decl of currentDeclarations) { |
| 127 | if (decl.type === 'directive' && decl.hostDirectives) { |
| 128 | addCodeLines(decl.hostDirectives.map((hostDir) => hostDir.directive)); |
| 129 | } |
| 130 | |
| 131 | codeLines.push( |
| 132 | decl.code ?? |
| 133 | `export class ${decl.name}${decl.type === 'directive' || decl.isGeneric ? '<T extends string>' : ''} { ${ |
| 134 | (decl as TestDirective).hasNgFieldDirective === true ? '[ɵNgFieldDirective]: any;' : '' |
| 135 | } }`, |
| 136 | ); |
| 137 | } |
| 138 | })(declarations); |
| 139 | |
| 140 | const rootFilePath = absoluteFrom('/synthetic.ts'); |
| 141 | const {program, host} = makeProgram([ |
| 142 | {name: rootFilePath, contents: codeLines.join('\n'), isRoot: true}, |
| 143 | ]); |
| 144 | |
| 145 | const sf = getSourceFileOrError(program, rootFilePath); |
| 146 | const clazz = getClass(sf, 'Test'); |
| 147 | const templateUrl = 'synthetic.html'; |
| 148 | const {nodes, errors} = parseTemplate(template, templateUrl, templateParserOptions); |
| 149 | |
| 150 | if (errors !== null) { |
| 151 | throw new Error('Template parse errors: \n' + errors.join('\n')); |
| 152 | } |
| 153 | |
| 154 | const {matcher, pipes, foreignMatcher} = prepareDeclarations( |
| 155 | declarations, |
| 156 | (decl) => getClass(sf, decl.name), |
| 157 | new Map(), |
| 158 | foreignComponents, |
| 159 | ); |
| 160 | const binder = new R3TargetBinder<DirectiveMeta>(matcher, foreignMatcher); |
| 161 | const boundTarget = binder.bind({template: nodes}); |
| 162 | |
| 163 | const id = 'tcb' as TypeCheckId; |
| 164 | const meta: TypeCheckBlockMetadata = { |
| 165 | id, |
| 166 | boundTarget, |
| 167 | pipes, |
| 168 | schemas: [], |
| 169 | isStandalone: false, |
no test coverage detected