(sourceFile: SourceFile)
| 362 | ] |
| 363 | |
| 364 | export const createCompiledJsxContext = (sourceFile: SourceFile): CompiledJsxContext => { |
| 365 | const imports = collectImports(sourceFile) |
| 366 | |
| 367 | const normalizeCallee = (node: Node): Node => { |
| 368 | const expression = unwrapExpression(node) |
| 369 | |
| 370 | if (MorphNode.isBinaryExpression(expression) && expression.getOperatorToken().getKind() === SyntaxKind.CommaToken) { |
| 371 | return normalizeCallee(expression.getRight()) |
| 372 | } |
| 373 | |
| 374 | return expression |
| 375 | } |
| 376 | |
| 377 | const localDefinitionCache = new Map<string, ResolvedCallee | null>() |
| 378 | |
| 379 | const resolveIdentifierDefinition = (identifier: Identifier): ResolvedCallee | undefined => { |
| 380 | const name = identifier.getText() |
| 381 | if (localDefinitionCache.has(name)) { |
| 382 | return localDefinitionCache.get(name) ?? undefined |
| 383 | } |
| 384 | |
| 385 | localDefinitionCache.set(name, null) |
| 386 | |
| 387 | const resolveLocalAlias = (aliasNode: Node | undefined): ResolvedCallee | undefined => { |
| 388 | if (!aliasNode) return |
| 389 | |
| 390 | const expression = unwrapExpression(aliasNode) |
| 391 | |
| 392 | if (MorphNode.isIdentifier(expression)) { |
| 393 | const imported = imports.bundledNamed.get(expression.getText()) |
| 394 | if (imported) { |
| 395 | return { mod: imported.mod, importedName: imported.importedName } |
| 396 | } |
| 397 | |
| 398 | return resolveIdentifierDefinition(expression) |
| 399 | } |
| 400 | |
| 401 | if (MorphNode.isConditionalExpression(expression)) { |
| 402 | return resolveLocalAlias(expression.getWhenTrue()) ?? resolveLocalAlias(expression.getWhenFalse()) |
| 403 | } |
| 404 | |
| 405 | if ( |
| 406 | MorphNode.isBinaryExpression(expression) && |
| 407 | expression.getOperatorToken().getKind() === SyntaxKind.CommaToken |
| 408 | ) { |
| 409 | return resolveLocalAlias(expression.getRight()) |
| 410 | } |
| 411 | } |
| 412 | |
| 413 | const declaration = imports.localDeclarations.get(name) |
| 414 | if (!declaration) return |
| 415 | |
| 416 | if (MorphNode.isFunctionDeclaration(declaration)) { |
| 417 | const imported = resolveBundledHelperImport(declaration.getName() ?? name, declaration) |
| 418 | if (!imported) return |
| 419 | |
| 420 | const resolved = { mod: imported.mod, importedName: imported.importedName } |
| 421 | localDefinitionCache.set(name, resolved) |
no test coverage detected